JSON deserialization for queries
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java
new file mode 100644
index 0000000..9917184
--- /dev/null
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java
@@ -0,0 +1,131 @@
+import java.util.*;
+import java.io.*;
+
+import org.apache.lucene.search.spans.SpanQuery;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
+
+import de.ids_mannheim.korap.KorapQuery;
+import de.ids_mannheim.korap.util.QueryException;
+
+import static org.junit.Assert.*;
+import org.junit.Test;
+import org.junit.Ignore;
+import org.junit.runner.RunWith;
+import org.junit.runners.JUnit4;
+
+@RunWith(JUnit4.class)
+public class TestKorapQueryJSON {
+
+ @Ignore
+ public void queryJSONBsp1 () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp1.json").getFile());
+
+ // ([base=foo]|[base=bar])[base=foobar]
+ assertEquals(sqwi.toQuery().toString(), "");
+ };
+
+ @Test
+ public void queryJSONBsp1b () {
+
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp1b.json").getFile());
+
+ // [base=foo]|([base=foo][base=bar]) meta author=Goethe&year=1815
+ assertEquals(sqwi.toQuery().toString(), "spanOr([tokens:base:foo, spanNext(tokens:base:foo, tokens:base:bar)])");
+ };
+
+
+ @Test
+ public void queryJSONBsp2 () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp2.json").getFile());
+
+ // ([base=foo]|[base=bar])[base=foobar]
+ assertEquals(sqwi.toQuery().toString(), "spanNext(spanOr([tokens:base:foo, tokens:base:bar]), tokens:base:foobar)");
+ };
+
+ @Test
+ public void queryJSONBsp3 () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp3.json").getFile());
+
+ // shrink({[base=Mann]})
+ assertEquals(sqwi.toQuery().toString(), "shrink(0: {0: tokens:base:Mann})");
+ };
+
+ @Test
+ public void queryJSONBsp4 () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp4.json").getFile());
+
+ // shrink({[base=foo]}[orth=bar])
+ assertEquals(sqwi.toQuery().toString(), "shrink(0: spanNext({0: tokens:base:foo}, tokens:orth:bar))");
+ };
+
+ @Test
+ public void queryJSONBsp5 () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp5.json").getFile());
+
+ // shrink(1:[base=Der]{1:[base=Mann]})
+ assertEquals(sqwi.toQuery().toString(), "shrink(1: spanNext(tokens:base:Der, {1: tokens:base:Mann}))");
+ };
+
+ @Test
+ public void queryJSONBsp6 () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp6.json").getFile());
+
+ // [base=katze]
+ assertEquals(sqwi.toQuery().toString(), "tokens:base:Katze");
+ };
+
+ @Ignore
+ public void queryJSONBsp7 () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp7.json").getFile());
+
+ // [!base=Katze]
+ assertEquals(sqwi.toQuery().toString(), "");
+ };
+
+ @Ignore
+ public void queryJSONBsp8 () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp8.json").getFile());
+
+ // [!base=Katze]
+ assertEquals(sqwi.toQuery().toString(), "");
+ };
+
+ /*
+ @Test
+ public void queryJSONBsp9 () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp9.json").getFile());
+
+ // [base=Katze&orth=Katzen]
+ assertEquals(sqwi.toQuery().toString(), "");
+ };
+ */
+
+ public static String getString (String path) {
+ StringBuilder contentBuilder = new StringBuilder();
+ try {
+ BufferedReader in = new BufferedReader(new FileReader(path));
+ String str;
+ while ((str = in.readLine()) != null) {
+ contentBuilder.append(str);
+ };
+ in.close();
+ } catch (IOException e) {
+ fail(e.getMessage());
+ }
+ return contentBuilder.toString();
+ };
+
+ public static SpanQueryWrapperInterface jsonQuery (String jsonFile) {
+ SpanQueryWrapperInterface sqwi;
+
+ try {
+ String json = getString(jsonFile);
+ sqwi = new KorapQuery("tokens").fromJSON(json);
+ }
+ catch (QueryException e) {
+ fail(e.getMessage());
+ sqwi = new KorapQuery("tokens").seg("???");
+ };
+ return sqwi;
+ };
+};
\ No newline at end of file
diff --git a/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java b/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
index 705bf33..0d62975 100644
--- a/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
+++ b/src/test/java/de/ids_mannheim/korap/search/TestKorapSearch.java
@@ -90,29 +90,4 @@
assertEquals(6, kr.totalResults());
assertEquals(kr.getMatch(0).getSnippetBrackets(), "... dem [Buchstaben] A ...");
};
-
- @Test
- public void searchJSON () throws IOException {
-
- String jsonFile = getClass().getResource("/queries/bsp1.json").getFile();
-
- KorapSearch ks = new KorapSearch(getString(jsonFile));
-
- // assertEquals(ks.getQuery().toString(), "");
- };
-
- public static String getString (String path) {
- StringBuilder contentBuilder = new StringBuilder();
- try {
- BufferedReader in = new BufferedReader(new FileReader(path));
- String str;
- while ((str = in.readLine()) != null) {
- contentBuilder.append(str);
- };
- in.close();
- } catch (IOException e) {
- }
- return contentBuilder.toString();
- };
-
};
diff --git a/src/test/resources/queries/readme.txt b/src/test/resources/queries/readme.txt
index 5cfd658..36745ce 100644
--- a/src/test/resources/queries/readme.txt
+++ b/src/test/resources/queries/readme.txt
@@ -1,4 +1,4 @@
-bsp1.json: [base=foo]|([base=foo][base=bar]) meta author=Goethe&year=1815
+bsp1.json: [base=foo]|([base=foo][base=bar])* meta author=Goethe&year=1815
bsp2.json: ([base=foo]|[base=bar])[base=foobar]
bsp3.json: shrink({[base=Mann]})
bsp4.json: shrink({[base=foo]}[orth=bar])