blob: 11cd197fe424266e105414c0c7bd4a07a64deab9 [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 Bingeld5161a12014-01-08 11:15:49 +0000209 "{@type=korap:group, relation=include, position=L, operands=[" +
210 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
211 "{@type=korap:element, @value=s}" +
212 "]}";
213 ppt = new CosmasTree(query);
214 map = ppt.getRequestMap().get("query").toString();
215 assertEquals(opin1.replaceAll(" ", ""), map.replaceAll(" ", ""));
216
217 // position argument is optional
218 query="wegen #IN <s>";
219 String opin2 =
220 "{@type=korap:group, relation=include, operands=[" +
221 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
222 "{@type=korap:element, @value=s}" +
223 "]}";
224 ppt = new CosmasTree(query);
225 map = ppt.getRequestMap().get("query").toString();
226 assertEquals(opin2.replaceAll(" ", ""), map.replaceAll(" ", ""));
227
228 // parentheses around 'wegen mir' are optional
229 query="wegen #IN (wegen mir)";
230 String opin3 =
231 "{@type=korap:group, relation=include, operands=[" +
232 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
233 "{@type=korap:sequence, operands=[" +
234 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
235 "{@type=korap:token, @value={@type=korap:term, @value=orth:mir, relation==}}" +
236 "]}" +
237 "]}";
238 ppt = new CosmasTree(query);
239 map = ppt.getRequestMap().get("query").toString();
240 assertEquals(opin3.replaceAll(" ", ""), map.replaceAll(" ", ""));
241 }
242
243 @Test
244 public void testOPOV() {
245 query="wegen #OV <s>";
246 String opin1 =
247 "{@type=korap:group, relation=overlap, operands=[" +
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000248 "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," +
249 "{@type=korap:element, @value=s}" +
250 "]}";
251 ppt = new CosmasTree(query);
252 map = ppt.getRequestMap().get("query").toString();
Joachim Bingel3a9f7932014-01-07 17:11:31 +0000253 assertEquals(opin1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000254 }
255
256 @Test
257 public void testOPNOT() {
Joachim Bingel3a9f7932014-01-07 17:11:31 +0000258 query="Sonne nicht Mond";
259 String opnot1 =
260 "{@type=korap:group, relation=not, operands=[" +
261 "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," +
262 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" +
263 "]}";
264 ppt = new CosmasTree(query);
265 map = ppt.getRequestMap().get("query").toString();
266 assertEquals(opnot1.replaceAll(" ", ""), map.replaceAll(" ", ""));
267 }
268
269 @Test
270 public void testBEG_END() {
271 // BEG and END operators
272 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/links.html
273 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/rechts.html
274 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/thematische-bsp/bsp-satzlaenge.html
Joachim Bingel89cceac2014-01-08 15:51:08 +0000275 query="";
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000276 }
Joachim Bingeld5161a12014-01-08 11:15:49 +0000277
278
279 @Test
280 public void testELEM() {
281 // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/elem.html
Joachim Bingel89cceac2014-01-08 15:51:08 +0000282 //XXX NOT WORKING IN ANTLR GRAMMAR! ELEM(S) returns (C2PQ (OPWF "ELEM") (OPWF "S"))
Joachim Bingeld5161a12014-01-08 11:15:49 +0000283 }
284
285 @Test
286 public void testOPALL() {
Joachim Bingel89cceac2014-01-08 15:51:08 +0000287 query="#ALL(gehen /w1:10 voran)";
288 String all1 =
289 "{@type=korap:group, relation=all, operands=[" +
290 "{@type=korap:group, relation=distance, @subtype=incl, " +
291 "constraint=[" +
292 "{@type=korap:distance, measure=w, direction=both, min=1, max=10}" +
293 "], " +
294 "operands=[" +
295 "{@type=korap:token, @value={@type=korap:term, @value=orth:gehen, relation==}}," +
296 "{@type=korap:token, @value={@type=korap:term, @value=orth:voran, relation==}}" +
297 "]" +
298 "}" +
299 "]}";
300 ppt = new CosmasTree(query);
301 map = ppt.getRequestMap().get("query").toString();
302 assertEquals(all1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000303 }
304
305 @Test
306 public void testOPNHIT() {
Joachim Bingel89cceac2014-01-08 15:51:08 +0000307 query="#NHIT(gehen /w1:10 voran)";
308 String nhit1 =
309 "{@type=korap:group, relation=nhit, operands=[" +
310 "{@type=korap:group, relation=distance, @subtype=incl, " +
311 "constraint=[" +
312 "{@type=korap:distance, measure=w, direction=both, min=1, max=10}" +
313 "], " +
314 "operands=[" +
315 "{@type=korap:token, @value={@type=korap:term, @value=orth:gehen, relation==}}," +
316 "{@type=korap:token, @value={@type=korap:term, @value=orth:voran, relation==}}" +
317 "]" +
318 "}" +
319 "]}";
320 ppt = new CosmasTree(query);
321 map = ppt.getRequestMap().get("query").toString();
322 assertEquals(nhit1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld5161a12014-01-08 11:15:49 +0000323 }
324
325 @Test
326 public void testOPBED() {
Joachim Bingel89cceac2014-01-08 15:51:08 +0000327 //XXX NOT WORKING IN ANTLR GRAMMAR! #BED(der, sa) returns (C2PQ (OPBED (OPWF "der,") (OPWF "sa") (OPTS <mismatched token: [@3,12:12=')',<45>,1:12], resync=)>)))
Joachim Bingeld5161a12014-01-08 11:15:49 +0000328 }
329
Joachim Bingelb5f7bf02014-01-07 16:36:54 +0000330}
331