blob: de9e493402e5674e824db040ea7cfade9494a718 [file] [log] [blame]
Joachim Bingel53333e62013-12-09 19:25:52 +00001import static org.junit.Assert.*;
2
3import org.junit.Test;
4
5import de.ids_mannheim.korap.query.serialize.PoliqarpPlusTree;
6
7public class PoliqarpPlusTreeTest {
8
9 PoliqarpPlusTree ppt;
10 String map;
11
12 private boolean equalsContent(String str, Object map) {
13 str = str.replaceAll(" ", "");
14 String mapStr = map.toString().replaceAll(" ", "");
15 return str.equals(mapStr);
16 }
17
18 private boolean equalsQueryContent(String res, String query) {
19 res = res.replaceAll(" ", "");
20 ppt = new PoliqarpPlusTree(query);
21 String queryMap = ppt.getRequestMap().get("query").toString().replaceAll(" ", "");
22 return res.equals(queryMap);
23 }
24
25 @Test
26 public void testContext() {
27 String contextString = "{korap=http://korap.ids-mannheim.de/ns/query, @language=de, operands={@id=korap:operands, @container=@list}, relation={@id=korap:relation, @type=korap:relation#types}, class={@id=korap:class, @type=xsd:integer}, query=korap:query, filter=korap:filter, meta=korap:meta}";
28 ppt = new PoliqarpPlusTree("[base=test]");
29 assertTrue(equalsContent(contextString, ppt.getRequestMap().get("@context")));
30 }
31
32 @Test
33 public void testSingleTokens() {
34 // [base=Mann]
35 String token1 = "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}";
36 assertTrue(equalsQueryContent(token1, "[base=Mann]"));
37
38 // [orth!=Frau]
39 String token2 = "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation=!=}}";
40 assertTrue(equalsQueryContent(token2, "[orth!=Frau]"));
41
42 // [!p=NN]
43 String token3 = "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation=!=}}";
44 assertTrue(equalsQueryContent(token3, "[!p=NN]"));
45
46 // [!p!=NN]
47 String token4 = "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation==}}";
48 assertTrue(equalsQueryContent(token4, "[!p!=NN]"));
49 }
50
51 @Test
52 public void testElements() {
53 // <s>
54 String elem1 = "{@type=korap:element, @value=s}";
55 assertTrue(equalsQueryContent(elem1, "<s>"));
56
57 // <vp>
58 String elem2 = "{@type=korap:element, @value=vp}";
59 assertTrue(equalsQueryContent(elem2, "<vp>"));
60 }
61
62 @Test
63 public void testCoordinatedFields() {
64 // [base=Mann&(cas=N|cas=A)]
65 String cof1 =
66 "{@type=korap:token, @value=" +
67 "{@type=korap:group, operands=[" +
68 "{@type=korap:term, @value=base:Mann, relation==}," +
69 "{@type=korap:group, operands=[" +
70 "{@type=korap:term, @value=cas:N, relation==}," +
71 "{@type=korap:term, @value=cas:A, relation==}" +
72 "], relation=or}" +
73 "], relation=and}" +
74 "}";
75 ppt = new PoliqarpPlusTree("[base=Mann&(cas=N|cas=A)]");
76 map = ppt.getRequestMap().get("query").toString();
77 assertEquals(cof1.replaceAll(" ", ""), map.replaceAll(" ", ""));
78 }
79
80 @Test
81 public void testOccurrence() {
Joachim Bingelcd9ed332013-12-09 21:01:35 +000082 // [base=foo]*
83 String occ1 = "{@type=korap:group, operands=[" +
84 "{@type=korap:token, @value={@type=korap:term, @value=base:foo, relation==}}" +
85 "], relation=repetition, quantifier=* }";
86 ppt = new PoliqarpPlusTree("[base=foo]*");
87 map = ppt.getRequestMap().get("query").toString();
88 assertEquals(occ1.replaceAll(" ", ""), map.replaceAll(" ", ""));
89
90 // [base=foo]*[base=bar]
91 String occ2 =
92 "{@type=korap:sequence, operands=[" +
93 "{@type=korap:group, operands=[" +
94 "{@type=korap:token, @value={@type=korap:term, @value=base:foo, relation==}}" +
95 "], relation=repetition, quantifier=* }," +
96 "{@type=korap:token, @value={@type=korap:term, @value=base:bar, relation==}}" +
97 "]}";
98 ppt = new PoliqarpPlusTree("[base=foo]*[base=bar]");
99 map = ppt.getRequestMap().get("query").toString();
100 assertEquals(occ2.replaceAll(" ", ""), map.replaceAll(" ", ""));
101
102 // [base=bar][base=foo]*
103 String occ3 =
104 "{@type=korap:sequence, operands=[" +
105 "{@type=korap:token, @value={@type=korap:term, @value=base:bar, relation==}}," +
106 "{@type=korap:group, operands=[" +
107 "{@type=korap:token, @value={@type=korap:term, @value=base:foo, relation==}}" +
108 "], relation=repetition, quantifier=* }" +
109 "]}";
110 ppt = new PoliqarpPlusTree("[base=bar][base=foo]*");
111 map = ppt.getRequestMap().get("query").toString();
112 assertEquals(occ3.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel53333e62013-12-09 19:25:52 +0000113 }
114
115 @Test
116 public void testTokenSequence() {
117 // [base=Mann][orth=Frau]
118 String seq1 = "{@type=korap:sequence, operands=[" +
119 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}, " +
120 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}" +
121 "]}";
122 assertTrue(equalsQueryContent(seq1, "[base=Mann][orth=Frau]"));
123
124 // [base=Mann][orth=Frau][p=NN]
125 String seq2 = "{@type=korap:sequence, operands=[" +
126 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}, " +
127 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}, " +
128 "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation==}}" +
129 "]}";
130 assertTrue(equalsQueryContent(seq2, "[base=Mann][orth=Frau][p=NN]"));
131 }
132
133 @Test
134 public void testTokenElemSequence() {
135 // [base=Mann]<vp>
136 String seq1 = "{@type=korap:sequence, operands=[" +
137 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}, " +
138 "{@type=korap:element, @value=vp}" +
139 "]}";
140 assertTrue(equalsQueryContent(seq1, "[base=Mann]<vp>"));
141
142 // <vp>[base=Mann]
143 String seq2 = "{@type=korap:sequence, operands=[" +
144 "{@type=korap:element, @value=vp}, "+
145 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}} " +
146 "]}";
147 assertTrue(equalsQueryContent(seq2, "<vp>[base=Mann]"));
148
149 // <vp>[base=Mann]<pp>
150 String seq3 = "{@type=korap:sequence, operands=[" +
151 "{@type=korap:element, @value=vp}, "+
152 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}, " +
153 "{@type=korap:element, @value=pp} "+
154 "]}";
155 assertTrue(equalsQueryContent(seq3, "<vp>[base=Mann]<pp>"));
156 }
157
158 @Test
159 public void testElemSequence() {
160 // <np><vp>
161 String seq1 = "{@type=korap:sequence, operands=[" +
162 "{@type=korap:element, @value=np}," +
163 "{@type=korap:element, @value=vp}" +
164 "]}";
165 assertTrue(equalsQueryContent(seq1, "<np><vp>"));
166
167 // <np><vp><pp>
168 String seq2 = "{@type=korap:sequence, operands=[" +
169 "{@type=korap:element, @value=np}," +
170 "{@type=korap:element, @value=vp}," +
171 "{@type=korap:element, @value=pp}" +
172 "]}";
173 assertTrue(equalsQueryContent(seq2, "<np><vp><pp>"));
174 }
175
176 @Test
177 public void testClasses() {
178 // {[base=Mann]}
179 String cls1 = "{@type=korap:group, class=0, operands=[" +
180 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}" +
181 "]}";
182 assertTrue(equalsQueryContent(cls1, "{[base=Mann]}"));
183
184 // {[base=Mann][orth=Frau]}
185 String cls2 = "{@type=korap:group, class=0, operands=[" +
186 "{@type=korap:sequence, operands=[" +
187 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}," +
188 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}" +
189 "]}" +
190 "]}";
191 assertTrue(equalsQueryContent(cls2, "{[base=Mann][orth=Frau]}"));
192
193 // [p=NN]{[base=Mann][orth=Frau]}
194 String cls3 = "{@type=korap:sequence, operands=[" +
195 "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation==}}," +
196 "{@type=korap:group, class=0, operands=[" +
197 "{@type=korap:sequence, operands=[" +
198 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}," +
199 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}" +
200 "]}" +
201 "]}" +
202 "]}";
203 assertTrue(equalsQueryContent(cls3, "[p=NN]{[base=Mann][orth=Frau]}"));
204
205 // {[base=Mann][orth=Frau]}[p=NN]
206 String cls4 = "{@type=korap:sequence, operands=[" +
207 "{@type=korap:group, class=0, operands=[" +
208 "{@type=korap:sequence, operands=[" +
209 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}," +
210 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}" +
211 "]}" +
212 "]}," +
213 "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation==}}" +
214 "]}";
215 assertTrue(equalsQueryContent(cls4, "{[base=Mann][orth=Frau]}[p=NN]"));
Joachim Bingelcd9ed332013-12-09 21:01:35 +0000216
217 // {2:{1:[tt/p=ADJA]}[mate/p=NN]}"
218 String cls5 = "{@type=korap:group, class=2, operands=[" +
219 "{@type=korap:sequence, operands=[" +
220 "{@type=korap:group, class=1, operands=[" +
221 "{@type=korap:token, @value={@type=korap:term, @value=tt/p:ADJA, relation==}}" +
222 "]}," +
223 "{@type=korap:token, @value={@type=korap:term, @value=mate/p:NN, relation==}}" +
224 "]}" +
225 "]}";
226 ppt = new PoliqarpPlusTree("{2: {1:[tt/p=ADJA]}[mate/p=NN]}");
227 map = ppt.getRequestMap().get("query").toString();
228 assertEquals(cls5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel53333e62013-12-09 19:25:52 +0000229 }
230
231 @Test
232 public void testPositions() {
233 // contains(<s>,<np>)
234 String pos1 = "{@type=korap:group, relation=position, position=contains, operands=[" +
235 "{@type=korap:element, @value=s}," +
236 "{@type=korap:element, @value=np}" +
237 "]}";
238 assertTrue(equalsQueryContent(pos1, "contains(<s>,<np>)"));
239
240 // contains(<s>,[base=Mann])
241 String pos2 = "{@type=korap:group, relation=position, position=contains, operands=[" +
242 "{@type=korap:element, @value=s}," +
243 "{@type=korap:token, @value= {@type=korap:term, @value=base:Mann, relation==}}" +
244 "]}";
245 assertTrue(equalsQueryContent(pos2, "contains(<s>,[base=Mann])"));
246
247 // contains(<s>,[orth=der][orth=Mann])
248 String pos3 = "{@type=korap:group, relation=position, position=contains, operands=[" +
249 "{@type=korap:element, @value=s}," +
250 "{@type=korap:sequence, operands=[" +
251 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}," +
252 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}" +
253 "]}" +
254 "]}";
255 ppt = new PoliqarpPlusTree("contains(<s>,[orth=der][orth=Mann])");
256 map = ppt.getRequestMap().get("query").toString();
257 assertEquals(pos3.replaceAll(" ", ""), map.replaceAll(" ", ""));
258
259 // [base=Auto]contains(<s>,[base=Mann])
260 String pos4 =
261 "{@type=korap:sequence, operands=[" +
262 "{@type=korap:token, @value={@type=korap:term, @value=base:Auto, relation==}}," +
263 "{@type=korap:group, relation=position, position=contains, operands=[" +
264 "{@type=korap:element, @value=s}," +
265 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}" +
266 "]}" +
267 "]}";
268 ppt = new PoliqarpPlusTree("[base=Auto]contains(<s>,[base=Mann])");
269 map = ppt.getRequestMap().get("query").toString();
270 assertEquals(pos4.replaceAll(" ", ""), map.replaceAll(" ", ""));
271 }
272
273 @Test
274 public void testNestedPositions() {
275 // contains(<s>,startswith(<np>,[orth=Der]))
276 String npos1 =
277 "{@type=korap:group, relation=position, position=contains, operands=[" +
278 "{@type=korap:element, @value=s}," +
279 "{@type=korap:group, relation=position, position=startswith, operands=[" +
280 "{@type=korap:element, @value=np}," +
281 "{@type=korap:token, @value={@type=korap:term, @value=orth:Der, relation==}}" +
282 "]}" +
283 "]}";
Joachim Bingelcd9ed332013-12-09 21:01:35 +0000284 ppt = new PoliqarpPlusTree("contains(<s>, startswith(<np>,[orth=Der]))");
Joachim Bingel53333e62013-12-09 19:25:52 +0000285 map = ppt.getRequestMap().get("query").toString();
286 assertEquals(npos1.replaceAll(" ", ""), map.replaceAll(" ", ""));
287 }
288
289 @Test
290 public void testShrinkSplit() {
291 // shrink([orth=Der]{[orth=Mann]})
292 String shr1 =
293 "{@type=korap:group, relation=shrink, shrink=0, operands=[" +
294 "{@type=korap:sequence, operands=[" +
295 "{@type=korap:token, @value={@type=korap:term, @value=orth:Der, relation==}}," +
296 "{@type=korap:group, class=0, operands=[" +
297 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}" +
298 "]}" +
299 "]}" +
300 "]}";
301 ppt = new PoliqarpPlusTree("shrink([orth=Der]{[orth=Mann]})");
302 map = ppt.getRequestMap().get("query").toString();
303 assertEquals(shr1.replaceAll(" ", ""), map.replaceAll(" ", ""));
304
305 // shrink([orth=Der]{[orth=Mann][orth=geht]})
306 String shr2 =
307 "{@type=korap:group, relation=shrink, shrink=0, operands=[" +
308 "{@type=korap:sequence, operands=[" +
309 "{@type=korap:token, @value={@type=korap:term, @value=orth:Der, relation==}}," +
310 "{@type=korap:group, class=0, operands=[" +
311 "{@type=korap:sequence, operands=[" +
312 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}," +
313 "{@type=korap:token, @value={@type=korap:term, @value=orth:geht, relation==}}" +
314 "]}" +
315 "]}" +
316 "]}" +
317 "]}";
318 ppt = new PoliqarpPlusTree("shrink([orth=Der]{[orth=Mann][orth=geht]})");
319 map = ppt.getRequestMap().get("query").toString();
320 assertEquals(shr2.replaceAll(" ", ""), map.replaceAll(" ", ""));
321
322 // shrink(1:[orth=Der]{1:[orth=Mann][orth=geht]})
323 String shr3 =
324 "{@type=korap:group, relation=shrink, shrink=1, operands=[" +
325 "{@type=korap:sequence, operands=[" +
326 "{@type=korap:token, @value={@type=korap:term, @value=orth:Der, relation==}}," +
327 "{@type=korap:group, class=1, operands=[" +
328 "{@type=korap:sequence, operands=[" +
329 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}," +
330 "{@type=korap:token, @value={@type=korap:term, @value=orth:geht, relation==}}" +
331 "]}" +
332 "]}" +
333 "]}" +
334 "]}";
335 ppt = new PoliqarpPlusTree("shrink(1:[orth=Der]{1:[orth=Mann][orth=geht]})");
336 map = ppt.getRequestMap().get("query").toString();
337 assertEquals(shr3.replaceAll(" ", ""), map.replaceAll(" ", ""));
338
339 // shrink(1:startswith(<s>,{1:<np>}))
340 String shr4 =
341 "{@type=korap:group, relation=shrink, shrink=1, operands=[" +
342 "{@type=korap:group, relation=position, position=startswith, operands=[" +
343 "{@type=korap:element, @value=s}," +
344 "{@type=korap:group, class=1, operands=[" +
345 "{@type=korap:element, @value=np}" +
346 "]}" +
347 "]}" +
348 "]}";
Joachim Bingelcd9ed332013-12-09 21:01:35 +0000349 ppt = new PoliqarpPlusTree("shrink(1: startswith(<s>,{1:<np>}))");
Joachim Bingel53333e62013-12-09 19:25:52 +0000350 map = ppt.getRequestMap().get("query").toString();
351 assertEquals(shr4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelcd9ed332013-12-09 21:01:35 +0000352
353 // shrink(3: startswith(<s>, {3:[base=der]{1:[mate/p=ADJA]{2:[tt/p=NN]}}}))
354 String shr5 =
355 "{@type=korap:group, relation=shrink, shrink=3, operands=[" +
356 "{@type=korap:group, relation=position, position=startswith, operands=[" +
357 "{@type=korap:element, @value=s}," +
358 "{@type=korap:group, class=3, operands=[" +
359 "{@type=korap:sequence, operands=[" +
360 "{@type=korap:token, @value={@type=korap:term, @value=base:der, relation==}}," +
361 "{@type=korap:group, class=1, operands=[" +
362 "{@type=korap:sequence, operands=[" +
363 "{@type=korap:token, @value={@type=korap:term, @value=mate/p:ADJA, relation==}}," +
364 "{@type=korap:group, class=2, operands=[" +
365 "{@type=korap:token, @value={@type=korap:term, @value=tt/p:NN, relation==}}" +
366 "]}" +
367 "]}" +
368 "]}" +
369 "]}" +
370 "]}" +
371 "]}" +
372 "]}";
373 ppt = new PoliqarpPlusTree("shrink(3: startswith(<s>, {3:[base=der]{1:[mate/p=ADJA]{2:[tt/p=NN]}}})) ");
374 map = ppt.getRequestMap().get("query").toString();
375 assertEquals(shr5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel53333e62013-12-09 19:25:52 +0000376 }
377
378 @Test
379 public void testLayers() {
Joachim Bingelcd9ed332013-12-09 21:01:35 +0000380 // [base=Mann]
381 String layer1 = "{@type=korap:token, @value={@type=korap:term, @value=tt/base:Mann, relation==}}";
382 assertTrue(equalsQueryContent(layer1, "[tt/base=Mann]"));
Joachim Bingel53333e62013-12-09 19:25:52 +0000383
384 }
385}
386