-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_translator.py
More file actions
179 lines (152 loc) · 5.28 KB
/
Copy pathtest_translator.py
File metadata and controls
179 lines (152 loc) · 5.28 KB
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
import unittest
import cqp_tree
from cqp_tree import *
from cqp_tree.frontends.deptreepy import translate_deptreepy
CONFIG = cqp_tree.default_configuration()
class TranslationTests(unittest.TestCase):
def test_tree(self):
with self.assertRaises(NotSupported):
translate_deptreepy('TREE a b c', CONFIG)
def test_tree_(self):
translate_deptreepy('TREE_ ((AND (POS NOUN) (DEPREL det))) (OR (LEMMA IN a b c)))', CONFIG)
def test_empty_string(self):
with self.assertRaises(ParsingFailed):
translate_deptreepy('', CONFIG)
def test_field_comparison(self):
(q,) = translate_deptreepy('field a', CONFIG).queries
(token,) = q.tokens
self.assertEqual(
token.attributes,
Comparison(
Attribute(None, 'field'),
'=',
Literal('"a"'),
),
)
def test_field_in_comparison(self):
(q,) = translate_deptreepy('field IN a b', CONFIG).queries
(token,) = q.tokens
self.assertEqual(
token.attributes,
Disjunction(
(
Comparison(
Attribute(None, 'field'),
'=',
Literal('"a"'),
),
Comparison(
Attribute(None, 'field'),
'=',
Literal('"b"'),
),
)
),
)
def test_field_contains_comparison(self):
(q,) = translate_deptreepy('field_ a', CONFIG).queries
(token,) = q.tokens
self.assertEqual(
token.attributes,
Comparison(
Attribute(None, 'field'),
'contains',
Literal('"a"'),
),
)
def test_field_in_contains_comparison(self):
(q,) = translate_deptreepy('field_ IN a b', CONFIG).queries
(token,) = q.tokens
self.assertEqual(
token.attributes,
Disjunction(
(
Comparison(
Attribute(None, 'field'),
'contains',
Literal('"a"'),
),
Comparison(
Attribute(None, 'field'),
'contains',
Literal('"b"'),
),
)
),
)
def test_true(self):
(q,) = translate_deptreepy('TRUE', CONFIG).queries
self.assertEqual(1, len(q.tokens), 'Query should have one token.')
self.assertEqual(0, len(q.predicates), 'Query should not have any predicates.')
(token,) = q.tokens
self.assertIsNone(token.attributes)
def test_and_predicate(self):
(q,) = translate_deptreepy('(AND (a 1) (b 2))', CONFIG).queries
(token,) = q.tokens
self.assertEqual(
token.attributes,
Conjunction(
(
Comparison(
Attribute(None, 'a'),
'=',
Literal('"1"'),
),
Comparison(
Attribute(None, 'b'),
'=',
Literal('"2"'),
),
)
),
)
def test_or_predicate(self):
(q,) = translate_deptreepy('(OR (a 1) (b 2))', CONFIG).queries
(token,) = q.tokens
self.assertEqual(
token.attributes,
Disjunction(
(
Comparison(
Attribute(None, 'a'),
'=',
Literal('"1"'),
),
Comparison(
Attribute(None, 'b'),
'=',
Literal('"2"'),
),
)
),
)
def test_not_predicate(self):
(q,) = translate_deptreepy('(NOT (a 1))', CONFIG).queries
(token,) = q.tokens
self.assertEqual(
token.attributes,
Negation(
Comparison(
Attribute(None, 'a'),
'=',
Literal('"1"'),
)
),
)
def test_and_dependency(self):
plan = translate_deptreepy('(AND (TREE_ (r 1) (d 1)) (TREE_ (r 2) (d 2)))', CONFIG)
self.assertEqual(len(plan.queries), 2)
self.assertEqual(len(plan.operations), 1)
(operation,) = plan.operations
self.assertEqual(operation.identifier, plan.goal)
self.assertEqual(operation.operator, SetOperator.CONJUNCTION)
def test_or_dependency(self):
plan = translate_deptreepy('(OR (TREE_ (r 1) (d 1)) (TREE_ (r 2) (d 2)))', CONFIG)
self.assertEqual(len(plan.queries), 2)
self.assertEqual(len(plan.operations), 1)
(operation,) = plan.operations
self.assertEqual(operation.identifier, plan.goal)
self.assertEqual(operation.operator, SetOperator.DISJUNCTION)
def test_not_dependency(self):
with self.assertRaises(NotSupported):
translate_deptreepy('(NOT (TREE_ (r 1) (d 1)))', CONFIG)