blob: a39ccc4fa2c87701cf6a1e0d0ca9ba69a81a5155 [file] [log] [blame]
Joachim Bingel019ba5c2014-04-28 14:59:04 +00001import static org.junit.Assert.*;
2
3import org.junit.Test;
4
5import de.ids_mannheim.korap.query.serialize.AqlTree;
Joachim Bingel019ba5c2014-04-28 14:59:04 +00006import de.ids_mannheim.korap.util.QueryException;
7
8public class AqlTreeTest {
9
10 AqlTree aqlt;
11 String map;
12 private String query;
13
14 private boolean equalsQueryContent(String res, String query) throws QueryException {
15 res = res.replaceAll(" ", "");
16 aqlt = new AqlTree(query);
17 String queryMap = aqlt.getRequestMap().get("query").toString().replaceAll(" ", "");
18 return res.equals(queryMap);
19 }
20
21 @Test
22 public void testContext() throws QueryException {
23 String contextString = "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld";
24 aqlt = new AqlTree("Test");
25 assertEquals(contextString.replaceAll(" ", ""), aqlt.getRequestMap().get("@context").toString().replaceAll(" ", ""));
26 }
27
28 @Test
29 public void testSingleTokens() throws QueryException {
30 // "Mann"
31 query = "\"Mann\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000032 String token1 = "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000033 assertTrue(equalsQueryContent(token1, query));
34
35 // [orth!=Frau]
36 query = "tok!=\"Frau\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000037 String token2 = "{@type=korap:token, wrap={@type=korap:term, key=Frau, match=match:ne}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000038 assertTrue(equalsQueryContent(token2, query));
39
40 // Mann
41 query = "Mann";
42 String token4 = "{@type=korap:span, layer=Mann}";
43 assertTrue(equalsQueryContent(token4, query));
44 }
45
46 @Test
47 public void testSpans() throws QueryException {
48 query = "node";
49 String span1 =
50 "{@type=korap:span}";
51 aqlt = new AqlTree(query);
52 map = aqlt.getRequestMap().get("query").toString();
53 assertEquals(span1.replaceAll(" ", ""), map.replaceAll(" ", ""));
54 }
55
56 @Test
57 public void testRegex() throws QueryException {
58 query = "/Mann/";
Joachim Bingeleee549e2014-04-29 11:15:37 +000059 String regex1 = "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=Mann, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000060 aqlt = new AqlTree(query);
61 map = aqlt.getRequestMap().get("query").toString();
62 assertEquals(regex1.replaceAll(" ", ""), map.replaceAll(" ", ""));
63 }
64
65 @Test
66 public void testLayers() throws QueryException {
67 query = "cnx/cat=\"NP\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000068 String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000069 aqlt = new AqlTree(query);
70 map = aqlt.getRequestMap().get("query").toString();
71 assertEquals(layers1.replaceAll(" ", ""), map.replaceAll(" ", ""));
72
73 query = "treetagger/pos=\"NN\"";
Joachim Bingeleee549e2014-04-29 11:15:37 +000074 String layers2 = "{@type=korap:token, wrap={@type=korap:term, foundry=treetagger, layer=pos, key=NN, match=match:eq}}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +000075 aqlt = new AqlTree(query);
76 map = aqlt.getRequestMap().get("query").toString();
77 assertEquals(layers2.replaceAll(" ", ""), map.replaceAll(" ", ""));
78 }
79
80 @Test
Joachim Bingel75038792014-05-19 15:12:23 +000081 public void testDirectDeclarationRelations() throws QueryException {
82 query = "node > node";
83 String ddr1 =
84 "{@type=korap:group, operation=operation:relation, operands=[" +
85 "{@type=korap:span}," +
86 "{@type=korap:span}" +
87 "], relation={@type=korap:treeRelation, reltype=dominance}" +
88 "}";
89 aqlt = new AqlTree(query);
90 map = aqlt.getRequestMap().get("query").toString();
91 assertEquals(ddr1.replaceAll(" ", ""), map.replaceAll(" ", ""));
92
93 query = "node > cnx/cat=\"NP\"";
94 String ddr2 =
95 "{@type=korap:group, operation=operation:relation, operands=[" +
96 "{@type=korap:span}," +
97 "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}" +
98 "], relation={@type=korap:treeRelation, reltype=dominance}" +
99 "}";
100 aqlt = new AqlTree(query);
101 map = aqlt.getRequestMap().get("query").toString();
102 assertEquals(ddr2.replaceAll(" ", ""), map.replaceAll(" ", ""));
103
104 }
105
106 @Test
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000107 public void testSimpleDominance() throws QueryException {
108 query = "node & node & #2 > #1";
109 String dom1 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000110 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000111 "{@type=korap:span}," +
112 "{@type=korap:span}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000113 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000114 "}";
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000115 aqlt = new AqlTree(query);
116 map = aqlt.getRequestMap().get("query").toString();
117 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeleee549e2014-04-29 11:15:37 +0000118
119 query = "\"Mann\" & node & #2 > #1";
120 String dom2 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000121 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000122 "{@type=korap:span}," +
123 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000124 "], relation={@type=korap:treeRelation, reltype=dominance}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000125 "}";
126 aqlt = new AqlTree(query);
127 map = aqlt.getRequestMap().get("query").toString();
128 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
129
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000130 query = "\"Mann\" & node & #2 >[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000131 String dom3 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000132 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000133 "{@type=korap:span}," +
134 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000135 "], relation={@type=korap:treeRelation, reltype=dominance, wrap=[{@type=korap:term, layer=cat, key=NP, match=match:eq}]}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000136 "}";
137 aqlt = new AqlTree(query);
138 map = aqlt.getRequestMap().get("query").toString();
139 assertEquals(dom3.replaceAll(" ", ""), map.replaceAll(" ", ""));
140
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000141 query = "\"Mann\" & node & #2 >@l[cat=\"NP\"] #1";
Joachim Bingeleee549e2014-04-29 11:15:37 +0000142 String dom4 =
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000143 "{@type=korap:group, operation=operation:relation, operands=[" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000144 "{@type=korap:span}," +
145 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000146 "], relation={@type=korap:treeRelation, reltype=dominance, index=0, wrap=[{@type=korap:term, layer=cat, key=NP, match=match:eq}]}" +
Joachim Bingeleee549e2014-04-29 11:15:37 +0000147 "}";
148 aqlt = new AqlTree(query);
149 map = aqlt.getRequestMap().get("query").toString();
150 assertEquals(dom4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000151
152 query = "\"Mann\" & node & #2 >2,4 #1";
153 String dom5 =
154 "{@type=korap:group, operation=operation:relation, operands=[" +
155 "{@type=korap:span}," +
156 "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" +
157 "], relation={@type=korap:treeRelation, reltype=dominance, " +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000158 "distance={@type=korap:distance, key=r, min=2, max=4}}" +
Joachim Bingeld4ae5fd2014-04-29 15:00:16 +0000159 "}";
160 aqlt = new AqlTree(query);
161 map = aqlt.getRequestMap().get("query").toString();
162 assertEquals(dom5.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000163 }
164
Joachim Bingel13421ef2014-05-07 16:22:58 +0000165 @Test
166 public void testPointingRelations() throws QueryException {
167 query = "node & node & #2 ->label[coref=\"true\"] #1";
168 String dom1 =
169 "{@type=korap:group, operation=operation:relation, operands=[" +
170 "{@type=korap:span}," +
171 "{@type=korap:span}" +
Joachim Bingela07b8e72014-05-09 15:06:07 +0000172 "], relation={@type=korap:relation, reltype=label, wrap=[{@type=korap:term, layer=coref, key=true, match=match:eq}]}" +
Joachim Bingel13421ef2014-05-07 16:22:58 +0000173 "}";
174 aqlt = new AqlTree(query);
175 map = aqlt.getRequestMap().get("query").toString();
176 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
177
Joachim Bingel639df9c2014-05-12 15:27:00 +0000178 query = "node & node & #2 ->label[mate/coref=\"true\"] #1";
179 String dom2 =
180 "{@type=korap:group, operation=operation:relation, operands=[" +
181 "{@type=korap:span}," +
182 "{@type=korap:span}" +
183 "], relation={@type=korap:relation, reltype=label, wrap=[{@type=korap:term, foundry=mate, layer=coref, key=true, match=match:eq}]}" +
184 "}";
185 aqlt = new AqlTree(query);
186 map = aqlt.getRequestMap().get("query").toString();
187 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000188 }
189
190 @Test
191 public void testSequence() throws QueryException {
192 query = "node & node & #1 . #2";
193 String dom1 =
194 "{@type=korap:group, operation=operation:sequence, " +
195 "operands=[" +
196 "{@type=korap:span}," +
197 "{@type=korap:span}" +
198 "], inOrder=true" +
199 "}";
200 aqlt = new AqlTree(query);
201 map = aqlt.getRequestMap().get("query").toString();
202 assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", ""));
203
204 query = "node & node & #1 .* #2";
205 String dom2 =
Joachim Bingel75038792014-05-19 15:12:23 +0000206 "{@type=korap:group, operation=operation:sequence, operands=[" +
Joachim Bingel13421ef2014-05-07 16:22:58 +0000207 "{@type=korap:span}," +
208 "{@type=korap:span}" +
209 "], distances=[" +
210 "{@type=korap:distance, key=w, min=0, max=100}" +
211 "], inOrder=true" +
212 "}";
213 aqlt = new AqlTree(query);
214 map = aqlt.getRequestMap().get("query").toString();
215 assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel75038792014-05-19 15:12:23 +0000216 }
217
218 @Test
219 public void testPositions() throws QueryException {
220 query = "node & node & #2 _=_ #1";
221 String pos1 =
222 "{@type=korap:group, operation=operation:position, operands=[" +
223 "{@type=korap:span}," +
224 "{@type=korap:span}" +
225 "], frame=frame:matches" +
226 "}";
227 aqlt = new AqlTree(query);
228 map = aqlt.getRequestMap().get("query").toString();
229 assertEquals(pos1.replaceAll(" ", ""), map.replaceAll(" ", ""));
230
231// query = "node & node & #2 _i_ #1";
232// String pos2 =
233// "{@type=korap:group, operation=operation:position, operands=[" +
234// "{@type=korap:span}," +
235// "{@type=korap:span}" +
236// "], frame=frame:contains" +
237// "}";
238// aqlt = new AqlTree(query);
239// map = aqlt.getRequestMap().get("query").toString();
240// assertEquals(pos2.replaceAll(" ", ""), map.replaceAll(" ", ""));
241//
242// query = "node & node & #2 _l_ #1";
243// String pos3 =
244// "{@type=korap:group, operation=operation:position, operands=[" +
245// "{@type=korap:span}," +
246// "{@type=korap:span}" +
247// "], frame=frame:startswith" +
248// "}";
249// aqlt = new AqlTree(query);
250// map = aqlt.getRequestMap().get("query").toString();
251// assertEquals(pos3.replaceAll(" ", ""), map.replaceAll(" ", ""));
252
253 query = "node & \"Mann\" & #2 _r_ #1";
254 String pos4 =
255 "{@type=korap:group, operation=operation:or, operands=[" +
256 "{@type=korap:group, operation=operation:position, operands=[" +
257 "{@type=korap:span}," +
258 "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=Mann, match=match:eq}}" +
259 "], frame=frame:endswith" +
260 "}," +
261 "{@type=korap:group, operation=operation:position, operands=[" +
262 "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=Mann, match=match:eq}}," +
263 "{@type=korap:span}" +
264 "], frame=frame:endswith" +
265 "}" +
266 "]}";
267 aqlt = new AqlTree(query);
268 map = aqlt.getRequestMap().get("query").toString();
269 assertEquals(pos4.replaceAll(" ", ""), map.replaceAll(" ", ""));
Joachim Bingel13421ef2014-05-07 16:22:58 +0000270
271 }
272
Joachim Bingeleee549e2014-04-29 11:15:37 +0000273
Joachim Bingel019ba5c2014-04-28 14:59:04 +0000274}
275