blob: 4ad1a3502a0475471f8f5c4ca7cbc07490c44c99 [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(" ", ""));
63 }
64
65 @Test
66 public void testLayers() throws QueryException {
67 query = "cnx/cat=\"NP\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000068 String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000069 aqlt = new AqlTree(query);
70 map = aqlt.getRequestMap().get("query").toString();
71 assertEquals(layers1.replaceAll(" ", ""), map.replaceAll(" ", ""));
72
73 query = "treetagger/pos=\"NN\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000074 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 +000075 aqlt = new AqlTree(query);
76 map = aqlt.getRequestMap().get("query").toString();
77 assertEquals(layers2.replaceAll(" ", ""), map.replaceAll(" ", ""));
78 }
79
80 @Test
Joachim Bingel75038792014-05-19 15:12:23 +000081 public void testDirectDeclarationRelations() throws QueryException {
82 query = "node > node";
83 String ddr1 =
84 "{@type=korap:group, operation=operation:relation, operands=[" +
85 "{@type=korap:span}," +
86 "{@type=korap:span}" +
87 "], relation={@type=korap:treeRelation, reltype=dominance}" +
88 "}";
89 aqlt = new AqlTree(query);
90 map = aqlt.getRequestMap().get("query").toString();
91 assertEquals(ddr1.replaceAll(" ", ""), map.replaceAll(" ", ""));
92
93 query = "node > cnx/cat=\"NP\"";
94 String ddr2 =
95 "{@type=korap:group, operation=operation:relation, operands=[" +
96 "{@type=korap:span}," +
97 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}" +
98 "], relation={@type=korap:treeRelation, reltype=dominance}" +
99 "}";
100 aqlt = new AqlTree(query);
101 map = aqlt.getRequestMap().get("query").toString();
102 assertEquals(ddr2.replaceAll(" ", ""), map.replaceAll(" ", ""));
103
104 }
105
106 @Test
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000107 public void testSimpleDominance() throws QueryException {
108 query = "node & node & #2 > #1";
109 String dom1 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000110 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000111 "{@type=korap:span}," +
112 "{@type=korap:span}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000113 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000114 "}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000115 aqlt = new AqlTree(query);
116 map = aqlt.getRequestMap().get("query").toString();
117 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeleee549e2014-04-29 11:15:37 +0000118
119 query = "\"Mann\" & node & #2 > #1";
120 String dom2 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000121 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000122 "{@type=korap:span}," +
123 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000124 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000125 "}";
126 aqlt = new AqlTree(query);
127 map = aqlt.getRequestMap().get("query").toString();
128 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
129
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000130 query = "\"Mann\" & node & #2 >[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000131 String dom3 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000132 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000133 "{@type=korap:span}," +
134 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000135 "], relation={@type=korap:treeRelation, reltype=dominance, wrap=[{@type=korap:term, layer=cat, key=NP, match=match:eq}]}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000136 "}";
137 aqlt = new AqlTree(query);
138 map = aqlt.getRequestMap().get("query").toString();
139 assertEquals(dom3.replaceAll(" ", ""), map.replaceAll(" ", ""));
140
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000141 query = "\"Mann\" & node & #2 >@l[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000142 String dom4 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000143 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000144 "{@type=korap:span}," +
145 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000146 "], 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 +0000147 "}";
148 aqlt = new AqlTree(query);
149 map = aqlt.getRequestMap().get("query").toString();
150 assertEquals(dom4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000151
152 query = "\"Mann\" & node & #2 >2,4 #1";
153 String dom5 =
154 "{@type=korap:group, operation=operation:relation, operands=[" +
155 "{@type=korap:span}," +
156 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
157 "], relation={@type=korap:treeRelation, reltype=dominance, " +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000158 "distance={@type=korap:distance, key=r, min=2, max=4}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000159 "}";
160 aqlt = new AqlTree(query);
161 map = aqlt.getRequestMap().get("query").toString();
162 assertEquals(dom5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000163 }
164
Joachim Bingel13421ef2014-05-07 16:22:58 +0000165 @Test
Joachim Bingelca4944e2014-06-13 13:55:10 +0000166 public void testMultipleDominance() throws QueryException {
167 query = "cat=\"NP\" & cat=\"VP\" & cat=\"NP\" & #1 > #2 > #3";
168 String dom1 =
169 "{@type=korap:group, operation=operation:relation, operands=[" +
170 "{@type=korap:span, layer=cat, key=NP, match=match:eq}," +
171 "{@type=korap:group, operation=operation:relation, operands=[" +
172 "{@type=korap:span, layer=cat, key=VP, match=match:eq}," +
173 "{@type=korap:span, layer=cat, key=NP, match=match:eq}" +
174 "], relation={@type=korap:treeRelation, reltype=dominance}}" +
175 "], relation={@type=korap:treeRelation, reltype=dominance}" +
176 "}";
177 aqlt = new AqlTree(query);
178 map = aqlt.getRequestMap().get("query").toString();
179 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
180 }
181
182 @Test
Joachim Bingel13421ef2014-05-07 16:22:58 +0000183 public void testPointingRelations() throws QueryException {
184 query = "node & node & #2 ->label[coref=\"true\"] #1";
185 String dom1 =
186 "{@type=korap:group, operation=operation:relation, operands=[" +
187 "{@type=korap:span}," +
188 "{@type=korap:span}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000189 "], relation={@type=korap:relation, reltype=label, wrap=[{@type=korap:term, layer=coref, key=true, match=match:eq}]}" +
Joachim Bingel13421ef2014-05-07 16:22:58 +0000190 "}";
191 aqlt = new AqlTree(query);
192 map = aqlt.getRequestMap().get("query").toString();
193 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
194
Joachim Bingel639df9c2014-05-12 15:27:00 +0000195 query = "node & node & #2 ->label[mate/coref=\"true\"] #1";
196 String dom2 =
197 "{@type=korap:group, operation=operation:relation, operands=[" +
198 "{@type=korap:span}," +
199 "{@type=korap:span}" +
200 "], relation={@type=korap:relation, reltype=label, wrap=[{@type=korap:term, foundry=mate, layer=coref, key=true, match=match:eq}]}" +
201 "}";
202 aqlt = new AqlTree(query);
203 map = aqlt.getRequestMap().get("query").toString();
204 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000205 }
206
207 @Test
208 public void testSequence() throws QueryException {
209 query = "node & node & #1 . #2";
Joachim Bingelca4944e2014-06-13 13:55:10 +0000210 String seq1 =
Joachim Bingel13421ef2014-05-07 16:22:58 +0000211 "{@type=korap:group, operation=operation:sequence, " +
212 "operands=[" +
213 "{@type=korap:span}," +
214 "{@type=korap:span}" +
215 "], inOrder=true" +
216 "}";
217 aqlt = new AqlTree(query);
218 map = aqlt.getRequestMap().get("query").toString();
Joachim Bingelca4944e2014-06-13 13:55:10 +0000219 assertEquals(seq1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000220
221 query = "node & node & #1 .* #2";
Joachim Bingelca4944e2014-06-13 13:55:10 +0000222 String seq2 =
Joachim Bingel75038792014-05-19 15:12:23 +0000223 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel13421ef2014-05-07 16:22:58 +0000224 "{@type=korap:span}," +
225 "{@type=korap:span}" +
226 "], distances=[" +
227 "{@type=korap:distance, key=w, min=0, max=100}" +
228 "], inOrder=true" +
229 "}";
230 aqlt = new AqlTree(query);
231 map = aqlt.getRequestMap().get("query").toString();
Joachim Bingelca4944e2014-06-13 13:55:10 +0000232 assertEquals(seq2.replaceAll(" ", ""), map.replaceAll(" ", ""));
233
234 query = "node & node & #1 .2,3 #2";
235 String seq3 =
236 "{@type=korap:group, operation=operation:sequence, operands=[" +
237 "{@type=korap:span}," +
238 "{@type=korap:span}" +
239 "], distances=[" +
240 "{@type=korap:distance, key=w, min=2, max=3}" +
241 "], inOrder=true" +
242 "}";
243 aqlt = new AqlTree(query);
244 map = aqlt.getRequestMap().get("query").toString();
245 assertEquals(seq3.replaceAll(" ", ""), map.replaceAll(" ", ""));
246
247 }
248
249 @Test
250 public void testMultipleSequence() throws QueryException {
251 query = "tok=\"Sonne\" & tok=\"Mond\" & tok=\"Sterne\" & #1 .0,2 #2 .0,4 #3";
252 String seq4 =
253 "{@type=korap:group, operation=operation:sequence," +
254 "operands=[" +
255 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sonne, match=match:eq}}," +
256 "{@type=korap:group, operation=operation:sequence, operands=[" +
257 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Mond, match=match:eq}}," +
258 "{@type=korap:token, wrap={@type=korap:term, layer=orth, key=Sterne, match=match:eq}}" +
259 "], distances=[" +
260 "{@type=korap:distance, key=w, min=0, max=4}" +
261 "], inOrder=true}" +
262 "],distances=[" +
263 "{@type=korap:distance, key=w, min=0, max=2}" +
264 "], inOrder=true" +
265 "}";
266 aqlt = new AqlTree(query);
267 map = aqlt.getRequestMap().get("query").toString();
268 assertEquals(seq4.replaceAll(" ", ""), map.replaceAll(" ", ""));
269
270 query = "node & node & node & #1 . #2 .1,3 #3";
271 String seq5 =
272 "{@type=korap:group, operation=operation:sequence, operands=[" +
273 "{@type=korap:span}," +
274 "{@type=korap:group, operation=operation:sequence, operands=[" +
275 "{@type=korap:span}," +
276 "{@type=korap:span}" +
277 "], distances=[" +
278 "{@type=korap:distance, key=w, min=1, max=3}" +
279 "], inOrder=true}" +
280 "], inOrder=true" +
281 "}";
282 aqlt = new AqlTree(query);
283 map = aqlt.getRequestMap().get("query").toString();
284 assertEquals(seq5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel75038792014-05-19 15:12:23 +0000285 }
286
287 @Test
288 public void testPositions() throws QueryException {
289 query = "node & node & #2 _=_ #1";
290 String pos1 =
291 "{@type=korap:group, operation=operation:position, operands=[" +
292 "{@type=korap:span}," +
293 "{@type=korap:span}" +
294 "], frame=frame:matches" +
295 "}";
296 aqlt = new AqlTree(query);
297 map = aqlt.getRequestMap().get("query").toString();
298 assertEquals(pos1.replaceAll(" ", ""), map.replaceAll(" ", ""));
299
Joachim Bingelca4944e2014-06-13 13:55:10 +0000300 query = "node & node & #2 _i_ #1";
301 String pos2 =
302 "{@type=korap:group, operation=operation:position, operands=[" +
303 "{@type=korap:span}," +
304 "{@type=korap:span}" +
305 "], frame=frame:contains" +
306 "}";
307 aqlt = new AqlTree(query);
308 map = aqlt.getRequestMap().get("query").toString();
309 assertEquals(pos2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel75038792014-05-19 15:12:23 +0000310
Joachim Bingelca4944e2014-06-13 13:55:10 +0000311 query = "node & node & #2 _l_ #1";
312 String pos3 =
313 "{@type=korap:group, operation=operation:position, operands=[" +
314 "{@type=korap:span}," +
315 "{@type=korap:span}" +
316 "], inOrder=false, frame=frame:startswith" +
317 "}";
318 aqlt = new AqlTree(query);
319 map = aqlt.getRequestMap().get("query").toString();
320 assertEquals(pos3.replaceAll(" ", ""), map.replaceAll(" ", ""));
321
322 query = "node & \"Mann\" & #1 _r_ #2";
Joachim Bingel75038792014-05-19 15:12:23 +0000323 String pos4 =
Joachim Bingel75038792014-05-19 15:12:23 +0000324 "{@type=korap:group, operation=operation:position, operands=[" +
325 "{@type=korap:span}," +
Joachim Bingelca4944e2014-06-13 13:55:10 +0000326 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
327 "], inOrder=false, frame=frame:endswith" +
328 "}";
Joachim Bingel75038792014-05-19 15:12:23 +0000329 aqlt = new AqlTree(query);
330 map = aqlt.getRequestMap().get("query").toString();
331 assertEquals(pos4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000332
Joachim Bingelca4944e2014-06-13 13:55:10 +0000333 query = "node & \"Mann\" & #2 _r_ #1";
334 String pos5 =
335 "{@type=korap:group, operation=operation:position, operands=[" +
336 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}," +
337 "{@type=korap:span}" +
338 "], inOrder=false, frame=frame:endswith" +
339 "}";
340 aqlt = new AqlTree(query);
341 map = aqlt.getRequestMap().get("query").toString();
342 assertEquals(pos5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000343 }
344
Joachim Bingelca4944e2014-06-13 13:55:10 +0000345 @Test
346 public void testMultiplePredications() throws QueryException {
347 // a noun before a verb before a noun
348 // XXX Warning incorrect serialisation! Need to ensure that the two Vs are identical!
349 // Embed in one sequence!
350 query = "pos=\"N\" & pos=\"V\" & pos=\"N\" & #1 . #2 & #2 . #3";
351 String mult1 =
352 "{@type=korap:group, operation=operation:sequence, operands=[" +
353 "{@type=korap:group, operation=operation:sequence, operands=[" +
354 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=N, match=match:eq}}," +
355 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=V, match=match:eq}}" +
356 "], inOrder=true}," +
357 "{@type=korap:group, operation=operation:sequence, operands=[" +
358 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=V, match=match:eq}}," +
359 "{@type=korap:token, wrap={@type=korap:term, layer=pos, key=N, match=match:eq}}" +
360 "], inOrder=true}" +
361 "], distances=[" +
362 "{@type=korap:distance, key=t, min=0, max=0}" +
363 "]}";
364 aqlt = new AqlTree(query);
365 map = aqlt.getRequestMap().get("query").toString();
366 assertEquals(mult1.replaceAll(" ", ""), map.replaceAll(" ", ""));
367 }
Joachim Bingeleee549e2014-04-29 11:15:37 +0000368
Joachim Bingelca4944e2014-06-13 13:55:10 +0000369 @Test
370 public void testUnaryRelations() throws QueryException {
371 query = "node & #1:tokenarity=2";
372 String unary1 =
373 "{@type=korap:span, attr={@type=korap:term, tokenarity={@type=korap:boundary,min=2,max=2}}}";
374 aqlt = new AqlTree(query);
375 map = aqlt.getRequestMap().get("query").toString();
376 assertEquals(unary1.replaceAll(" ", ""), map.replaceAll(" ", ""));
377
378 query = "cnx/cat=\"NP\" & #1:tokenarity=2";
379 String unary2 =
380 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq, attr={@type=korap:term, tokenarity={@type=korap:boundary,min=2,max=2}}}";
381 aqlt = new AqlTree(query);
382 map = aqlt.getRequestMap().get("query").toString();
383 assertEquals(unary2.replaceAll(" ", ""), map.replaceAll(" ", ""));
384
385 query = "cnx/cat=\"NP\" & #1:root";
386 String unary3 =
387 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq, attr={@type=korap:term, root=true}}";
388 aqlt = new AqlTree(query);
389 map = aqlt.getRequestMap().get("query").toString();
390 assertEquals(unary3.replaceAll(" ", ""), map.replaceAll(" ", ""));
391
392 query = "cnx/cat=\"NP\" & node & #1>#2 & #1:tokenarity=2";
393 String unary4 =
394 "{@type=korap:group, operation=operation:relation, operands=[" +
395 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq, attr={@type=korap:term, tokenarity={@type=korap:boundary,min=2,max=2}}}," +
396 "{@type=korap:span}" +
397 "], relation={@type=korap:treeRelation, reltype=dominance}" +
398 "}";
399 aqlt = new AqlTree(query);
400 map = aqlt.getRequestMap().get("query").toString();
401 assertEquals(unary4.replaceAll(" ", ""), map.replaceAll(" ", ""));
402 }
403
404 // TODO commonparent, commonancestor
405
406}