blob: 44a8a5c988e6b42d492ddc6e1f42505bf573d350 [file] [log] [blame]
Joachim Bingel019ba5c2014-04-28 14:59:04 +00001import static org.junit.Assert.*;
2
3import org.junit.Test;
4
5import de.ids_mannheim.korap.query.serialize.AqlTree;
Joachim Bingel019ba5c2014-04-28 14:59:04 +00006import de.ids_mannheim.korap.util.QueryException;
7
8public class AqlTreeTest {
9
10 AqlTree aqlt;
11 String map;
12 private String query;
13
14 private boolean equalsQueryContent(String res, String query) throws QueryException {
15 res = res.replaceAll(" ", "");
16 aqlt = new AqlTree(query);
17 String queryMap = aqlt.getRequestMap().get("query").toString().replaceAll(" ", "");
18 return res.equals(queryMap);
19 }
20
21 @Test
22 public void testContext() throws QueryException {
23 String contextString = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld";
24 aqlt = new AqlTree("Test");
25 assertEquals(contextString.replaceAll(" ", ""), aqlt.getRequestMap().get("@context").toString().replaceAll(" ", ""));
26 }
27
28 @Test
29 public void testSingleTokens() throws QueryException {
30 // "Mann"
31 query = "\"Mann\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000032 String token1 = "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000033 assertTrue(equalsQueryContent(token1, query));
34
35 // [orth!=Frau]
36 query = "tok!=\"Frau\"";
Joachim Bingelca4944e2014-06-13 13:55:10 +000037 String token2 = "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Frau, match=match:ne}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000038 assertTrue(equalsQueryContent(token2, query));
39
40 // Mann
41 query = "Mann";
42 String token4 = "{@type=korap:span, layer=Mann}";
43 assertTrue(equalsQueryContent(token4, query));
44 }
45
46 @Test
47 public void testSpans() throws QueryException {
48 query = "node";
49 String span1 =
50 "{@type=korap:span}";
51 aqlt = new AqlTree(query);
52 map = aqlt.getRequestMap().get("query").toString();
53 assertEquals(span1.replaceAll(" ", ""), map.replaceAll(" ", ""));
54 }
55
56 @Test
57 public void testRegex() throws QueryException {
58 query = "/Mann/";
Joachim Bingeleee549e2014-04-29 11:15:37 +000059 String regex1 = "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=Mann, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000060 aqlt = new AqlTree(query);
61 map = aqlt.getRequestMap().get("query").toString();
62 assertEquals(regex1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingele49b4672014-07-16 08:06:56 +000063
64 query = "/.*?Mann.*?/";
65 String regex2 = "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=.*?Mann.*?, match=match:eq}}";
66 aqlt = new AqlTree(query);
67 map = aqlt.getRequestMap().get("query").toString();
68 assertEquals(regex2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel019ba5c2014-04-28 14:59:04 +000069 }
70
71 @Test
72 public void testLayers() throws QueryException {
73 query = "cnx/cat=\"NP\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000074 String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000075 aqlt = new AqlTree(query);
76 map = aqlt.getRequestMap().get("query").toString();
77 assertEquals(layers1.replaceAll(" ", ""), map.replaceAll(" ", ""));
78
79 query = "treetagger/pos=\"NN\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000080 String layers2 = "{@type=korap:token, wrap={@type=korap:term, foundry=treetagger, layer=pos, key=NN, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000081 aqlt = new AqlTree(query);
82 map = aqlt.getRequestMap().get("query").toString();
83 assertEquals(layers2.replaceAll(" ", ""), map.replaceAll(" ", ""));
84 }
85
86 @Test
Joachim Bingel75038792014-05-19 15:12:23 +000087 public void testDirectDeclarationRelations() throws QueryException {
88 query = "node > node";
89 String ddr1 =
90 "{@type=korap:group, operation=operation:relation, operands=[" +
91 "{@type=korap:span}," +
92 "{@type=korap:span}" +
93 "], relation={@type=korap:treeRelation, reltype=dominance}" +
94 "}";
95 aqlt = new AqlTree(query);
96 map = aqlt.getRequestMap().get("query").toString();
97 assertEquals(ddr1.replaceAll(" ", ""), map.replaceAll(" ", ""));
98
99 query = "node > cnx/cat=\"NP\"";
100 String ddr2 =
101 "{@type=korap:group, operation=operation:relation, operands=[" +
102 "{@type=korap:span}," +
103 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}" +
104 "], relation={@type=korap:treeRelation, reltype=dominance}" +
105 "}";
106 aqlt = new AqlTree(query);
107 map = aqlt.getRequestMap().get("query").toString();
108 assertEquals(ddr2.replaceAll(" ", ""), map.replaceAll(" ", ""));
109
110 }
111
112 @Test
Joachim Bingelfc428812014-06-18 14:50:14 +0000113 public void testDefPredicationInversion() throws QueryException {
114 query = " #1 > #2 & cnx/cat=\"VP\" & cnx/cat=\"NP\"";
115 String dom1 =
116 "{@type=korap:group, operation=operation:relation, operands=[" +
117 "{@type=korap:span, foundry=cnx, layer=cat, key=VP, match=match:eq}," +
118 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}" +
119 "], relation={@type=korap:treeRelation, reltype=dominance}" +
120 "}";
121 aqlt = new AqlTree(query);
122 map = aqlt.getRequestMap().get("query").toString();
123 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
124 }
125
126 @Test
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000127 public void testSimpleDominance() throws QueryException {
128 query = "node & node & #2 > #1";
129 String dom1 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000130 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000131 "{@type=korap:span}," +
132 "{@type=korap:span}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000133 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000134 "}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000135 aqlt = new AqlTree(query);
136 map = aqlt.getRequestMap().get("query").toString();
137 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeleee549e2014-04-29 11:15:37 +0000138
139 query = "\"Mann\" & node & #2 > #1";
140 String dom2 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000141 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000142 "{@type=korap:span}," +
143 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000144 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000145 "}";
146 aqlt = new AqlTree(query);
147 map = aqlt.getRequestMap().get("query").toString();
148 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
149
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000150 query = "\"Mann\" & node & #2 >[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000151 String dom3 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000152 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000153 "{@type=korap:span}," +
154 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000155 "], relation={@type=korap:treeRelation, reltype=dominance, wrap=[{@type=korap:term, layer=cat, key=NP, match=match:eq}]}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000156 "}";
157 aqlt = new AqlTree(query);
158 map = aqlt.getRequestMap().get("query").toString();
159 assertEquals(dom3.replaceAll(" ", ""), map.replaceAll(" ", ""));
160
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000161 query = "\"Mann\" & node & #2 >@l[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000162 String dom4 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000163 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000164 "{@type=korap:span}," +
165 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000166 "], relation={@type=korap:treeRelation, reltype=dominance, index=0, wrap=[{@type=korap:term, layer=cat, key=NP, match=match:eq}]}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000167 "}";
168 aqlt = new AqlTree(query);
169 map = aqlt.getRequestMap().get("query").toString();
170 assertEquals(dom4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000171
172 query = "\"Mann\" & node & #2 >2,4 #1";
173 String dom5 =
174 "{@type=korap:group, operation=operation:relation, operands=[" +
175 "{@type=korap:span}," +
176 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
177 "], relation={@type=korap:treeRelation, reltype=dominance, " +
Joachim Bingel86195a52014-07-08 14:29:48 +0000178 "boundary={@type=korap:boundary, min=2, max=4}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000179 "}";
180 aqlt = new AqlTree(query);
181 map = aqlt.getRequestMap().get("query").toString();
182 assertEquals(dom5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000183 }
184
Joachim Bingel13421ef2014-05-07 16:22:58 +0000185 @Test
Joachim Bingelca4944e2014-06-13 13:55:10 +0000186 public void testMultipleDominance() throws QueryException {
Joachim Bingel949e3a82014-06-16 13:20:43 +0000187 query = "cat=\"CP\" & cat=\"VP\" & cat=\"NP\" & #1 > #2 > #3";
Joachim Bingelca4944e2014-06-13 13:55:10 +0000188 String dom1 =
189 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000190 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelfc428812014-06-18 14:50:14 +0000191 "{@type=korap:group, operation=operation:relation, operands=[" +
192 "{@type=korap:span, layer=cat, key=CP, match=match:eq}," +
193 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
194 "{@type=korap:span, layer=cat, key=VP, match=match:eq}" +
195 "]}" +
196 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000197 "]}," +
Joachim Bingel949e3a82014-06-16 13:20:43 +0000198 "{@type=korap:span, layer=cat, key=NP, match=match:eq}" +
Joachim Bingelca4944e2014-06-13 13:55:10 +0000199 "], relation={@type=korap:treeRelation, reltype=dominance}" +
200 "}";
201 aqlt = new AqlTree(query);
202 map = aqlt.getRequestMap().get("query").toString();
203 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel949e3a82014-06-16 13:20:43 +0000204
205 query = "cat=\"CP\" & cat=\"VP\" & cat=\"NP\" & cat=\"DP\" & #1 > #2 > #3 > #4";
206 String dom2 =
207 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000208 "{@type=korap:reference, operation=operation:focus, classRef=[1], operands=[" +
Joachim Bingel949e3a82014-06-16 13:20:43 +0000209 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000210 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelfc428812014-06-18 14:50:14 +0000211 "{@type=korap:group, operation=operation:relation, operands=[" +
212 "{@type=korap:span, layer=cat, key=CP, match=match:eq}," +
213 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
214 "{@type=korap:span, layer=cat, key=VP, match=match:eq}" +
215 "]}" +
216 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000217 "]}," +
Joachim Bingelfc428812014-06-18 14:50:14 +0000218 "{@type=korap:group, operation=operation:class, class=1, operands=[" +
219 "{@type=korap:span, layer=cat, key=NP, match=match:eq}" +
220 "]}" +
221 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000222 "]}," +
Joachim Bingel949e3a82014-06-16 13:20:43 +0000223 "{@type=korap:span, layer=cat, key=DP, match=match:eq}" +
224 "], relation={@type=korap:treeRelation, reltype=dominance}" +
225 "}";
226 aqlt = new AqlTree(query);
227 map = aqlt.getRequestMap().get("query").toString();
228 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelca4944e2014-06-13 13:55:10 +0000229 }
230
231 @Test
Joachim Bingel13421ef2014-05-07 16:22:58 +0000232 public void testPointingRelations() throws QueryException {
233 query = "node & node & #2 ->label[coref=\"true\"] #1";
234 String dom1 =
235 "{@type=korap:group, operation=operation:relation, operands=[" +
236 "{@type=korap:span}," +
237 "{@type=korap:span}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000238 "], relation={@type=korap:relation, reltype=label, wrap=[{@type=korap:term, layer=coref, key=true, match=match:eq}]}" +
Joachim Bingel13421ef2014-05-07 16:22:58 +0000239 "}";
240 aqlt = new AqlTree(query);
241 map = aqlt.getRequestMap().get("query").toString();
242 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
243
Joachim Bingel639df9c2014-05-12 15:27:00 +0000244 query = "node & node & #2 ->label[mate/coref=\"true\"] #1";
245 String dom2 =
246 "{@type=korap:group, operation=operation:relation, operands=[" +
247 "{@type=korap:span}," +
248 "{@type=korap:span}" +
249 "], relation={@type=korap:relation, reltype=label, wrap=[{@type=korap:term, foundry=mate, layer=coref, key=true, match=match:eq}]}" +
250 "}";
251 aqlt = new AqlTree(query);
252 map = aqlt.getRequestMap().get("query").toString();
253 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000254 }
255
256 @Test
257 public void testSequence() throws QueryException {
258 query = "node & node & #1 . #2";
Joachim Bingelca4944e2014-06-13 13:55:10 +0000259 String seq1 =
Joachim Bingel13421ef2014-05-07 16:22:58 +0000260 "{@type=korap:group, operation=operation:sequence, " +
261 "operands=[" +
262 "{@type=korap:span}," +
263 "{@type=korap:span}" +
264 "], inOrder=true" +
265 "}";
266 aqlt = new AqlTree(query);
267 map = aqlt.getRequestMap().get("query").toString();
Joachim Bingelca4944e2014-06-13 13:55:10 +0000268 assertEquals(seq1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000269
270 query = "node & node & #1 .* #2";
Joachim Bingelca4944e2014-06-13 13:55:10 +0000271 String seq2 =
Joachim Bingel75038792014-05-19 15:12:23 +0000272 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel13421ef2014-05-07 16:22:58 +0000273 "{@type=korap:span}," +
274 "{@type=korap:span}" +
275 "], distances=[" +
Joachim Bingel687e4d42014-07-30 09:34:18 +0000276 "{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=0}, min=0}" +
Joachim Bingel13421ef2014-05-07 16:22:58 +0000277 "], inOrder=true" +
278 "}";
279 aqlt = new AqlTree(query);
280 map = aqlt.getRequestMap().get("query").toString();
Joachim Bingelca4944e2014-06-13 13:55:10 +0000281 assertEquals(seq2.replaceAll(" ", ""), map.replaceAll(" ", ""));
282
283 query = "node & node & #1 .2,3 #2";
284 String seq3 =
285 "{@type=korap:group, operation=operation:sequence, operands=[" +
286 "{@type=korap:span}," +
287 "{@type=korap:span}" +
288 "], distances=[" +
Joachim Bingel55020e82014-07-28 14:22:51 +0000289 "{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=2, max=3}, min=2, max=3}" +
Joachim Bingelca4944e2014-06-13 13:55:10 +0000290 "], inOrder=true" +
291 "}";
292 aqlt = new AqlTree(query);
293 map = aqlt.getRequestMap().get("query").toString();
294 assertEquals(seq3.replaceAll(" ", ""), map.replaceAll(" ", ""));
295
296 }
297
298 @Test
299 public void testMultipleSequence() throws QueryException {
300 query = "tok=\"Sonne\" & tok=\"Mond\" & tok=\"Sterne\" & #1 .0,2 #2 .0,4 #3";
301 String seq4 =
302 "{@type=korap:group, operation=operation:sequence," +
Joachim Bingelfc428812014-06-18 14:50:14 +0000303 "operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000304 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelca4944e2014-06-13 13:55:10 +0000305 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel949e3a82014-06-16 13:20:43 +0000306 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sonne, match=match:eq}}," +
Joachim Bingelfc428812014-06-18 14:50:14 +0000307 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
308 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Mond, match=match:eq}}" +
309 "]}" +
Joachim Bingel949e3a82014-06-16 13:20:43 +0000310 "], distances=[" +
Joachim Bingel55020e82014-07-28 14:22:51 +0000311 "{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=0, max=2}, min=0, max=2}" +
Joachim Bingelfc428812014-06-18 14:50:14 +0000312 "], inOrder=true}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000313 "]}," +
Joachim Bingelfc428812014-06-18 14:50:14 +0000314 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sterne, match=match:eq}}" +
315 "],distances=[" +
Joachim Bingel55020e82014-07-28 14:22:51 +0000316 "{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=0, max=4}, min=0, max=4}" +
Joachim Bingelfc428812014-06-18 14:50:14 +0000317 "], inOrder=true" +
318 "}";
Joachim Bingelca4944e2014-06-13 13:55:10 +0000319 aqlt = new AqlTree(query);
320 map = aqlt.getRequestMap().get("query").toString();
321 assertEquals(seq4.replaceAll(" ", ""), map.replaceAll(" ", ""));
322
323 query = "node & node & node & #1 . #2 .1,3 #3";
324 String seq5 =
325 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000326 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelfc428812014-06-18 14:50:14 +0000327 "{@type=korap:group, operation=operation:sequence, operands=[" +
328 "{@type=korap:span}," +
329 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
330 "{@type=korap:span}" +
331 "]} "+
332 "], inOrder=true}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000333 "]}," +
Joachim Bingel949e3a82014-06-16 13:20:43 +0000334 "{@type=korap:span}" +
335 "], distances=[" +
Joachim Bingel55020e82014-07-28 14:22:51 +0000336 "{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=1, max=3}, min=1, max=3}" +
Joachim Bingel949e3a82014-06-16 13:20:43 +0000337 "], inOrder=true" +
Joachim Bingelca4944e2014-06-13 13:55:10 +0000338 "}";
339 aqlt = new AqlTree(query);
340 map = aqlt.getRequestMap().get("query").toString();
341 assertEquals(seq5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000342
343 query = "tok=\"Sonne\" & tok=\"Mond\" & tok=\"Sterne\" & tok=\"Himmel\" & #1 .0,2 #2 .0,4 #3 . #4";
344 String seq6 =
345 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000346 "{@type=korap:reference, operation=operation:focus, classRef=[1], operands=[" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000347 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000348 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000349 "{@type=korap:group, operation=operation:sequence, operands=[" +
350 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sonne, match=match:eq}}," +
351 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
352 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Mond, match=match:eq}}" +
353 "]}" +
354 "], distances=[" +
Joachim Bingel55020e82014-07-28 14:22:51 +0000355 "{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=0, max=2}, min=0, max=2}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000356 "], inOrder=true}" +
357 "]}," +
358 "{@type=korap:group, operation=operation:class, class=1, operands=[" +
359 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sterne, match=match:eq}}" +
360 "]}" +
361 "],distances=[" +
Joachim Bingel55020e82014-07-28 14:22:51 +0000362 "{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=0, max=4}, min=0, max=4}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000363 "], inOrder=true}" +
364 "]}," +
365 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Himmel, match=match:eq}}" +
366 "], inOrder=true}" ;
367 aqlt = new AqlTree(query);
368 map = aqlt.getRequestMap().get("query").toString();
369 assertEquals(seq6.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel75038792014-05-19 15:12:23 +0000370 }
371
372 @Test
Joachim Bingel949e3a82014-06-16 13:20:43 +0000373 public void testMultipleMixedOperators() throws QueryException {
374 query = "tok=\"Sonne\" & tok=\"Mond\" & tok=\"Sterne\" & #1 > #2 .0,4 #3";
375 String seq4 =
Joachim Bingelfc428812014-06-18 14:50:14 +0000376 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000377 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelfc428812014-06-18 14:50:14 +0000378 "{@type=korap:group, operation=operation:relation, operands=[" +
379 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sonne, match=match:eq}}," +
380 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
381 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Mond, match=match:eq}}" +
382 "]}" +
383 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000384 "]}," +
Joachim Bingel949e3a82014-06-16 13:20:43 +0000385 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sterne, match=match:eq}}" +
386 "], distances=[" +
Joachim Bingel55020e82014-07-28 14:22:51 +0000387 "{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=0, max=4}, min=0, max=4}" +
Joachim Bingel949e3a82014-06-16 13:20:43 +0000388 "], inOrder=true" +
389 "}";
390 aqlt = new AqlTree(query);
391 map = aqlt.getRequestMap().get("query").toString();
392 assertEquals(seq4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelfc428812014-06-18 14:50:14 +0000393
394 query = "tok=\"Sonne\" & tok=\"Mond\" & #1 > #2 .0,4 tok=\"Sterne\"";
395 String seq5 =
396 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000397 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelfc428812014-06-18 14:50:14 +0000398 "{@type=korap:group, operation=operation:relation, operands=[" +
399 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sonne, match=match:eq}}," +
400 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
401 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Mond, match=match:eq}}" +
402 "]}" +
403 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000404 "]}," +
Joachim Bingelfc428812014-06-18 14:50:14 +0000405 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sterne, match=match:eq}}" +
406 "], distances=[" +
Joachim Bingel55020e82014-07-28 14:22:51 +0000407 "{@type=korap:distance, key=w, boundary={@type=korap:boundary, min=0, max=4}, min=0, max=4}" +
Joachim Bingelfc428812014-06-18 14:50:14 +0000408 "], inOrder=true" +
409 "}";
410 aqlt = new AqlTree(query);
411 map = aqlt.getRequestMap().get("query").toString();
412 assertEquals(seq5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000413
414 query = "cat=\"NP\" & cat=\"VP\" & cat=\"PP\" & #1 $ #2 > #3";
415 String cp2 =
416 "{@type=korap:group, operation=operation:relation, operands=[" +
417 "{@type=korap:reference, operation=operation:focus, classRef=[1], operands=[" +
418 "{@type=korap:group, operation=operation:relation, operands=[" +
419 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
420 "{@type=korap:group, operation=operation:relation, operands=[" +
421 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
422 "{@type=korap:span}" +
423 "]}," +
424 "{@type=korap:span, layer=cat, key=NP, match=match:eq}" +
425 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
426 "]}," +
427 "{@type=korap:group, operation=operation:class, class=1, operands=[" +
428 "{@type=korap:span, layer=cat, key=VP, match=match:eq}" +
429 "]}" +
430 "], relation={@type=korap:treeRelation, reltype=dominance}" +
431 "}" +
432 "]}," +
433 "{@type=korap:span, layer=cat, key=PP, match=match:eq}" +
434 "], relation={@type=korap:treeRelation, reltype=dominance}" +
435 "}";
436 aqlt = new AqlTree(query);
437 map = aqlt.getRequestMap().get("query").toString();
438 assertEquals(cp2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel949e3a82014-06-16 13:20:43 +0000439 }
440
441 @Test
Joachim Bingel75038792014-05-19 15:12:23 +0000442 public void testPositions() throws QueryException {
443 query = "node & node & #2 _=_ #1";
444 String pos1 =
445 "{@type=korap:group, operation=operation:position, operands=[" +
446 "{@type=korap:span}," +
447 "{@type=korap:span}" +
448 "], frame=frame:matches" +
449 "}";
450 aqlt = new AqlTree(query);
451 map = aqlt.getRequestMap().get("query").toString();
452 assertEquals(pos1.replaceAll(" ", ""), map.replaceAll(" ", ""));
453
Joachim Bingelca4944e2014-06-13 13:55:10 +0000454 query = "node & node & #2 _i_ #1";
455 String pos2 =
456 "{@type=korap:group, operation=operation:position, operands=[" +
457 "{@type=korap:span}," +
458 "{@type=korap:span}" +
459 "], frame=frame:contains" +
460 "}";
461 aqlt = new AqlTree(query);
462 map = aqlt.getRequestMap().get("query").toString();
463 assertEquals(pos2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel75038792014-05-19 15:12:23 +0000464
Joachim Bingelca4944e2014-06-13 13:55:10 +0000465 query = "node & node & #2 _l_ #1";
466 String pos3 =
467 "{@type=korap:group, operation=operation:position, operands=[" +
468 "{@type=korap:span}," +
469 "{@type=korap:span}" +
470 "], inOrder=false, frame=frame:startswith" +
471 "}";
472 aqlt = new AqlTree(query);
473 map = aqlt.getRequestMap().get("query").toString();
474 assertEquals(pos3.replaceAll(" ", ""), map.replaceAll(" ", ""));
475
476 query = "node & \"Mann\" & #1 _r_ #2";
Joachim Bingel75038792014-05-19 15:12:23 +0000477 String pos4 =
Joachim Bingel75038792014-05-19 15:12:23 +0000478 "{@type=korap:group, operation=operation:position, operands=[" +
479 "{@type=korap:span}," +
Joachim Bingelca4944e2014-06-13 13:55:10 +0000480 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
481 "], inOrder=false, frame=frame:endswith" +
482 "}";
Joachim Bingel75038792014-05-19 15:12:23 +0000483 aqlt = new AqlTree(query);
484 map = aqlt.getRequestMap().get("query").toString();
485 assertEquals(pos4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000486
Joachim Bingelca4944e2014-06-13 13:55:10 +0000487 query = "node & \"Mann\" & #2 _r_ #1";
488 String pos5 =
489 "{@type=korap:group, operation=operation:position, operands=[" +
490 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}," +
491 "{@type=korap:span}" +
492 "], inOrder=false, frame=frame:endswith" +
493 "}";
494 aqlt = new AqlTree(query);
495 map = aqlt.getRequestMap().get("query").toString();
496 assertEquals(pos5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000497 }
498
Joachim Bingelca4944e2014-06-13 13:55:10 +0000499 @Test
500 public void testMultiplePredications() throws QueryException {
Joachim Bingel9c3ddb92014-06-23 13:49:58 +0000501 // a noun before a verb before a preposition
Joachim Bingelfc428812014-06-18 14:50:14 +0000502 query = "pos=\"N\" & pos=\"V\" & pos=\"P\" & #1 . #2 & #2 . #3";
Joachim Bingelca4944e2014-06-13 13:55:10 +0000503 String mult1 =
Joachim Bingel9c3ddb92014-06-23 13:49:58 +0000504 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000505 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelca4944e2014-06-13 13:55:10 +0000506 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel9c3ddb92014-06-23 13:49:58 +0000507 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=N, match=match:eq}}," +
508 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
509 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=V, match=match:eq}}" +
510 "]}" +
511 "], inOrder=true}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000512 "]}," +
Joachim Bingel9c3ddb92014-06-23 13:49:58 +0000513 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=P, match=match:eq}}" +
514 "], inOrder=true}";
Joachim Bingelca4944e2014-06-13 13:55:10 +0000515 aqlt = new AqlTree(query);
516 map = aqlt.getRequestMap().get("query").toString();
517 assertEquals(mult1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel9c3ddb92014-06-23 13:49:58 +0000518
519 // a noun before a verb before a preposition
520 query = "pos=\"N\" & pos=\"V\" & #1 . #2 & #2 . pos=\"P\"";
521 String mult2 =
522 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000523 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingel9c3ddb92014-06-23 13:49:58 +0000524 "{@type=korap:group, operation=operation:sequence, operands=[" +
525 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=N, match=match:eq}}," +
526 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
527 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=V, match=match:eq}}" +
528 "]}" +
529 "], inOrder=true}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000530 "]}," +
Joachim Bingel9c3ddb92014-06-23 13:49:58 +0000531 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=P, match=match:eq}}" +
532 "], inOrder=true}";
533 aqlt = new AqlTree(query);
534 map = aqlt.getRequestMap().get("query").toString();
535 assertEquals(mult2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000536
537 query = "pos=\"N\" & pos=\"V\" & pos=\"P\" & #1 > #2 & #1 > #3";
538 String mult3 =
539 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000540 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000541 "{@type=korap:group, operation=operation:relation, operands=[" +
542 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
543 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=N, match=match:eq}}" +
544 "]}," +
545 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=V, match=match:eq}}" +
546 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
547 "]}," +
548 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=P, match=match:eq}}" +
549 "], relation={@type=korap:treeRelation, reltype=dominance}}";
550 aqlt = new AqlTree(query);
551 map = aqlt.getRequestMap().get("query").toString();
552 assertEquals(mult3.replaceAll(" ", ""), map.replaceAll(" ", ""));
553
554 query = "cat=\"NP\" & pos=\"V\" & pos=\"P\" & #1 > #2 & #1 > #3 & #2 . #3";
555 String mult4 =
556 "{@type=korap:group, operation=operation:sequence, operands=[" +
557 // reduce dominance relations "#1 > #2 & #1 > #3" to operand #2 in order to make it accessible for #2 . #3 (the last/outermost relation)
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000558 "{@type=korap:reference, operation=operation:focus, classRef=[1], operands=[" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000559 "{@type=korap:group, operation=operation:relation, operands=[" +
560 // dominance relation #1 > #2 is reduced to #1, for expressing #1 > #3
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000561 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000562 "{@type=korap:group, operation=operation:relation, operands=[" +
563 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
564 "{@type=korap:span, layer=cat, key=NP, match=match:eq}" +
565 "]}," +
566 "{@type=korap:group, operation=operation:class, class=1, operands=[" +
567 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=V, match=match:eq}}" +
568 "]}" +
569 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
570 "]}," +
571 // establish class 2 around P for later reference
572 "{@type=korap:group, operation=operation:class, class=2, operands=[" +
573 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=P, match=match:eq}}" +
574 "]}" +
575 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
576 "]}," +
577 // refer back to class 2 as second operand
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000578 "{@type=korap:reference, operation=operation:focus, classRef=[2]}" +
Joachim Bingelaee38ae2014-06-25 09:32:56 +0000579 "], inOrder=true}";
580 aqlt = new AqlTree(query);
581 map = aqlt.getRequestMap().get("query").toString();
582 assertEquals(mult4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelca4944e2014-06-13 13:55:10 +0000583 }
Joachim Bingeleee549e2014-04-29 11:15:37 +0000584
Joachim Bingelca4944e2014-06-13 13:55:10 +0000585 @Test
586 public void testUnaryRelations() throws QueryException {
587 query = "node & #1:tokenarity=2";
588 String unary1 =
589 "{@type=korap:span, attr={@type=korap:term, tokenarity={@type=korap:boundary,min=2,max=2}}}";
590 aqlt = new AqlTree(query);
591 map = aqlt.getRequestMap().get("query").toString();
592 assertEquals(unary1.replaceAll(" ", ""), map.replaceAll(" ", ""));
593
594 query = "cnx/cat=\"NP\" & #1:tokenarity=2";
595 String unary2 =
596 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq, attr={@type=korap:term, tokenarity={@type=korap:boundary,min=2,max=2}}}";
597 aqlt = new AqlTree(query);
598 map = aqlt.getRequestMap().get("query").toString();
599 assertEquals(unary2.replaceAll(" ", ""), map.replaceAll(" ", ""));
600
601 query = "cnx/cat=\"NP\" & #1:root";
602 String unary3 =
603 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq, attr={@type=korap:term, root=true}}";
604 aqlt = new AqlTree(query);
605 map = aqlt.getRequestMap().get("query").toString();
606 assertEquals(unary3.replaceAll(" ", ""), map.replaceAll(" ", ""));
607
608 query = "cnx/cat=\"NP\" & node & #1>#2 & #1:tokenarity=2";
609 String unary4 =
610 "{@type=korap:group, operation=operation:relation, operands=[" +
611 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq, attr={@type=korap:term, tokenarity={@type=korap:boundary,min=2,max=2}}}," +
612 "{@type=korap:span}" +
613 "], relation={@type=korap:treeRelation, reltype=dominance}" +
614 "}";
615 aqlt = new AqlTree(query);
616 map = aqlt.getRequestMap().get("query").toString();
617 assertEquals(unary4.replaceAll(" ", ""), map.replaceAll(" ", ""));
618 }
619
Joachim Bingel1846c8c2014-07-08 14:13:31 +0000620 @Test
621 public void testCommonParent() throws QueryException {
622 query = "cat=\"NP\" & cat=\"VP\" & #1 $ #2";
623 String cp1 =
624 "{@type=korap:group, operation=operation:relation, operands=[" +
625 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
626 "{@type=korap:group, operation=operation:relation, operands=[" +
627 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
628 "{@type=korap:span}" +
629 "]}," +
630 "{@type=korap:span, layer=cat, key=NP, match=match:eq}" +
631 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
632 "]}," +
633 "{@type=korap:span, layer=cat, key=VP, match=match:eq}" +
634 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
635 "";
636 aqlt = new AqlTree(query);
637 map = aqlt.getRequestMap().get("query").toString();
638 assertEquals(cp1.replaceAll(" ", ""), map.replaceAll(" ", ""));
639
640 query = "cat=\"NP\" & cat=\"VP\" & cat=\"PP\" & #1 $ #2 $ #3";
641 String cp2 =
642 "{@type=korap:group, operation=operation:relation, operands=[" +
643 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
644 "{@type=korap:group, operation=operation:relation, operands=[" +
645 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
646 "{@type=korap:group, operation=operation:relation, operands=[" +
647 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
648 "{@type=korap:span}" +
649 "]}," +
650 "{@type=korap:span, layer=cat, key=NP, match=match:eq}" +
651 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
652 "]}," +
653 "{@type=korap:span, layer=cat, key=VP, match=match:eq}" +
654 "], relation={@type=korap:treeRelation, reltype=dominance}" +
655 "}" +
656 "]}," +
657 "{@type=korap:span, layer=cat, key=PP, match=match:eq}" +
658 "], relation={@type=korap:treeRelation, reltype=dominance}" +
659 "}";
660 aqlt = new AqlTree(query);
661 map = aqlt.getRequestMap().get("query").toString();
662 assertEquals(cp2.replaceAll(" ", ""), map.replaceAll(" ", ""));
663
664 query = "cat=\"NP\" & cat=\"VP\" & cat=\"PP\" & cat=\"CP\" & #1 $ #2 $ #3 $ #4";
665 String cp3 =
666 "{@type=korap:group, operation=operation:relation, operands=[" +
667 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
668 "{@type=korap:group, operation=operation:relation, operands=[" +
669 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
670 "{@type=korap:group, operation=operation:relation, operands=[" +
671 "{@type=korap:reference, operation=operation:focus, classRef=[0], operands=[" +
672 "{@type=korap:group, operation=operation:relation, operands=[" +
673 "{@type=korap:group, operation=operation:class, class=0, operands=[" +
674 "{@type=korap:span}" +
675 "]}," +
676 "{@type=korap:span, layer=cat, key=NP, match=match:eq}" +
677 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
678 "]}," +
679 "{@type=korap:span, layer=cat, key=VP, match=match:eq}" +
680 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
681 "]}," +
682 "{@type=korap:span, layer=cat, key=PP, match=match:eq}" +
683 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
684 "]}," +
685 "{@type=korap:span, layer=cat, key=CP, match=match:eq}" +
686 "], relation={@type=korap:treeRelation, reltype=dominance}" +
687 "}" +
688 "";
689 aqlt = new AqlTree(query);
690 map = aqlt.getRequestMap().get("query").toString();
691 assertEquals(cp3.replaceAll(" ", ""), map.replaceAll(" ", ""));
692 }
693
Joachim Bingelca4944e2014-06-13 13:55:10 +0000694}