blob: 6f0de626a17527886a3d1b8cce77e3f02914a429 [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() {
207 query="wegen #IN(L) <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=[" +
210 "{@type=korap:group, relation=position, position=startswith, operands=[" +
211 "{@type=korap:elem, @value=s}," +
212 "{@type=korap:group, class=1, operands=[" +
213 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
214 "]}" +
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 Bingeleecc7652014-01-11 17:21:07 +0000221// // position argument is optional
222// query="wegen #IN <s>";
223// String opin2 =
224// "{@type=korap:group, relation=include, operands=[" +
225// "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
226// "{@type=korap:elem, @value=s}" +
227// "]}";
228// ppt = new CosmasTree(query);
229// map = ppt.getRequestMap().get("query").toString();
230// assertEquals(opin2.replaceAll(" ", ""), map.replaceAll(" ", ""));
231//
232// // parentheses around 'wegen mir' are optional
233// query="wegen #IN (wegen mir)";
234// String opin3 =
235// "{@type=korap:group, relation=include, operands=[" +
236// "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
237// "{@type=korap:sequence, operands=[" +
238// "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
239// "{@type=korap:token, @value={@type=korap:term, @value=orth:mir, relation==}}" +
240// "]}" +
241// "]}";
242// ppt = new CosmasTree(query);
243// map = ppt.getRequestMap().get("query").toString();
244// assertEquals(opin3.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000245 }
246
247 @Test
248 public void testOPOV() {
249 query="wegen #OV <s>";
250 String opin1 =
251 "{@type=korap:group, relation=overlap, operands=[" +
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000252 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
Joachim Bingeleecc7652014-01-11 17:21:07 +0000253 "{@type=korap:elem, @value=s}" +
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000254 "]}";
255 ppt = new CosmasTree(query);
256 map = ppt.getRequestMap().get("query").toString();
Joachim Bingel3a9f7932014-01-07 17:11:31 +0000257 assertEquals(opin1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000258 }
259
260 @Test
261 public void testOPNOT() {
Joachim Bingel3a9f7932014-01-07 17:11:31 +0000262 query="Sonne nicht Mond";
263 String opnot1 =
264 "{@type=korap:group, relation=not, operands=[" +
265 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
266 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" +
267 "]}";
268 ppt = new CosmasTree(query);
269 map = ppt.getRequestMap().get("query").toString();
270 assertEquals(opnot1.replaceAll(" ", ""), map.replaceAll(" ", ""));
271 }
272
273 @Test
274 public void testBEG_END() {
275 // BEG and END operators
276 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/links.html
277 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/rechts.html
278 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/thematische-bsp/bsp-satzlaenge.html
Joachim Bingel89cceac2014-01-08 15:51:08 +0000279 query="";
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000280 }
Joachim Bingeld5161a12014-01-08 11:15:49 +0000281
282
283 @Test
284 public void testELEM() {
285 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/elem.html
Joachim Bingeleecc7652014-01-11 17:21:07 +0000286 query="#ELEM(S)";
287 String elem1 = "{@type=korap:elem, @value=s}";
288 ppt = new CosmasTree(query);
289 map = ppt.getRequestMap().get("query").toString();
290 assertEquals(elem1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000291 }
292
293 @Test
294 public void testOPALL() {
Joachim Bingel89cceac2014-01-08 15:51:08 +0000295 query="#ALL(gehen /w1:10 voran)";
296 String all1 =
297 "{@type=korap:group, relation=all, operands=[" +
298 "{@type=korap:group, relation=distance, @subtype=incl, " +
299 "constraint=[" +
300 "{@type=korap:distance, measure=w, direction=both, min=1, max=10}" +
301 "], " +
302 "operands=[" +
303 "{@type=korap:token, @value={@type=korap:term, @value=orth:gehen, relation==}}," +
304 "{@type=korap:token, @value={@type=korap:term, @value=orth:voran, relation==}}" +
305 "]" +
306 "}" +
307 "]}";
308 ppt = new CosmasTree(query);
309 map = ppt.getRequestMap().get("query").toString();
310 assertEquals(all1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000311 }
312
313 @Test
314 public void testOPNHIT() {
Joachim Bingel89cceac2014-01-08 15:51:08 +0000315 query="#NHIT(gehen /w1:10 voran)";
316 String nhit1 =
317 "{@type=korap:group, relation=nhit, operands=[" +
Joachim Bingeleecc7652014-01-11 17:21:07 +0000318 "{@type=korap:group, relation=distance, @subtype=incl, " +
319 "constraint=[" +
320 "{@type=korap:distance, measure=w, direction=both, min=1, max=10}" +
321 "], " +
322 "operands=[" +
323 "{@type=korap:token, @value={@type=korap:term, @value=orth:gehen, relation==}}," +
324 "{@type=korap:token, @value={@type=korap:term, @value=orth:voran, relation==}}" +
325 "]" +
326 "}" +
327 "]}";
Joachim Bingel89cceac2014-01-08 15:51:08 +0000328 ppt = new CosmasTree(query);
329 map = ppt.getRequestMap().get("query").toString();
330 assertEquals(nhit1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000331 }
332
333 @Test
334 public void testOPBED() {
Joachim Bingeleecc7652014-01-11 17:21:07 +0000335 query = "#BED(der , sa)";
336 String bed1 =
337 "{@type=korap:group, relation=position, position=startswith, operands=[" +
338 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}," +
339 "{@type=korap:elem, @value=s}" +
340 "]}";
341 ppt = new CosmasTree(query);
342 map = ppt.getRequestMap().get("query").toString();
343 assertEquals(bed1.replaceAll(" ", ""), map.replaceAll(" ", ""));
344
345 query = "#BED(der Mann , +pe)";
346 String bed2 =
347 "{@type=korap:group, relation=position, position=endswith, operands=[" +
348 "{@type=korap:sequence, operands=[" +
349 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}," +
350 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}" +
351 "]}," +
352 "{@type=korap:elem, @value=p}" +
353 "]}";
354 ppt = new CosmasTree(query);
355 map = ppt.getRequestMap().get("query").toString();
356 assertEquals(bed2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000357 }
358
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000359}
360