blob: 63437f28753886777c8a9f2b9023a0381b0f1fef [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() {
82 }
83
84 @Test
85 public void testTokenSequence() {
86 // [base=Mann][orth=Frau]
87 String seq1 = "{@type=korap:sequence, operands=[" +
88 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}, " +
89 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}" +
90 "]}";
91 assertTrue(equalsQueryContent(seq1, "[base=Mann][orth=Frau]"));
92
93 // [base=Mann][orth=Frau][p=NN]
94 String seq2 = "{@type=korap:sequence, operands=[" +
95 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}, " +
96 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}, " +
97 "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation==}}" +
98 "]}";
99 assertTrue(equalsQueryContent(seq2, "[base=Mann][orth=Frau][p=NN]"));
100 }
101
102 @Test
103 public void testTokenElemSequence() {
104 // [base=Mann]<vp>
105 String seq1 = "{@type=korap:sequence, operands=[" +
106 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}, " +
107 "{@type=korap:element, @value=vp}" +
108 "]}";
109 assertTrue(equalsQueryContent(seq1, "[base=Mann]<vp>"));
110
111 // <vp>[base=Mann]
112 String seq2 = "{@type=korap:sequence, operands=[" +
113 "{@type=korap:element, @value=vp}, "+
114 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}} " +
115 "]}";
116 assertTrue(equalsQueryContent(seq2, "<vp>[base=Mann]"));
117
118 // <vp>[base=Mann]<pp>
119 String seq3 = "{@type=korap:sequence, operands=[" +
120 "{@type=korap:element, @value=vp}, "+
121 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}, " +
122 "{@type=korap:element, @value=pp} "+
123 "]}";
124 assertTrue(equalsQueryContent(seq3, "<vp>[base=Mann]<pp>"));
125 }
126
127 @Test
128 public void testElemSequence() {
129 // <np><vp>
130 String seq1 = "{@type=korap:sequence, operands=[" +
131 "{@type=korap:element, @value=np}," +
132 "{@type=korap:element, @value=vp}" +
133 "]}";
134 assertTrue(equalsQueryContent(seq1, "<np><vp>"));
135
136 // <np><vp><pp>
137 String seq2 = "{@type=korap:sequence, operands=[" +
138 "{@type=korap:element, @value=np}," +
139 "{@type=korap:element, @value=vp}," +
140 "{@type=korap:element, @value=pp}" +
141 "]}";
142 assertTrue(equalsQueryContent(seq2, "<np><vp><pp>"));
143 }
144
145 @Test
146 public void testClasses() {
147 // {[base=Mann]}
148 String cls1 = "{@type=korap:group, class=0, operands=[" +
149 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}" +
150 "]}";
151 assertTrue(equalsQueryContent(cls1, "{[base=Mann]}"));
152
153 // {[base=Mann][orth=Frau]}
154 String cls2 = "{@type=korap:group, class=0, operands=[" +
155 "{@type=korap:sequence, operands=[" +
156 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}," +
157 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}" +
158 "]}" +
159 "]}";
160 assertTrue(equalsQueryContent(cls2, "{[base=Mann][orth=Frau]}"));
161
162 // [p=NN]{[base=Mann][orth=Frau]}
163 String cls3 = "{@type=korap:sequence, operands=[" +
164 "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation==}}," +
165 "{@type=korap:group, class=0, operands=[" +
166 "{@type=korap:sequence, operands=[" +
167 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}," +
168 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}" +
169 "]}" +
170 "]}" +
171 "]}";
172 assertTrue(equalsQueryContent(cls3, "[p=NN]{[base=Mann][orth=Frau]}"));
173
174 // {[base=Mann][orth=Frau]}[p=NN]
175 String cls4 = "{@type=korap:sequence, operands=[" +
176 "{@type=korap:group, class=0, operands=[" +
177 "{@type=korap:sequence, operands=[" +
178 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}," +
179 "{@type=korap:token, @value={@type=korap:term, @value=orth:Frau, relation==}}" +
180 "]}" +
181 "]}," +
182 "{@type=korap:token, @value={@type=korap:term, @value=p:NN, relation==}}" +
183 "]}";
184 assertTrue(equalsQueryContent(cls4, "{[base=Mann][orth=Frau]}[p=NN]"));
185 }
186
187 @Test
188 public void testPositions() {
189 // contains(<s>,<np>)
190 String pos1 = "{@type=korap:group, relation=position, position=contains, operands=[" +
191 "{@type=korap:element, @value=s}," +
192 "{@type=korap:element, @value=np}" +
193 "]}";
194 assertTrue(equalsQueryContent(pos1, "contains(<s>,<np>)"));
195
196 // contains(<s>,[base=Mann])
197 String pos2 = "{@type=korap:group, relation=position, position=contains, operands=[" +
198 "{@type=korap:element, @value=s}," +
199 "{@type=korap:token, @value= {@type=korap:term, @value=base:Mann, relation==}}" +
200 "]}";
201 assertTrue(equalsQueryContent(pos2, "contains(<s>,[base=Mann])"));
202
203 // contains(<s>,[orth=der][orth=Mann])
204 String pos3 = "{@type=korap:group, relation=position, position=contains, operands=[" +
205 "{@type=korap:element, @value=s}," +
206 "{@type=korap:sequence, operands=[" +
207 "{@type=korap:token, @value={@type=korap:term, @value=orth:der, relation==}}," +
208 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}" +
209 "]}" +
210 "]}";
211 ppt = new PoliqarpPlusTree("contains(<s>,[orth=der][orth=Mann])");
212 map = ppt.getRequestMap().get("query").toString();
213 assertEquals(pos3.replaceAll(" ", ""), map.replaceAll(" ", ""));
214
215 // [base=Auto]contains(<s>,[base=Mann])
216 String pos4 =
217 "{@type=korap:sequence, operands=[" +
218 "{@type=korap:token, @value={@type=korap:term, @value=base:Auto, relation==}}," +
219 "{@type=korap:group, relation=position, position=contains, operands=[" +
220 "{@type=korap:element, @value=s}," +
221 "{@type=korap:token, @value={@type=korap:term, @value=base:Mann, relation==}}" +
222 "]}" +
223 "]}";
224 ppt = new PoliqarpPlusTree("[base=Auto]contains(<s>,[base=Mann])");
225 map = ppt.getRequestMap().get("query").toString();
226 assertEquals(pos4.replaceAll(" ", ""), map.replaceAll(" ", ""));
227 }
228
229 @Test
230 public void testNestedPositions() {
231 // contains(<s>,startswith(<np>,[orth=Der]))
232 String npos1 =
233 "{@type=korap:group, relation=position, position=contains, operands=[" +
234 "{@type=korap:element, @value=s}," +
235 "{@type=korap:group, relation=position, position=startswith, operands=[" +
236 "{@type=korap:element, @value=np}," +
237 "{@type=korap:token, @value={@type=korap:term, @value=orth:Der, relation==}}" +
238 "]}" +
239 "]}";
240 ppt = new PoliqarpPlusTree("contains(<s>,startswith(<np>,[orth=Der]))");
241 map = ppt.getRequestMap().get("query").toString();
242 assertEquals(npos1.replaceAll(" ", ""), map.replaceAll(" ", ""));
243 }
244
245 @Test
246 public void testShrinkSplit() {
247 // shrink([orth=Der]{[orth=Mann]})
248 String shr1 =
249 "{@type=korap:group, relation=shrink, shrink=0, operands=[" +
250 "{@type=korap:sequence, operands=[" +
251 "{@type=korap:token, @value={@type=korap:term, @value=orth:Der, relation==}}," +
252 "{@type=korap:group, class=0, operands=[" +
253 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}" +
254 "]}" +
255 "]}" +
256 "]}";
257 ppt = new PoliqarpPlusTree("shrink([orth=Der]{[orth=Mann]})");
258 map = ppt.getRequestMap().get("query").toString();
259 assertEquals(shr1.replaceAll(" ", ""), map.replaceAll(" ", ""));
260
261 // shrink([orth=Der]{[orth=Mann][orth=geht]})
262 String shr2 =
263 "{@type=korap:group, relation=shrink, shrink=0, operands=[" +
264 "{@type=korap:sequence, operands=[" +
265 "{@type=korap:token, @value={@type=korap:term, @value=orth:Der, relation==}}," +
266 "{@type=korap:group, class=0, operands=[" +
267 "{@type=korap:sequence, operands=[" +
268 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}," +
269 "{@type=korap:token, @value={@type=korap:term, @value=orth:geht, relation==}}" +
270 "]}" +
271 "]}" +
272 "]}" +
273 "]}";
274 ppt = new PoliqarpPlusTree("shrink([orth=Der]{[orth=Mann][orth=geht]})");
275 map = ppt.getRequestMap().get("query").toString();
276 assertEquals(shr2.replaceAll(" ", ""), map.replaceAll(" ", ""));
277
278 // shrink(1:[orth=Der]{1:[orth=Mann][orth=geht]})
279 String shr3 =
280 "{@type=korap:group, relation=shrink, shrink=1, operands=[" +
281 "{@type=korap:sequence, operands=[" +
282 "{@type=korap:token, @value={@type=korap:term, @value=orth:Der, relation==}}," +
283 "{@type=korap:group, class=1, operands=[" +
284 "{@type=korap:sequence, operands=[" +
285 "{@type=korap:token, @value={@type=korap:term, @value=orth:Mann, relation==}}," +
286 "{@type=korap:token, @value={@type=korap:term, @value=orth:geht, relation==}}" +
287 "]}" +
288 "]}" +
289 "]}" +
290 "]}";
291 ppt = new PoliqarpPlusTree("shrink(1:[orth=Der]{1:[orth=Mann][orth=geht]})");
292 map = ppt.getRequestMap().get("query").toString();
293 assertEquals(shr3.replaceAll(" ", ""), map.replaceAll(" ", ""));
294
295 // shrink(1:startswith(<s>,{1:<np>}))
296 String shr4 =
297 "{@type=korap:group, relation=shrink, shrink=1, operands=[" +
298 "{@type=korap:group, relation=position, position=startswith, operands=[" +
299 "{@type=korap:element, @value=s}," +
300 "{@type=korap:group, class=1, operands=[" +
301 "{@type=korap:element, @value=np}" +
302 "]}" +
303 "]}" +
304 "]}";
305 ppt = new PoliqarpPlusTree("shrink(1:startswith(<s>,{1:<np>}))");
306 map = ppt.getRequestMap().get("query").toString();
307 assertEquals(shr4.replaceAll(" ", ""), map.replaceAll(" ", ""));
308 }
309
310 @Test
311 public void testLayers() {
312
313 }
314}
315