Prepare SpanQueryWrapper for empty token support
diff --git a/src/main/java/de/ids_mannheim/korap/KorapQuery.java b/src/main/java/de/ids_mannheim/korap/KorapQuery.java
index e693b48..f07ce51 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapQuery.java
@@ -102,7 +102,7 @@
};
};
- public SpanQueryWrapperInterface fromJSON (String jsonString) throws QueryException {
+ public SpanQueryWrapper fromJSON (String jsonString) throws QueryException {
JsonNode json;
try {
json = this.json.readValue(jsonString, JsonNode.class);
@@ -122,7 +122,7 @@
// TODO: Use the shortcuts implemented in this class instead of the wrapper constructors
// TODO: Check for isArray()
// TODO: Rename this span context!
- public SpanQueryWrapperInterface fromJSON (JsonNode json) throws QueryException {
+ public SpanQueryWrapper fromJSON (JsonNode json) throws QueryException {
int number = 0;
@@ -374,6 +374,7 @@
if (min > max)
max = max;
+ // This may be an empty repetitor
return new SpanRepetitionQueryWrapper(
this.fromJSON(operands.get(0)), min, max
);
@@ -420,8 +421,9 @@
);
case "korap:token":
+
if (!json.has("wrap"))
- throw new QueryException("Empty Tokens are not supported yet");
+ return new SpanEmptyTokenWrapper();
return this._segFromJSON(json.get("wrap"));
@@ -436,7 +438,7 @@
- private SpanQueryWrapperInterface _segFromJSON (JsonNode json) throws QueryException {
+ private SpanQueryWrapper _segFromJSON (JsonNode json) throws QueryException {
String type = json.get("@type").asText();
if (DEBUG)
@@ -477,7 +479,7 @@
case "relation:and":
for (JsonNode operand : json.get("operands")) {
- SpanQueryWrapperInterface part = this._segFromJSON(operand);
+ SpanQueryWrapper part = this._segFromJSON(operand);
if (part instanceof SpanAlterQueryWrapper) {
ssegqw.with((SpanAlterQueryWrapper) part);
}
@@ -506,7 +508,7 @@
- private SpanQueryWrapperInterface _termFromJSON (JsonNode json) throws QueryException {
+ private SpanQueryWrapper _termFromJSON (JsonNode json) throws QueryException {
if (!json.has("key") || json.get("key").asText().length() < 1)
throw new QueryException("Terms and spans have to provide key attributes");
@@ -681,9 +683,9 @@
* Create a segment alternation query object.
* @param terms[] An array of alternative terms.
*/
- public SpanAlterQueryWrapper or (SpanQueryWrapperInterface ... terms) {
+ public SpanAlterQueryWrapper or (SpanQueryWrapper ... terms) {
SpanAlterQueryWrapper ssaq = new SpanAlterQueryWrapper(this.field);
- for (SpanQueryWrapperInterface t : terms) {
+ for (SpanQueryWrapper t : terms) {
ssaq.or(t);
};
return ssaq;
@@ -711,9 +713,9 @@
* Create a sequence of segments query object.
* @param terms[] An array of segment defining terms.
*/
- public SpanSequenceQueryWrapper seq (SpanQueryWrapperInterface ... terms) {
+ public SpanSequenceQueryWrapper seq (SpanQueryWrapper ... terms) {
SpanSequenceQueryWrapper sssq = new SpanSequenceQueryWrapper(this.field);
- for (SpanQueryWrapperInterface t : terms) {
+ for (SpanQueryWrapper t : terms) {
sssq.append(t);
};
return sssq;
@@ -732,8 +734,8 @@
public SpanSequenceQueryWrapper seq (Object ... terms) {
SpanSequenceQueryWrapper ssq = new SpanSequenceQueryWrapper(this.field);
for (Object t : terms) {
- if (t instanceof SpanQueryWrapperInterface) {
- ssq.append((SpanQueryWrapperInterface) t);
+ if (t instanceof SpanQueryWrapper) {
+ ssq.append((SpanQueryWrapper) t);
}
else if (t instanceof SpanRegexQueryWrapper) {
ssq.append((SpanRegexQueryWrapper) t);
@@ -756,79 +758,78 @@
* @param embedded A SpanQuery that is wrapped in the element.
*/
@Deprecated
- public SpanWithinQueryWrapper within (SpanQueryWrapperInterface element,
- SpanQueryWrapperInterface embedded) {
+ public SpanWithinQueryWrapper within (SpanQueryWrapper element,
+ SpanQueryWrapper embedded) {
return new SpanWithinQueryWrapper(element, embedded);
};
- public SpanWithinQueryWrapper contains (SpanQueryWrapperInterface element,
- SpanQueryWrapperInterface embedded) {
+ public SpanWithinQueryWrapper contains (SpanQueryWrapper element,
+ SpanQueryWrapper embedded) {
return new SpanWithinQueryWrapper(element, embedded, WITHIN);
};
- public SpanWithinQueryWrapper startswith (SpanQueryWrapperInterface element,
- SpanQueryWrapperInterface embedded) {
+ public SpanWithinQueryWrapper startswith (SpanQueryWrapper element,
+ SpanQueryWrapper embedded) {
return new SpanWithinQueryWrapper(element, embedded, STARTSWITH);
};
- public SpanWithinQueryWrapper endswith (SpanQueryWrapperInterface element,
- SpanQueryWrapperInterface embedded) {
+ public SpanWithinQueryWrapper endswith (SpanQueryWrapper element,
+ SpanQueryWrapper embedded) {
return new SpanWithinQueryWrapper(element, embedded, ENDSWITH);
};
- public SpanWithinQueryWrapper overlaps (SpanQueryWrapperInterface element,
- SpanQueryWrapperInterface embedded) {
+ public SpanWithinQueryWrapper overlaps (SpanQueryWrapper element,
+ SpanQueryWrapper embedded) {
return new SpanWithinQueryWrapper(element, embedded, OVERLAP);
};
- public SpanWithinQueryWrapper matches (SpanQueryWrapperInterface element,
- SpanQueryWrapperInterface embedded) {
+ public SpanWithinQueryWrapper matches (SpanQueryWrapper element,
+ SpanQueryWrapper embedded) {
return new SpanWithinQueryWrapper(element, embedded, MATCH);
};
// Class
- public SpanClassQueryWrapper _ (byte number, SpanQueryWrapperInterface element) {
+ public SpanClassQueryWrapper _ (byte number, SpanQueryWrapper element) {
return new SpanClassQueryWrapper(element, number);
};
- public SpanClassQueryWrapper _ (int number, SpanQueryWrapperInterface element) {
+ public SpanClassQueryWrapper _ (int number, SpanQueryWrapper element) {
return new SpanClassQueryWrapper(element, number);
};
- public SpanClassQueryWrapper _ (short number, SpanQueryWrapperInterface element) {
+ public SpanClassQueryWrapper _ (short number, SpanQueryWrapper element) {
return new SpanClassQueryWrapper(element, number);
};
- public SpanClassQueryWrapper _ (SpanQueryWrapperInterface element) {
+ public SpanClassQueryWrapper _ (SpanQueryWrapper element) {
return new SpanClassQueryWrapper(element);
};
// MatchModify
- public SpanMatchModifyQueryWrapper shrink (byte number, SpanQueryWrapperInterface element) {
+ public SpanMatchModifyQueryWrapper shrink (byte number, SpanQueryWrapper element) {
return new SpanMatchModifyQueryWrapper(element, number);
};
- public SpanMatchModifyQueryWrapper shrink (int number, SpanQueryWrapperInterface element) {
+ public SpanMatchModifyQueryWrapper shrink (int number, SpanQueryWrapper element) {
return new SpanMatchModifyQueryWrapper(element, number);
};
- public SpanMatchModifyQueryWrapper shrink (short number, SpanQueryWrapperInterface element) {
+ public SpanMatchModifyQueryWrapper shrink (short number, SpanQueryWrapper element) {
return new SpanMatchModifyQueryWrapper(element, number);
};
- public SpanMatchModifyQueryWrapper shrink (SpanQueryWrapperInterface element) {
+ public SpanMatchModifyQueryWrapper shrink (SpanQueryWrapper element) {
return new SpanMatchModifyQueryWrapper(element);
};
// Repetition
- public SpanRepetitionQueryWrapper repeat (SpanQueryWrapperInterface element, int exact) {
+ public SpanRepetitionQueryWrapper repeat (SpanQueryWrapper element, int exact) {
return new SpanRepetitionQueryWrapper(element, exact);
};
- public SpanRepetitionQueryWrapper repeat (SpanQueryWrapperInterface element, int min, int max) {
+ public SpanRepetitionQueryWrapper repeat (SpanQueryWrapper element, int min, int max) {
return new SpanRepetitionQueryWrapper(element, min, max);
};
-
// split
};
diff --git a/src/main/java/de/ids_mannheim/korap/KorapSearch.java b/src/main/java/de/ids_mannheim/korap/KorapSearch.java
index 6d14e7e..bdb059d 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapSearch.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapSearch.java
@@ -3,7 +3,7 @@
import java.io.*;
import org.apache.lucene.search.spans.SpanQuery;
-import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import de.ids_mannheim.korap.KorapCollection;
import de.ids_mannheim.korap.KorapIndex;
import de.ids_mannheim.korap.KorapResult;
@@ -54,7 +54,7 @@
// "query" value
if (this.request.has("query")) {
try {
- SpanQueryWrapperInterface queryIface = new KorapQuery("tokens").fromJSON(this.request.get("query"));
+ SpanQueryWrapper queryIface = new KorapQuery("tokens").fromJSON(this.request.get("query"));
this.query = queryIface.toQuery();
if (queryIface.isOptional())
@@ -116,7 +116,7 @@
};
// Maybe accept queryWrapperStuff
- public KorapSearch (SpanQueryWrapperInterface sqwi) {
+ public KorapSearch (SpanQueryWrapper sqwi) {
this.query = sqwi.toQuery();
};
@@ -150,7 +150,7 @@
return this.request;
};
- public KorapSearch setQuery (SpanQueryWrapperInterface sqwi) {
+ public KorapSearch setQuery (SpanQueryWrapper sqwi) {
this.query = sqwi.toQuery();
return this;
};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java
index 7f6f394..2a04554 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanAlterQueryWrapper.java
@@ -3,7 +3,7 @@
import de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper;
import de.ids_mannheim.korap.query.wrap.SpanWildcardQueryWrapper;
import de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper;
-import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanTermQuery;
@@ -12,13 +12,10 @@
import java.util.*;
-public class SpanAlterQueryWrapper implements SpanQueryWrapperInterface {
+public class SpanAlterQueryWrapper extends SpanQueryWrapper {
private String field;
private SpanQuery query;
private List<SpanQuery> alternatives;
- private boolean isNull = true;
- private boolean isOptional = false;
- private boolean isNegative = false;
public SpanAlterQueryWrapper (String field) {
this.field = field;
@@ -50,7 +47,7 @@
return this;
};
- public SpanAlterQueryWrapper or (SpanQueryWrapperInterface term) {
+ public SpanAlterQueryWrapper or (SpanQueryWrapper term) {
if (term.isNull())
return this;
@@ -94,16 +91,4 @@
};
return (SpanQuery) soquery;
};
-
- public boolean isOptional () {
- return this.isOptional;
- };
-
- public boolean isNull () {
- return this.isNull;
- };
-
- public boolean isNegative () {
- return this.isNegative;
- };
};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java
index bb43edc..865c61e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanClassQueryWrapper.java
@@ -3,31 +3,31 @@
import org.apache.lucene.search.spans.SpanQuery;
import de.ids_mannheim.korap.query.SpanClassQuery;
-import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import java.util.*;
-public class SpanClassQueryWrapper implements SpanQueryWrapperInterface {
- private SpanQueryWrapperInterface subquery;
+public class SpanClassQueryWrapper extends SpanQueryWrapper {
+ private SpanQueryWrapper subquery;
private byte number = (byte) 0;
- public SpanClassQueryWrapper (SpanQueryWrapperInterface subquery, byte number) {
+ public SpanClassQueryWrapper (SpanQueryWrapper subquery, byte number) {
this.subquery = subquery;
this.number = number;
};
- public SpanClassQueryWrapper (SpanQueryWrapperInterface subquery, short number) {
+ public SpanClassQueryWrapper (SpanQueryWrapper subquery, short number) {
this.subquery = subquery;
this.number = (byte) number;
};
- public SpanClassQueryWrapper (SpanQueryWrapperInterface subquery, int number) {
+ public SpanClassQueryWrapper (SpanQueryWrapper subquery, int number) {
this.subquery = subquery;
this.number = (byte) number;
};
- public SpanClassQueryWrapper (SpanQueryWrapperInterface subquery) {
+ public SpanClassQueryWrapper (SpanQueryWrapper subquery) {
this.subquery = subquery;
this.number = (byte) 0;
};
@@ -35,6 +35,9 @@
public SpanQuery toQuery () {
if (this.subquery.isNull())
return (SpanQuery) null;
+
+ // TODO: If this.subquery.isNegative(), it may be an Expansion!
+ // SpanExpansionQuery(x, y.negative, min, max. direction???, classNumber, true)
if (this.number == (byte) 0) {
return new SpanClassQuery((SpanQuery) this.subquery.toQuery());
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java
index 4add693..92a7090 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanElementQueryWrapper.java
@@ -3,9 +3,9 @@
import org.apache.lucene.search.spans.SpanQuery;
import de.ids_mannheim.korap.query.SpanElementQuery;
-import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
-public class SpanElementQueryWrapper implements SpanQueryWrapperInterface {
+public class SpanElementQueryWrapper extends SpanQueryWrapper {
String element;
String field;
@@ -18,15 +18,7 @@
return (SpanQuery) new SpanElementQuery(this.field, this.element);
};
- public boolean isOptional () {
- return false;
- };
-
public boolean isNull () {
return false;
};
-
- public boolean isNegative () {
- return false;
- };
};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanEmptyTokenWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanEmptyTokenWrapper.java
new file mode 100644
index 0000000..a4691f0
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanEmptyTokenWrapper.java
@@ -0,0 +1,12 @@
+package de.ids_mannheim.korap.query.wrap;
+
+import org.apache.lucene.search.spans.SpanQuery;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
+
+import java.util.*;
+
+/**
+ * Implement an empty token
+ */
+public class SpanEmptyTokenWrapper extends SpanQueryWrapper {
+};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanMatchModifyQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanMatchModifyQueryWrapper.java
index 252ee0a..8e9a812 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanMatchModifyQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanMatchModifyQueryWrapper.java
@@ -3,31 +3,31 @@
import org.apache.lucene.search.spans.SpanQuery;
import de.ids_mannheim.korap.query.SpanMatchModifyClassQuery;
-import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import java.util.*;
-public class SpanMatchModifyQueryWrapper implements SpanQueryWrapperInterface {
- private SpanQueryWrapperInterface subquery;
+public class SpanMatchModifyQueryWrapper extends SpanQueryWrapper {
+ private SpanQueryWrapper subquery;
private byte number;
- public SpanMatchModifyQueryWrapper (SpanQueryWrapperInterface subquery, byte number) {
+ public SpanMatchModifyQueryWrapper (SpanQueryWrapper subquery, byte number) {
this.subquery = subquery;
this.number = number;
};
- public SpanMatchModifyQueryWrapper (SpanQueryWrapperInterface subquery, short number) {
+ public SpanMatchModifyQueryWrapper (SpanQueryWrapper subquery, short number) {
this.subquery = subquery;
this.number = (byte) number;
};
- public SpanMatchModifyQueryWrapper (SpanQueryWrapperInterface subquery, int number) {
+ public SpanMatchModifyQueryWrapper (SpanQueryWrapper subquery, int number) {
this.subquery = subquery;
this.number = (byte) number;
};
- public SpanMatchModifyQueryWrapper (SpanQueryWrapperInterface subquery) {
+ public SpanMatchModifyQueryWrapper (SpanQueryWrapper subquery) {
this.subquery = subquery;
this.number = (byte) 0;
};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQuantifierQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQuantifierQueryWrapper.java
index f6d3718..1926310 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQuantifierQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQuantifierQueryWrapper.java
@@ -5,12 +5,12 @@
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
-import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import org.apache.lucene.search.spans.SpanQuery;
// This might be irrelevant now with repetition!
-public class SpanQuantifierQueryWrapper implements SpanQueryWrapperInterface {
+public class SpanQuantifierQueryWrapper extends SpanQueryWrapper {
private String field;
public SpanQuantifierQueryWrapper (String field) {
@@ -21,19 +21,10 @@
return (SpanQuery) null;
};
- public boolean isOptional () {
- return false;
- };
-
public boolean isNull () {
return false;
};
- public boolean isNegative () {
- return false;
- };
-
-
/*
Only support spans with minimal one occurrence and then
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java
new file mode 100644
index 0000000..30fe7b9
--- /dev/null
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapper.java
@@ -0,0 +1,56 @@
+package de.ids_mannheim.korap.query.wrap;
+
+import org.apache.lucene.search.spans.SpanQuery;
+
+// TODO: Add warning and error
+
+/**
+ * A wrapper class for Lucene Spanqueries that add certain information
+ * to the queries, necessary for correct deserialization of nested queries.
+ *
+ * @author Nils Diewald
+ */
+public class SpanQueryWrapper {
+ protected boolean isNull = true,
+ isOptional = false,
+ isNegative = false;
+ protected int min = 1,
+ max = 1;
+
+ // Serialize query to Lucene SpanQuery
+ public SpanQuery toQuery () {
+ return (SpanQuery) null;
+ };
+
+ // The subquery is not necessary, like in
+ // "the [pos=ADJ]? tree"
+ // The adjective can be there, but it's not necessary
+ public boolean isOptional () {
+ return this.isOptional;
+ };
+
+ // The subquery won't match anything at all,
+ // like in
+ // "the [pos=ADJ]{0} tree"
+ public boolean isNull () {
+ return this.isNull;
+ };
+
+ // The subquery should match if the condition does not hold true like in
+ // "the [base!=tree]"
+ public boolean isNegative () {
+ return this.isNegative;
+ };
+
+ // Repetition queries may be more specific regarding repetition
+ // This is a minimum repetition value
+ public int min () {
+ return this.min;
+ };
+
+ // Repetition queries may be more specific regarding repetition
+ // This is a maximum repetition value
+ public int max () {
+ return this.max;
+ };
+};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapperInterface.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapperInterface.java
deleted file mode 100644
index 9a24a50..0000000
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanQueryWrapperInterface.java
+++ /dev/null
@@ -1,14 +0,0 @@
-package de.ids_mannheim.korap.query.wrap;
-
-import org.apache.lucene.search.spans.SpanQuery;
-
-// Todo: Make this an abstract class to deal with regexes
-// in a parent abstract class!
-// Add warning and error
-
-public interface SpanQueryWrapperInterface {
- public SpanQuery toQuery ();
- public boolean isOptional ();
- public boolean isNull ();
- public boolean isNegative ();
-};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
index 8be32cc..d39a960 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRegexQueryWrapper.java
@@ -5,6 +5,7 @@
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
import org.apache.lucene.util.automaton.RegExp;
import org.apache.lucene.index.Term;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import java.util.*;
@@ -12,7 +13,7 @@
TODO: Don't allow queries like ".*?"!!!
*/
-public class SpanRegexQueryWrapper {
+public class SpanRegexQueryWrapper extends SpanQueryWrapper {
private SpanQuery query;
public SpanRegexQueryWrapper (String field, String re) {
@@ -43,15 +44,7 @@
return this.query;
};
- public boolean isOptional () {
- return false;
- };
-
public boolean isNull () {
return false;
};
-
- public boolean isNegative () {
- return false;
- };
};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
index 3ad592d..b46d929 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanRepetitionQueryWrapper.java
@@ -3,22 +3,21 @@
import org.apache.lucene.search.spans.SpanQuery;
import de.ids_mannheim.korap.query.SpanRepetitionQuery;
-import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
-public class SpanRepetitionQueryWrapper implements SpanQueryWrapperInterface {
- private SpanQueryWrapperInterface subquery;
- private int min = 1;
- private int max = 1;
- private boolean isOptional = false;
- private boolean isNull = false;
+public class SpanRepetitionQueryWrapper extends SpanQueryWrapper {
+ private SpanQueryWrapper subquery;
- public SpanRepetitionQueryWrapper (SpanQueryWrapperInterface subquery, int exact) {
+ // This is for exact enumbered repetition, like in a{3}
+ public SpanRepetitionQueryWrapper (SpanQueryWrapper subquery, int exact) {
this.subquery = subquery;
if (exact < 1 || this.subquery.isNull()) {
this.isNull = true;
this.isOptional = true;
+ this.min = 0;
+ this.max = 0;
return;
};
@@ -26,12 +25,18 @@
this.max = exact;
};
- public SpanRepetitionQueryWrapper (SpanQueryWrapperInterface subquery, int min, int max) {
+ // This is for a range of repetitions, like in a{2,3}, a{,4}, a{3,}, a+, a*, a?
+ public SpanRepetitionQueryWrapper (SpanQueryWrapper subquery, int min, int max) {
+
this.subquery = subquery;
+ // Subquery may be an empty token
if (this.subquery.isNull()) {
this.isNull = true;
return;
+ }
+ else {
+ this.isNull = false;
};
if (min == 0) {
@@ -40,24 +45,30 @@
if (max == 0)
this.isNull = true;
};
+
this.min = min;
this.max = max;
};
+
+ // Serialize to Lucene SpanQuery
public SpanQuery toQuery () {
+
+ // The query is null
if (this.isNull)
return (SpanQuery) null;
+
+ // The query is not a repetition query at all, but may be optional
if (this.min == 1 && this.max == 1)
return this.subquery.toQuery();
- return new SpanRepetitionQuery(this.subquery.toQuery(), this.min, this.max, true);
- };
- public boolean isOptional () {
- return this.isOptional;
- };
-
- public boolean isNull () {
- return this.isNull;
+ // That's a fine repetition query
+ return new SpanRepetitionQuery(
+ this.subquery.toQuery(),
+ this.min,
+ this.max,
+ true
+ );
};
public boolean isNegative () {
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java
index e72c566..21032a9 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSegmentQueryWrapper.java
@@ -8,6 +8,7 @@
import org.apache.lucene.search.spans.SpanTermQuery;
import org.apache.lucene.search.spans.SpanNotQuery;
import org.apache.lucene.search.spans.SpanOrQuery;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper;
import de.ids_mannheim.korap.query.wrap.SpanWildcardQueryWrapper;
import de.ids_mannheim.korap.query.SpanSegmentQuery;
@@ -21,12 +22,10 @@
* regular expressions and alternatives. These elements can also be excluded.
*/
-public class SpanSegmentQueryWrapper implements SpanQueryWrapperInterface {
+public class SpanSegmentQueryWrapper extends SpanQueryWrapper {
public ArrayList<SpanQuery> inclusive;
public ArrayList<SpanQuery> exclusive;
private String field;
- private boolean isNull = true;
- private boolean isNegative = false;
/**
* Constructor.
@@ -220,10 +219,6 @@
return false;
};
- public boolean isNull () {
- return this.isNull;
- };
-
public boolean isNegative () {
if (this.inclusive.size() == 0 && this.exclusive.size() >= 1) {
return true;
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
index b316fdf..2afb15b 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanSequenceQueryWrapper.java
@@ -5,8 +5,10 @@
import de.ids_mannheim.korap.query.SpanElementQuery;
import de.ids_mannheim.korap.query.SpanNextQuery;
import de.ids_mannheim.korap.query.SpanDistanceQuery;
+import de.ids_mannheim.korap.query.SpanExpansionQuery;
import de.ids_mannheim.korap.query.SpanMultipleDistanceQuery;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper;
import de.ids_mannheim.korap.query.wrap.SpanAlterQueryWrapper;
import de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper;
@@ -15,7 +17,6 @@
import org.apache.lucene.index.Term;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanTermQuery;
-import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -26,9 +27,11 @@
/**
+ * Deserialize complexe sequence queries to Lucene SpanQueries.
+ *
* @author Nils Diewald
*/
-public class SpanSequenceQueryWrapper implements SpanQueryWrapperInterface {
+public class SpanSequenceQueryWrapper extends SpanQueryWrapper {
private String field;
private ArrayList<SpanQuery> segments;
private ArrayList<DistanceConstraint> constraints;
@@ -37,15 +40,13 @@
private final static Logger log = LoggerFactory.getLogger(SpanSequenceQueryWrapper.class);
// This advices the java compiler to ignore all loggings
- public static final boolean DEBUG = false;
+ public static final boolean DEBUG = true;
private boolean
isInOrder = true,
- isNull = true,
isOptional = true,
lastIsOptional = false,
- firstIsOptional = false,
- isNegative = false;
+ firstIsOptional = false;
public SpanSequenceQueryWrapper (String field) {
this.field = field;
@@ -70,7 +71,7 @@
this.isNull = false;
};
- public SpanSequenceQueryWrapper (String field, SpanQueryWrapperInterface sswq) {
+ public SpanSequenceQueryWrapper (String field, SpanQueryWrapper sswq) {
this(field);
if (!sswq.isNull()) {
@@ -141,6 +142,7 @@
SpanAlterQueryWrapper saqw = new SpanAlterQueryWrapper(field, query);
SpanSequenceQueryWrapper ssqw = new SpanSequenceQueryWrapper(field, query);
+
// Remove last element of the list and prepend it
ssqw.prepend(this.segments.remove(this.segments.size() - 1));
saqw.or(ssqw);
@@ -164,22 +166,30 @@
return this;
};
- public SpanSequenceQueryWrapper append (SpanQueryWrapperInterface ssq) {
+ public SpanSequenceQueryWrapper append (SpanQueryWrapper ssq) {
+
+ if (DEBUG)
+ log.trace("Try to append query {}", ssq.toString());
+
if (!ssq.isNull()) {
+ if (DEBUG)
+ log.trace("THe query {} is not null", ssq.toString());
+
if (ssq.isNegative())
this.isNegative = true;
SpanQuery appendQuery = ssq.toQuery();
if (!ssq.isOptional()) {
if (DEBUG)
- log.trace("Append non-opt query to non-opt query " +
+ log.trace("Append non-opt query to non-opt query {}",
appendQuery.toString());
return this.append(appendQuery);
};
// Situation is ab? or a?b?
if (this.segments.size() != 0) {
+
// Remove last element of the list and prepend it
SpanQuery lastQuery = this.segments.remove(this.segments.size() - 1);
SpanAlterQueryWrapper saqw = new SpanAlterQueryWrapper(field, lastQuery);
@@ -191,12 +201,12 @@
saqw.or(appendQuery);
// last stays optional
if (DEBUG)
- log.trace("Append opt query to opt query " +
+ log.trace("Append opt query to opt query {}",
appendQuery.toString());
}
else if (DEBUG) {
- log.trace("Append opt query to non-opt query " +
+ log.trace("Append opt query to non-opt query {}",
appendQuery.toString());
};
@@ -209,7 +219,7 @@
this.segments.add(appendQuery);
if (DEBUG)
- log.trace("Append opt query " +
+ log.trace("Append opt query {}",
appendQuery.toString());
// Update boundary optionality
@@ -265,7 +275,7 @@
return this;
};
- public SpanSequenceQueryWrapper prepend (SpanQueryWrapperInterface ssq) {
+ public SpanSequenceQueryWrapper prepend (SpanQueryWrapper ssq) {
if (!ssq.isNull()) {
if (ssq.isNegative())
@@ -436,16 +446,4 @@
return false;
return true;
};
-
- public boolean isOptional () {
- return this.isOptional;
- };
-
- public boolean isNull () {
- return this.isNull;
- };
-
- public boolean isNegative () {
- return this.isNegative;
- };
};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java
index 38d60ce..87d777a 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWildcardQueryWrapper.java
@@ -3,11 +3,12 @@
import org.apache.lucene.search.WildcardQuery;
import org.apache.lucene.search.spans.SpanQuery;
import org.apache.lucene.search.spans.SpanMultiTermQueryWrapper;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import org.apache.lucene.index.Term;
import java.util.*;
-public class SpanWildcardQueryWrapper {
+public class SpanWildcardQueryWrapper extends SpanQueryWrapper {
private SpanQuery query;
public SpanWildcardQueryWrapper (String field, String wc) {
@@ -29,15 +30,7 @@
return this.query;
};
- public boolean isOptional () {
- return false;
- };
-
public boolean isNull () {
return false;
};
-
- public boolean isNegative () {
- return false;
- };
};
diff --git a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java
index d67505c..0707882 100644
--- a/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java
+++ b/src/main/java/de/ids_mannheim/korap/query/wrap/SpanWithinQueryWrapper.java
@@ -4,7 +4,7 @@
import de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper;
import de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper;
import de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper;
-import de.ids_mannheim.korap.query.wrap.SpanQueryWrapperInterface;
+import de.ids_mannheim.korap.query.wrap.SpanQueryWrapper;
import java.util.*;
@@ -51,13 +51,12 @@
*/
-public class SpanWithinQueryWrapper implements SpanQueryWrapperInterface {
- private SpanQueryWrapperInterface element;
- private SpanQueryWrapperInterface wrap;
+public class SpanWithinQueryWrapper extends SpanQueryWrapper {
+ private SpanQueryWrapper element;
+ private SpanQueryWrapper wrap;
private byte flag;
- private boolean isNull = true;
- public SpanWithinQueryWrapper (SpanQueryWrapperInterface element, SpanQueryWrapperInterface wrap) {
+ public SpanWithinQueryWrapper (SpanQueryWrapper element, SpanQueryWrapper wrap) {
this.element = element;
this.wrap = wrap;
@@ -68,7 +67,7 @@
this.isNull = false;
};
- public SpanWithinQueryWrapper (SpanQueryWrapperInterface element, SpanQueryWrapperInterface wrap, byte flag) {
+ public SpanWithinQueryWrapper (SpanQueryWrapper element, SpanQueryWrapper wrap, byte flag) {
this.element = element;
this.wrap = wrap;
this.flag = flag;
@@ -88,14 +87,6 @@
return new SpanWithinQuery(this.element.toQuery(), this.wrap.toQuery(), this.flag);
};
- public boolean isOptional () {
- return false;
- };
-
- public boolean isNull () {
- return this.isNull;
- };
-
public boolean isNegative () {
if (this.element.isNegative())
return true;
diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties
index 48c48d1..2ed007e 100644
--- a/src/main/resources/log4j.properties
+++ b/src/main/resources/log4j.properties
@@ -1,6 +1,6 @@
## logger file can be used with
-log4j.rootLogger = ERROR, stdout
+# log4j.rootLogger = ERROR, stdout
# Spans:
#log4j.logger.de.ids_mannheim.korap.query.spans.ElementSpans = TRACE, stdout
@@ -15,8 +15,8 @@
#log4j.logger.de.ids_mannheim.korap.query.spans.MatchSpans = TRACE, stdout
# Wrapper
-#log4j.logger.de.ids_mannheim.korap.KorapQuery = TRACE, stdout
-#log4j.logger.de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper = TRACE, stdout
+# log4j.logger.de.ids_mannheim.korap.KorapQuery = TRACE, stdout
+# log4j.logger.de.ids_mannheim.korap.query.wrap.SpanSequenceQueryWrapper = TRACE, stdout
# Collections
#log4j.logger.de.ids_mannheim.korap.KorapFilter = TRACE, stdout