blob: 6e67f3156f597a108044538865395ef1d6cc440d [file] [log] [blame]
Joachim Bingelb5f7bf02014-01-07 16:36:54 +00001import static org.junit.Assert.*;
2
3import org.junit.Test;
4
5import de.ids_mannheim.korap.query.serialize.CosmasTree;
6import de.ids_mannheim.korap.query.serialize.PoliqarpPlusTree;
7import de.ids_mannheim.korap.util.QueryException;
8
9public class CosmasTreeTest {
10
11 CosmasTree ppt;
12 String map;
13 String query;
14
15 private boolean equalsContent(String str, Object map) {
16 str = str.replaceAll(" ", "");
17 String mapStr = map.toString().replaceAll(" ", "");
18 return str.equals(mapStr);
19 }
20
21 private boolean equalsQueryContent(String res, String query) throws QueryException {
22 res = res.replaceAll(" ", "");
23 ppt = new CosmasTree(query);
24 String queryMap = ppt.getRequestMap().get("query").toString().replaceAll(" ", "");
25 return res.equals(queryMap);
26 }
27
28 @Test
29 public void testContext() throws QueryException {
30 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}";
31 ppt = new CosmasTree("Test");
32 assertTrue(equalsContent(contextString, ppt.getRequestMap().get("@context")));
33 }
34
35
36 @Test
37 public void testSingleToken() {
38 query="der";
39 String single1 =
40 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}";
41 ppt = new CosmasTree(query);
42 map = ppt.getRequestMap().get("query").toString();
43 assertEquals(single1.replaceAll(" ", ""), map.replaceAll(" ", ""));
44
45 query="Mann";
46 String single2 =
47 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}";
48 ppt = new CosmasTree(query);
49 map = ppt.getRequestMap().get("query").toString();
50 assertEquals(single2.replaceAll(" ", ""), map.replaceAll(" ", ""));
51
52 query="&Mann";
53 String single3 =
54 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}";
55 ppt = new CosmasTree(query);
56 map = ppt.getRequestMap().get("query").toString();
57 assertEquals(single3.replaceAll(" ", ""), map.replaceAll(" ", ""));
58 }
59
60 @Test
61 public void testSequence() {
62 query="der Mann";
63 String seq1 =
64 "{@type=korap:sequence, operands=[" +
65 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}," +
66 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}" +
67 "]}";
68 ppt = new CosmasTree(query);
69 map = ppt.getRequestMap().get("query").toString();
70 assertEquals(seq1.replaceAll(" ", ""), map.replaceAll(" ", ""));
71
Joachim Bingeld5161a12014-01-08 11:15:49 +000072 query="der Mann schläft";
73 String seq2 =
74 "{@type=korap:sequence, operands=[" +
75 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}," +
76 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}," +
77 "{@type=korap:token, @value={@type=korap:term, @value=orth:schläft, relation==}}" +
78 "]}";
79 ppt = new CosmasTree(query);
80 map = ppt.getRequestMap().get("query").toString();
81 assertEquals(seq2.replaceAll(" ", ""), map.replaceAll(" ", ""));
82
83 query="der Mann schläft lang";
84 String seq3 =
85 "{@type=korap:sequence, operands=[" +
86 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}," +
87 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}," +
88 "{@type=korap:token, @value={@type=korap:term, @value=orth:schläft, relation==}}," +
89 "{@type=korap:token, @value={@type=korap:term, @value=orth:lang, relation==}}" +
90 "]}";
91 ppt = new CosmasTree(query);
92 map = ppt.getRequestMap().get("query").toString();
93 assertEquals(seq3.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelb5f7bf02014-01-07 16:36:54 +000094 }
95
96 @Test
97 public void testOPOR() throws QueryException {
98 query="Sonne oder Mond";
99 String disj1 =
100 "{@type=korap:group, relation=or, operands=[" +
101 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
102 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" +
103 "]}";
104 ppt = new CosmasTree(query);
105 map = ppt.getRequestMap().get("query").toString();
106 assertEquals(disj1.replaceAll(" ", ""), map.replaceAll(" ", ""));
107
Joachim Bingeld5161a12014-01-08 11:15:49 +0000108 query="(Sonne scheint) oder Mond";
109 String disj2 =
110 "{@type=korap:group, relation=or, operands=[" +
111 "{@type=korap:sequence, operands=[" +
112 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
113 "{@type=korap:token, @value={@type=korap:term, @value=orth:scheint, relation==}}" +
114 "]}," +
115 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" +
116 "]}";
117 ppt = new CosmasTree(query);
118 map = ppt.getRequestMap().get("query").toString();
119 assertEquals(disj2.replaceAll(" ", ""), map.replaceAll(" ", ""));
120
121 query="(Sonne scheint) oder (Mond scheint)";
122 String disj3 =
123 "{@type=korap:group, relation=or, operands=[" +
124 "{@type=korap:sequence, operands=[" +
125 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
126 "{@type=korap:token, @value={@type=korap:term, @value=orth:scheint, relation==}}" +
127 "]}," +
128 "{@type=korap:sequence, operands=[" +
129 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}," +
130 "{@type=korap:token, @value={@type=korap:term, @value=orth:scheint, relation==}}" +
131 "]}" +
132 "]}";
133 ppt = new CosmasTree(query);
134 map = ppt.getRequestMap().get("query").toString();
135 assertEquals(disj3.replaceAll(" ", ""), map.replaceAll(" ", ""));
136
137 }
138
139 @Test
140 public void testOPORAND() {
141 query="(Sonne oder Mond) und scheint";
142 String orand1 =
143 "{@type=korap:group, relation=and, operands=[" +
144 "{@type=korap:group, relation=or, operands=[" +
145 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
146 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" +
147 "]}," +
148 "{@type=korap:token, @value={@type=korap:term, @value=orth:scheint, relation==}}" +
149 "]}";
150 ppt = new CosmasTree(query);
151 map = ppt.getRequestMap().get("query").toString();
152 assertEquals(orand1.replaceAll(" ", ""), map.replaceAll(" ", ""));
153
154 query="scheint und (Sonne oder Mond)";
155 String orand2 =
156 "{@type=korap:group, relation=and, operands=[" +
157 "{@type=korap:token, @value={@type=korap:term, @value=orth:scheint, relation==}}," +
158 "{@type=korap:group, relation=or, operands=[" +
159 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
160 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" +
161 "]}" +
162 "]}";
163 ppt = new CosmasTree(query);
164 map = ppt.getRequestMap().get("query").toString();
165 assertEquals(orand2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000166 }
167
168 @Test
169 public void testOPPROX() {
170 query="Sonne /+w1:4 Mond";
171 String prox1 =
Joachim Bingel89cceac2014-01-08 15:51:08 +0000172 "{@type=korap:group, relation=distance, @subtype=incl, " +
173 "constraint=[" +
174 "{@type=korap:distance, measure=w, direction=plus, min=1, max=4}" +
175 "], " +
176 "operands=[" +
177 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
178 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" +
179 "]" +
180 "}";
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000181 ppt = new CosmasTree(query);
182 map = ppt.getRequestMap().get("query").toString();
183 assertEquals(prox1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel89cceac2014-01-08 15:51:08 +0000184
185 query="Sonne /+w1:4,s0,p1:3 Mond";
186 String prox2 =
187 "{@type=korap:group, relation=distance, @subtype=incl, " +
188 "constraint=[" +
189 "{@type=korap:group, relation=and, operands=[" +
190 "{@type=korap:distance, measure=w, direction=plus, min=1, max=4}," +
191 "{@type=korap:distance, measure=s, direction=both, min=0, max=0}," +
192 "{@type=korap:distance, measure=p, direction=both, min=1, max=3}" +
193 "]}" +
194 "], " +
195 "operands=[" +
196 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
197 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" +
198 "]" +
199 "}";
200 ppt = new CosmasTree(query);
201 map = ppt.getRequestMap().get("query").toString();
202 assertEquals(prox2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000203 }
204
205 @Test
206 public void testOPIN() {
Joachim Bingelcc1dc242014-01-15 09:32:38 +0000207 query="wegen #IN <s>";
Joachim Bingel3a9f7932014-01-07 17:11:31 +0000208 String opin1 =
Joachim Bingeleecc7652014-01-11 17:21:07 +0000209 "{@type=korap:group, relation=shrink, shrink=1, operands=[" +
Joachim Bingelcc1dc242014-01-15 09:32:38 +0000210 "{@type=korap:group, relation=position, position=contains, operands=[" +
Joachim Bingeleecc7652014-01-11 17:21:07 +0000211 "{@type=korap:elem, @value=s}," +
212 "{@type=korap:group, class=1, operands=[" +
Joachim Bingelcc1dc242014-01-15 09:32:38 +0000213 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}" +
Joachim Bingeleecc7652014-01-11 17:21:07 +0000214 "]}" +
215 "]}" +
Joachim Bingeld5161a12014-01-08 11:15:49 +0000216 "]}";
217 ppt = new CosmasTree(query);
218 map = ppt.getRequestMap().get("query").toString();
219 assertEquals(opin1.replaceAll(" ", ""), map.replaceAll(" ", ""));
220
Joachim Bingelcc1dc242014-01-15 09:32:38 +0000221 query="wegen #IN(L) <s>";
222 String opin2 =
223 "{@type=korap:group, relation=shrink, shrink=1, operands=[" +
224 "{@type=korap:group, relation=position, position=startswith, operands=[" +
225 "{@type=korap:elem, @value=s}," +
226 "{@type=korap:group, class=1, operands=[" +
227 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}" +
228 "]}" +
229 "]}" +
230 "]}";
231 ppt = new CosmasTree(query);
232 map = ppt.getRequestMap().get("query").toString();
233 assertEquals(opin2.replaceAll(" ", ""), map.replaceAll(" ", ""));
234
Joachim Bingeleecc7652014-01-11 17:21:07 +0000235// // position argument is optional
236// query="wegen #IN <s>";
237// String opin2 =
238// "{@type=korap:group, relation=include, operands=[" +
239// "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
240// "{@type=korap:elem, @value=s}" +
241// "]}";
242// ppt = new CosmasTree(query);
243// map = ppt.getRequestMap().get("query").toString();
244// assertEquals(opin2.replaceAll(" ", ""), map.replaceAll(" ", ""));
245//
246// // parentheses around 'wegen mir' are optional
247// query="wegen #IN (wegen mir)";
248// String opin3 =
249// "{@type=korap:group, relation=include, operands=[" +
250// "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
251// "{@type=korap:sequence, operands=[" +
252// "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
253// "{@type=korap:token, @value={@type=korap:term, @value=orth:mir, relation==}}" +
254// "]}" +
255// "]}";
256// ppt = new CosmasTree(query);
257// map = ppt.getRequestMap().get("query").toString();
258// assertEquals(opin3.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000259 }
260
261 @Test
262 public void testOPOV() {
263 query="wegen #OV <s>";
Joachim Bingelcc1dc242014-01-15 09:32:38 +0000264 String opov1 =
265 "{@type=korap:group, relation=shrink, shrink=1, operands=[" +
266 "{@type=korap:group, relation=overlap, position=full, operands=[" +
267 "{@type=korap:elem, @value=s}," +
268 "{@type=korap:group, class=1, operands=[" +
269 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}" +
270 "]}" +
271 "]}" +
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000272 "]}";
273 ppt = new CosmasTree(query);
274 map = ppt.getRequestMap().get("query").toString();
Joachim Bingelcc1dc242014-01-15 09:32:38 +0000275 assertEquals(opov1.replaceAll(" ", ""), map.replaceAll(" ", ""));
276
277 query="wegen #OV(L) <s>";
278 String opov2 =
279 "{@type=korap:group, relation=shrink, shrink=1, operands=[" +
280 "{@type=korap:group, relation=overlap, position=startswith, operands=[" +
281 "{@type=korap:elem, @value=s}," +
282 "{@type=korap:group, class=1, operands=[" +
283 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}" +
284 "]}" +
285 "]}" +
286 "]}";
287 ppt = new CosmasTree(query);
288 map = ppt.getRequestMap().get("query").toString();
289 assertEquals(opov2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000290 }
291
292 @Test
293 public void testOPNOT() {
Joachim Bingel3a9f7932014-01-07 17:11:31 +0000294 query="Sonne nicht Mond";
295 String opnot1 =
296 "{@type=korap:group, relation=not, operands=[" +
297 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
298 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" +
299 "]}";
300 ppt = new CosmasTree(query);
301 map = ppt.getRequestMap().get("query").toString();
302 assertEquals(opnot1.replaceAll(" ", ""), map.replaceAll(" ", ""));
303 }
304
305 @Test
306 public void testBEG_END() {
307 // BEG and END operators
308 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/links.html
309 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/rechts.html
310 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/thematische-bsp/bsp-satzlaenge.html
Joachim Bingel89cceac2014-01-08 15:51:08 +0000311 query="";
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000312 }
Joachim Bingeld5161a12014-01-08 11:15:49 +0000313
314
315 @Test
316 public void testELEM() {
317 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/elem.html
Joachim Bingeleecc7652014-01-11 17:21:07 +0000318 query="#ELEM(S)";
319 String elem1 = "{@type=korap:elem, @value=s}";
320 ppt = new CosmasTree(query);
321 map = ppt.getRequestMap().get("query").toString();
322 assertEquals(elem1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000323 }
324
325 @Test
326 public void testOPALL() {
Joachim Bingel89cceac2014-01-08 15:51:08 +0000327 query="#ALL(gehen /w1:10 voran)";
328 String all1 =
329 "{@type=korap:group, relation=all, operands=[" +
330 "{@type=korap:group, relation=distance, @subtype=incl, " +
331 "constraint=[" +
332 "{@type=korap:distance, measure=w, direction=both, min=1, max=10}" +
333 "], " +
334 "operands=[" +
335 "{@type=korap:token, @value={@type=korap:term, @value=orth:gehen, relation==}}," +
336 "{@type=korap:token, @value={@type=korap:term, @value=orth:voran, relation==}}" +
337 "]" +
338 "}" +
339 "]}";
340 ppt = new CosmasTree(query);
341 map = ppt.getRequestMap().get("query").toString();
342 assertEquals(all1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000343 }
344
345 @Test
346 public void testOPNHIT() {
Joachim Bingel89cceac2014-01-08 15:51:08 +0000347 query="#NHIT(gehen /w1:10 voran)";
348 String nhit1 =
349 "{@type=korap:group, relation=nhit, operands=[" +
Joachim Bingeleecc7652014-01-11 17:21:07 +0000350 "{@type=korap:group, relation=distance, @subtype=incl, " +
351 "constraint=[" +
352 "{@type=korap:distance, measure=w, direction=both, min=1, max=10}" +
353 "], " +
354 "operands=[" +
355 "{@type=korap:token, @value={@type=korap:term, @value=orth:gehen, relation==}}," +
356 "{@type=korap:token, @value={@type=korap:term, @value=orth:voran, relation==}}" +
357 "]" +
358 "}" +
359 "]}";
Joachim Bingel89cceac2014-01-08 15:51:08 +0000360 ppt = new CosmasTree(query);
361 map = ppt.getRequestMap().get("query").toString();
362 assertEquals(nhit1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000363 }
364
365 @Test
366 public void testOPBED() {
Joachim Bingeleecc7652014-01-11 17:21:07 +0000367 query = "#BED(der , sa)";
368 String bed1 =
369 "{@type=korap:group, relation=position, position=startswith, operands=[" +
370 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}," +
371 "{@type=korap:elem, @value=s}" +
372 "]}";
373 ppt = new CosmasTree(query);
374 map = ppt.getRequestMap().get("query").toString();
375 assertEquals(bed1.replaceAll(" ", ""), map.replaceAll(" ", ""));
376
377 query = "#BED(der Mann , +pe)";
378 String bed2 =
379 "{@type=korap:group, relation=position, position=endswith, operands=[" +
380 "{@type=korap:sequence, operands=[" +
381 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}," +
382 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}" +
383 "]}," +
384 "{@type=korap:elem, @value=p}" +
385 "]}";
386 ppt = new CosmasTree(query);
387 map = ppt.getRequestMap().get("query").toString();
388 assertEquals(bed2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000389 }
390
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000391}
392