Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 1 | import static org.junit.Assert.*; |
| 2 | |
| 3 | import org.junit.Test; |
| 4 | |
| 5 | import de.ids_mannheim.korap.query.serialize.AqlTree; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 6 | import de.ids_mannheim.korap.util.QueryException; |
| 7 | |
| 8 | public 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 Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 32 | String token1 = "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 33 | assertTrue(equalsQueryContent(token1, query)); |
| 34 | |
| 35 | // [orth!=Frau] |
| 36 | query = "tok!=\"Frau\""; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 37 | String token2 = "{@type=korap:token, wrap={@type=korap:term, key=Frau, match=match:ne}}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 38 | 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 Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 59 | String regex1 = "{@type=korap:token, wrap={@type=korap:term, type=type:regex, key=Mann, match=match:eq}}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 60 | 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 Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 68 | String layers1 = "{@type=korap:span, foundry=cnx, layer=cat, key=NP, match=match:eq}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 69 | aqlt = new AqlTree(query); |
| 70 | map = aqlt.getRequestMap().get("query").toString(); |
| 71 | assertEquals(layers1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 72 | |
| 73 | query = "treetagger/pos=\"NN\""; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 74 | String layers2 = "{@type=korap:token, wrap={@type=korap:term, foundry=treetagger, layer=pos, key=NN, match=match:eq}}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 75 | aqlt = new AqlTree(query); |
| 76 | map = aqlt.getRequestMap().get("query").toString(); |
| 77 | assertEquals(layers2.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void testSimpleDominance() throws QueryException { |
| 82 | query = "node & node & #2 > #1"; |
| 83 | String dom1 = |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 84 | "{@type=korap:group, operation=operation:relation, operands=[" + |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 85 | "{@type=korap:span}," + |
| 86 | "{@type=korap:span}" + |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 87 | "], relation={@type=korap:treeRelation, reltype=dominance}" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 88 | "}"; |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 89 | aqlt = new AqlTree(query); |
| 90 | map = aqlt.getRequestMap().get("query").toString(); |
| 91 | assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 92 | |
| 93 | query = "\"Mann\" & node & #2 > #1"; |
| 94 | String dom2 = |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 95 | "{@type=korap:group, operation=operation:relation, operands=[" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 96 | "{@type=korap:span}," + |
| 97 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" + |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 98 | "], relation={@type=korap:treeRelation, reltype=dominance}" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 99 | "}"; |
| 100 | aqlt = new AqlTree(query); |
| 101 | map = aqlt.getRequestMap().get("query").toString(); |
| 102 | assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 103 | |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 104 | query = "\"Mann\" & node & #2 >[cat=\"NP\"] #1"; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 105 | String dom3 = |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 106 | "{@type=korap:group, operation=operation:relation, operands=[" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 107 | "{@type=korap:span}," + |
| 108 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" + |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 109 | "], relation={@type=korap:treeRelation, reltype=dominance, edges=[{@type=korap:edge, label=cat, key=NP, match=match:eq}]}" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 110 | "}"; |
| 111 | aqlt = new AqlTree(query); |
| 112 | map = aqlt.getRequestMap().get("query").toString(); |
| 113 | assertEquals(dom3.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 114 | |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 115 | query = "\"Mann\" & node & #2 >@l[cat=\"NP\"] #1"; |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 116 | String dom4 = |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 117 | "{@type=korap:group, operation=operation:relation, operands=[" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 118 | "{@type=korap:span}," + |
| 119 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" + |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 120 | "], relation={@type=korap:treeRelation, reltype=dominance, index=0, edges=[{@type=korap:edge, label=cat, key=NP, match=match:eq}]}" + |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 121 | "}"; |
| 122 | aqlt = new AqlTree(query); |
| 123 | map = aqlt.getRequestMap().get("query").toString(); |
| 124 | assertEquals(dom4.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Joachim Bingel | d4ae5fd | 2014-04-29 15:00:16 +0000 | [diff] [blame] | 125 | |
| 126 | query = "\"Mann\" & node & #2 >2,4 #1"; |
| 127 | String dom5 = |
| 128 | "{@type=korap:group, operation=operation:relation, operands=[" + |
| 129 | "{@type=korap:span}," + |
| 130 | "{@type=korap:token, wrap={@type=korap:term, key=Mann, match=match:eq}}" + |
| 131 | "], relation={@type=korap:treeRelation, reltype=dominance, " + |
| 132 | "boundary={@type=korap:boundary, min=2, max=4}}" + |
| 133 | "}"; |
| 134 | aqlt = new AqlTree(query); |
| 135 | map = aqlt.getRequestMap().get("query").toString(); |
| 136 | assertEquals(dom5.replaceAll(" ", ""), map.replaceAll(" ", "")); |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Joachim Bingel | 13421ef | 2014-05-07 16:22:58 +0000 | [diff] [blame^] | 139 | @Test |
| 140 | public void testPointingRelations() throws QueryException { |
| 141 | query = "node & node & #2 ->label[coref=\"true\"] #1"; |
| 142 | String dom1 = |
| 143 | "{@type=korap:group, operation=operation:relation, operands=[" + |
| 144 | "{@type=korap:span}," + |
| 145 | "{@type=korap:span}" + |
| 146 | "], relation={@type=korap:relation, layer=coref, label=label, key=true}" + |
| 147 | "}"; |
| 148 | aqlt = new AqlTree(query); |
| 149 | map = aqlt.getRequestMap().get("query").toString(); |
| 150 | assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 151 | |
| 152 | } |
| 153 | |
| 154 | @Test |
| 155 | public void testSequence() throws QueryException { |
| 156 | query = "node & node & #1 . #2"; |
| 157 | String dom1 = |
| 158 | "{@type=korap:group, operation=operation:sequence, " + |
| 159 | "operands=[" + |
| 160 | "{@type=korap:span}," + |
| 161 | "{@type=korap:span}" + |
| 162 | "], inOrder=true" + |
| 163 | "}"; |
| 164 | aqlt = new AqlTree(query); |
| 165 | map = aqlt.getRequestMap().get("query").toString(); |
| 166 | assertEquals(dom1.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 167 | |
| 168 | query = "node & node & #1 .* #2"; |
| 169 | String dom2 = |
| 170 | "{@type=korap:group, operation=operation:sequence, " + |
| 171 | "operands=[" + |
| 172 | "{@type=korap:span}," + |
| 173 | "{@type=korap:span}" + |
| 174 | "], distances=[" + |
| 175 | "{@type=korap:distance, key=w, min=0, max=100}" + |
| 176 | "], inOrder=true" + |
| 177 | "}"; |
| 178 | aqlt = new AqlTree(query); |
| 179 | map = aqlt.getRequestMap().get("query").toString(); |
| 180 | assertEquals(dom2.replaceAll(" ", ""), map.replaceAll(" ", "")); |
| 181 | |
| 182 | } |
| 183 | |
Joachim Bingel | eee549e | 2014-04-29 11:15:37 +0000 | [diff] [blame] | 184 | |
Joachim Bingel | 019ba5c | 2014-04-28 14:59:04 +0000 | [diff] [blame] | 185 | } |
| 186 | |