Joachim Bingel | b5f7bf0 | 2014-01-07 16:36:54 +0000 | [diff] [blame] | 1 | import static org.junit.Assert.*; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import de.ids_mannheim.korap.query.serialize.CosmasTree; |
| 6 | import de.ids_mannheim.korap.query.serialize.PoliqarpPlusTree; |
| 7 | import de.ids_mannheim.korap.util.QueryException; |
| 8 | |
| 9 | public 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 Bingel | d5161a1 | 2014-01-08 11:15:49 +0000 | [diff] [blame^] | 72 | 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 Bingel | b5f7bf0 | 2014-01-07 16:36:54 +0000 | [diff] [blame] | 94 | } |
| 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 Bingel | d5161a1 | 2014-01-08 11:15:49 +0000 | [diff] [blame^] | 108 | 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 Bingel | b5f7bf0 | 2014-01-07 16:36:54 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void testOPPROX() { |
| 170 | query="Sonne /+w1:4 Mond"; |
| 171 | String prox1 = |
Joachim Bingel | d5161a1 | 2014-01-08 11:15:49 +0000 | [diff] [blame^] | 172 | "{@type=korap:group, relation=distance, @subtype=incl, measure=w, min=1, max=4, operands=[" + |
Joachim Bingel | b5f7bf0 | 2014-01-07 16:36:54 +0000 | [diff] [blame] | 173 | "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," + |
| 174 | "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" + |
| 175 | "]}"; |
| 176 | ppt = new CosmasTree(query); |
| 177 | map = ppt.getRequestMap().get("query").toString(); |
| 178 | assertEquals(prox1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | public void testOPIN() { |
| 183 | query="wegen #IN(L) <s>"; |
Joachim Bingel | 3a9f793 | 2014-01-07 17:11:31 +0000 | [diff] [blame] | 184 | String opin1 = |
Joachim Bingel | d5161a1 | 2014-01-08 11:15:49 +0000 | [diff] [blame^] | 185 | "{@type=korap:group, relation=include, position=L, operands=[" + |
| 186 | "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," + |
| 187 | "{@type=korap:element, @value=s}" + |
| 188 | "]}"; |
| 189 | ppt = new CosmasTree(query); |
| 190 | map = ppt.getRequestMap().get("query").toString(); |
| 191 | assertEquals(opin1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 192 | |
| 193 | // position argument is optional |
| 194 | query="wegen #IN <s>"; |
| 195 | String opin2 = |
| 196 | "{@type=korap:group, relation=include, operands=[" + |
| 197 | "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," + |
| 198 | "{@type=korap:element, @value=s}" + |
| 199 | "]}"; |
| 200 | ppt = new CosmasTree(query); |
| 201 | map = ppt.getRequestMap().get("query").toString(); |
| 202 | assertEquals(opin2.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 203 | |
| 204 | // parentheses around 'wegen mir' are optional |
| 205 | query="wegen #IN (wegen mir)"; |
| 206 | String opin3 = |
| 207 | "{@type=korap:group, relation=include, operands=[" + |
| 208 | "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," + |
| 209 | "{@type=korap:sequence, operands=[" + |
| 210 | "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," + |
| 211 | "{@type=korap:token, @value={@type=korap:term, @value=orth:mir, relation==}}" + |
| 212 | "]}" + |
| 213 | "]}"; |
| 214 | ppt = new CosmasTree(query); |
| 215 | map = ppt.getRequestMap().get("query").toString(); |
| 216 | assertEquals(opin3.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 217 | } |
| 218 | |
| 219 | @Test |
| 220 | public void testOPOV() { |
| 221 | query="wegen #OV <s>"; |
| 222 | String opin1 = |
| 223 | "{@type=korap:group, relation=overlap, operands=[" + |
Joachim Bingel | b5f7bf0 | 2014-01-07 16:36:54 +0000 | [diff] [blame] | 224 | "{@type=korap:token, @value={@type=korap:term, @value=orth:wegen, relation==}}," + |
| 225 | "{@type=korap:element, @value=s}" + |
| 226 | "]}"; |
| 227 | ppt = new CosmasTree(query); |
| 228 | map = ppt.getRequestMap().get("query").toString(); |
Joachim Bingel | 3a9f793 | 2014-01-07 17:11:31 +0000 | [diff] [blame] | 229 | assertEquals(opin1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Joachim Bingel | b5f7bf0 | 2014-01-07 16:36:54 +0000 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | @Test |
| 233 | public void testOPNOT() { |
Joachim Bingel | 3a9f793 | 2014-01-07 17:11:31 +0000 | [diff] [blame] | 234 | query="Sonne nicht Mond"; |
| 235 | String opnot1 = |
| 236 | "{@type=korap:group, relation=not, operands=[" + |
| 237 | "{@type=korap:token, @value={@type=korap:term, @value=orth:Sonne, relation==}}," + |
| 238 | "{@type=korap:token, @value={@type=korap:term, @value=orth:Mond, relation==}}" + |
| 239 | "]}"; |
| 240 | ppt = new CosmasTree(query); |
| 241 | map = ppt.getRequestMap().get("query").toString(); |
| 242 | assertEquals(opnot1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 243 | } |
| 244 | |
| 245 | @Test |
| 246 | public void testBEG_END() { |
| 247 | // BEG and END operators |
| 248 | // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/links.html |
| 249 | // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/rechts.html |
| 250 | // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/thematische-bsp/bsp-satzlaenge.html |
Joachim Bingel | b5f7bf0 | 2014-01-07 16:36:54 +0000 | [diff] [blame] | 251 | } |
Joachim Bingel | d5161a1 | 2014-01-08 11:15:49 +0000 | [diff] [blame^] | 252 | |
| 253 | |
| 254 | @Test |
| 255 | public void testELEM() { |
| 256 | // http://www.ids-mannheim.de/cosmas2/web-app/hilfe/suchanfrage/eingabe-zeile/syntax/elem.html |
| 257 | } |
| 258 | |
| 259 | @Test |
| 260 | public void testOPALL() { |
| 261 | |
| 262 | } |
| 263 | |
| 264 | @Test |
| 265 | public void testOPNHIT() { |
| 266 | |
| 267 | } |
| 268 | |
| 269 | @Test |
| 270 | public void testOPBED() { |
| 271 | |
| 272 | } |
| 273 | |
| 274 | // TODO |
| 275 | /* |
| 276 | * |
| 277 | */ |
Joachim Bingel | b5f7bf0 | 2014-01-07 16:36:54 +0000 | [diff] [blame] | 278 | } |
| 279 | |