forked from meta-toolkit/meta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser_test.cpp
203 lines (169 loc) · 6.55 KB
/
parser_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
* @file parser_test.cpp
* @author Chase Geigle
* @author Sean Massung
*/
#include <sstream>
#include "bandit/bandit.h"
#include "meta/parser/io/ptb_reader.h"
#include "meta/parser/trees/visitors/visitor.h"
#include "meta/parser/trees/visitors/annotation_remover.h"
#include "meta/parser/trees/visitors/empty_remover.h"
#include "meta/parser/trees/visitors/unary_chain_remover.h"
#include "meta/parser/trees/visitors/multi_transformer.h"
#include "meta/parser/trees/visitors/head_finder.h"
#include "meta/parser/trees/visitors/binarizer.h"
#include "meta/parser/trees/visitors/debinarizer.h"
#include "meta/parser/trees/internal_node.h"
#include "meta/parser/trees/leaf_node.h"
using namespace bandit;
using namespace meta;
namespace {
parser::parse_tree tree(std::string input) {
std::stringstream in_ss{input};
auto in_trees = parser::io::extract_trees(in_ss);
return std::move(in_trees.front());
}
void assert_tree_equal(std::string input, std::string expected,
parser::tree_transformer& trns) {
std::stringstream in_ss{input};
auto in_trees = parser::io::extract_trees(in_ss);
in_trees.front().transform(trns);
std::stringstream exp_ss{expected};
auto exp_trees = parser::io::extract_trees(exp_ss);
AssertThat(in_trees.front(), Equals(exp_trees.front()));
}
struct annotation_checker : public parser::const_visitor<bool> {
bool operator()(const parser::leaf_node&) override {
return true;
}
bool operator()(const parser::internal_node& inode) override {
if (!inode.head_constituent())
return false;
if (!inode.head_lexicon())
return false;
bool res = true;
inode.each_child([&](const parser::node* child) {
res = res && child->accept(*this);
});
return res;
}
};
struct binary_checker : public parser::const_visitor<bool> {
bool operator()(const parser::leaf_node&) override {
return true;
}
bool operator()(const parser::internal_node& inode) override {
if (inode.num_children() > 2)
return false;
bool res = true;
inode.each_child([&](const parser::node* child) {
res = res && child->accept(*this);
});
return res;
}
};
}
go_bandit([]() {
using namespace parser;
describe("[parser] transformer", [&]() {
annotation_remover ann_remover;
empty_remover empty_rem;
unary_chain_remover uchain_rem;
multi_transformer<annotation_remover, empty_remover,
unary_chain_remover> multi;
it("should remove annotations", [&]() {
auto tree = "((X (Y (Z-XXX (Y z))) (Z|Q (Y=1 (X x)))))";
auto tree_noann = "((X (Y (Z (Y z))) (Z (Y (X x)))))";
assert_tree_equal(tree, tree_noann, ann_remover);
});
it("should remove empty nodes", [&]() {
auto tree = "((X (Y (-NONE- *)) (Z z) (W (Y (-NONE- *) (Q q)))))";
auto tree_noempty = "((X (Z z) (W (Y (Q q)))))";
assert_tree_equal(tree, tree_noempty, empty_rem);
});
it("should remove unary chains", [&]() {
auto tree = "((X (X (X (Y y) (Z z)) (X (X (X x))))))";
auto tree_nochain = "((X (X (Y y) (Z z)) (X x)))";
assert_tree_equal(tree, tree_nochain, uchain_rem);
});
it("should be able to perform multiple transformations", [&]() {
auto tree
= "((X (Y-NNN (-NONE- *)) (Z (Z (Z z))) (W (W (Y (-NONE- *) "
"(Q q))))))";
auto tree_trans = "((X (Z z) (W (Y (Q q)))))";
assert_tree_equal(tree, tree_trans, multi);
});
});
describe("[parser] head finder", [&]() {
it("should find all annotated heads", [&]() {
head_finder hf;
annotation_checker ac;
auto tr = tree(
"((S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ "
"likes) (S (VP (VBG eating) (NP (NN sausage))))) (. .)))");
tr.visit(hf);
AssertThat(tr.visit(ac), IsTrue());
});
});
describe("[parser] binarizer", [&]() {
head_finder hf;
binarizer bin;
binary_checker bin_check;
annotation_checker ann_check;
it("should make a binary tree", [&]() {
auto tr = tree(
"((S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ "
"likes) (S (VP (VBG eating) (NP (NN sausage))))) (. .)))");
tr.visit(hf);
tr.transform(bin);
AssertThat(tr.visit(bin_check), IsTrue());
});
it("should keep annotations", [&]() {
auto tr = tree(
"((S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ "
"likes) (S (VP (VBG eating) (NP (NN sausage))))) (. .)))");
tr.visit(hf);
tr.transform(bin);
AssertThat(tr.visit(ann_check), IsTrue());
});
it("should have correct output", [&]() {
auto tr = tree(
"((S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ "
"likes) (S (VP (VBG eating) (NP (NN sausage))))) (. .)))");
tr.visit(hf);
tr.transform(bin);
auto expected
= tree("((S (NP (PRP$ My) (NN dog)) (S* (ADVP (RB also)) "
"(S* (VP (VBZ likes) (S (VP (VBG eating) (NP (NN "
"sausage))))) (. .)))))");
AssertThat(tr, Equals(expected));
});
});
describe("[parser] debinarizer", [&]() {
head_finder hf;
binarizer bin;
debinarizer debin;
annotation_checker ann_check;
it("should debinarize to correct output", [&]() {
auto tr = tree(
"((S (S* (NP (PRP$ My) (NN dog)) (S* (ADVP (RB also)) (VP "
"(VBZ likes) (S (VP (VBG eating) (NP (NN sausage))))))) (. "
".)))");
tr.transform(debin);
auto expected = tree(
"((S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ "
"likes) (S (VP (VBG eating) (NP (NN sausage))))) (. .)))");
AssertThat(tr, Equals(expected));
});
it("should preserve annotations", [&]() {
auto tr = tree(
"((S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ "
"likes) (S (VP (VBG eating) (NP (NN sausage))))) (. .)))");
tr.visit(hf);
tr.transform(bin);
tr.transform(debin);
AssertThat(tr.visit(ann_check), IsTrue());
});
});
});