Added exclusion to KorapQuery wrapper
diff --git a/src/main/java/de/ids_mannheim/korap/KorapQuery.java b/src/main/java/de/ids_mannheim/korap/KorapQuery.java
index 28fbcc0..bb6261c 100644
--- a/src/main/java/de/ids_mannheim/korap/KorapQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/KorapQuery.java
@@ -40,6 +40,10 @@
// Logger
private final static Logger log = LoggerFactory.getLogger(KorapQuery.class);
+ // This advices the java compiler to ignore all loggings
+ public static final boolean DEBUG = false;
+
+
/**
* Constructs a new base object for query generation.
* @param field The specific index field for the query.
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanElementQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanElementQuery.java
index ad97152..7e4ee9e 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanElementQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanElementQuery.java
@@ -19,6 +19,9 @@
import java.util.Map;
import java.util.Set;
+/**
+ * @author Nils Diewald
+ */
/** Matches spans wrapped by an element. */
public class SpanElementQuery extends SpanQuery {
@@ -121,7 +124,9 @@
return new ElementSpans(postings, element);
// element does exist, but has no positions
- throw new IllegalStateException("field \"" + element.field() + "\" was indexed without position data; cannot run SpanElementQuery (element=" + element.text() + ")");
+ throw new IllegalStateException("field \"" + element.field() + "\" was indexed " +
+ "without position data; cannot run " +
+ "SpanElementQuery (element=" + element.text() + ")");
};
public String getElementStr () {
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
index f10809c..a44ed49 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/ElementSpans.java
@@ -23,7 +23,7 @@
// TODO: Store payloads in 12 byte instead of the complicated ByteBuffer stuff!
/**
- * @author ndiewald
+ * @author Nils Diewald
*/
public class ElementSpans extends Spans {
@@ -47,6 +47,10 @@
private final static Logger log = LoggerFactory.getLogger(ElementSpans.class);
+
+ /**
+ * The constructor.
+ */
public ElementSpans(DocsAndPositionsEnum postings, Term term) {
this.postings = postings;
this.term = term;
@@ -61,7 +65,7 @@
};
// only for EmptyElementSpans (below)
- ElementSpans() {
+ public ElementSpans() {
term = null;
postings = null;
};
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 5fa21d0..e6c0488 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
@@ -90,25 +90,37 @@
};
public SpanSequenceQueryWrapper withConstraint (int min, int max) {
+ return this.withConstraint(min, max, false);
+ };
+
+ public SpanSequenceQueryWrapper withConstraint (int min, int max, boolean exclusion) {
if (this.constraints == null)
this.constraints = new ArrayList<DistanceConstraint>(1);
- this.constraints.add(new DistanceConstraint(min, max,false));
+ this.constraints.add(new DistanceConstraint(min, max, exclusion));
return this;
};
public SpanSequenceQueryWrapper withConstraint (int min, int max, String unit) {
+ return this.withConstraint(min, max, unit, false);
+ };
+
+ public SpanSequenceQueryWrapper withConstraint (int min,
+ int max,
+ String unit,
+ boolean exclusion) {
if (this.constraints == null)
this.constraints = new ArrayList<DistanceConstraint>(1);
if (unit.equals("w"))
- this.constraints.add(new DistanceConstraint(min, max,false));
+ this.constraints.add(new DistanceConstraint(min, max, exclusion));
else
this.constraints.add(
new DistanceConstraint(
- new SpanElementQuery(this.field, unit), min, max,false)
+ new SpanElementQuery(this.field, unit), min, max, exclusion)
);
return this;
};
+
public SpanQuery toQuery () {
if (this.segments.size() == 0) {
return (SpanQuery) null;
diff --git a/src/main/resources/log4j.properties b/src/main/resources/log4j.properties
index e876668..c5ac092 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 = DEBUG, stdout
+log4j.rootLogger = DEBUG, stdout
# Spans:
#log4j.logger.de.ids_mannheim.korap.query.spans.ElementSpans = TRACE, stdout
diff --git a/src/test/java/de/ids_mannheim/korap/benchmark/TestBenchmarkElementSpans.java b/src/test/java/de/ids_mannheim/korap/benchmark/TestBenchmarkElementSpans.java
index 7bfed40..d177eb3 100644
--- a/src/test/java/de/ids_mannheim/korap/benchmark/TestBenchmarkElementSpans.java
+++ b/src/test/java/de/ids_mannheim/korap/benchmark/TestBenchmarkElementSpans.java
@@ -112,6 +112,45 @@
// 77.167124985 sec
};
+
+ @Test
+ public void checkBenchmarkSentences () throws IOException {
+ Properties prop = new Properties();
+ InputStream fr = new FileInputStream(getClass().getResource("/korap.conf").getFile());
+ prop.load(fr);
+
+ // Get the real index
+ KorapIndex ki = new KorapIndex(new MMapDirectory(new File(prop.getProperty("lucene.indexDir"))));
+
+ // Create a container for virtual collections:
+ KorapCollection kc = new KorapCollection(ki);
+
+ long t1 = 0, t2 = 0;
+ /// cosmas20.json!!!
+ String json = getString(getClass().getResource("/queries/benchmark4.jsonld").getFile());
+
+ int rounds = 10;
+
+ KorapResult kr = new KorapResult();
+
+ t1 = System.nanoTime();
+ double length = 0;
+ for (int i = 1; i <= rounds; i++) {
+ kr = new KorapSearch(json).run(ki);
+ };
+ t2 = System.nanoTime();
+
+ System.err.println(kr.getMatch(0).toJSON());
+
+ assertEquals("TotalResults1", 4116282, kr.getTotalResults());
+ assertEquals("TotalResults2", kr.getTotalResults(), ki.numberOf("sentences"));
+
+ double seconds = (double)(t2-t1) / 1000000000.0;
+
+ System.out.println("It took " + seconds + " seconds");
+ };
+
+
@Test
public void checkBenchmarkIndexDocuments () throws IOException {
long t1 = 0, t2 = 0;
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKorapQuery.java b/src/test/java/de/ids_mannheim/korap/query/TestKorapQuery.java
index 6442f29..6ed797c 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKorapQuery.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKorapQuery.java
@@ -155,6 +155,64 @@
assertEquals("spanNext(spanNext(field:try1, shrink(1: {1: field:try2})), field:try3)", sq.toString());
};
+
+ @Test
+ public void KorapSequenceQuery1 () {
+ KorapQuery kq = new KorapQuery("field");
+ SpanQuery sq = kq.seq(kq.seg("try1")).append(kq.seg("try2")).toQuery();
+ assertEquals("spanNext(field:try1, field:try2)", sq.toString());
+ };
+
+ @Test
+ public void KorapSequenceQuery2 () {
+ KorapQuery kq = new KorapQuery("field");
+ SpanQuery sq = kq.seq(kq.seg("try1")).append(kq.seg("try2")).withConstraint(2,3).toQuery();
+ assertEquals("spanDistance(field:try1, field:try2, [(w[2:3], ordered, notExcluded)])", sq.toString());
+ };
+
+ @Test
+ public void KorapSequenceQuery3 () {
+ KorapQuery kq = new KorapQuery("field");
+ SpanQuery sq = kq.seq(kq.seg("try1")).append(kq.seg("try2")).withConstraint(2,3, "s").toQuery();
+ assertEquals("spanElementDistance(field:try1, field:try2, [(s[2:3], ordered, notExcluded)])", sq.toString());
+ };
+
+ @Test
+ public void KorapSequenceQuery4 () {
+ KorapQuery kq = new KorapQuery("field");
+ SpanQuery sq = kq.seq(kq.seg("try1")).append(kq.seg("try2")).withConstraint(2,3,"s").withConstraint(5,6,"w").toQuery();
+ assertEquals("spanMultipleDistance(field:try1, field:try2, [(s[2:3], ordered, notExcluded), (w[5:6], ordered, notExcluded)])", sq.toString());
+ };
+
+ @Test
+ public void KorapSequenceQuery5 () {
+ KorapQuery kq = new KorapQuery("field");
+ SpanQuery sq = kq.seq(kq.seg("try1")).append(kq.seg("try2")).withConstraint(2,3,true).toQuery();
+ assertEquals("spanDistance(field:try1, field:try2, [(w[2:3], ordered, excluded)])", sq.toString());
+ };
+
+ @Test
+ public void KorapSequenceQuery6 () {
+ KorapQuery kq = new KorapQuery("field");
+ SpanQuery sq = kq.seq(kq.seg("try1")).append(kq.seg("try2")).withConstraint(2,3,"s",true).toQuery();
+ assertEquals("spanElementDistance(field:try1, field:try2, [(s[2:3], ordered, excluded)])", sq.toString());
+ };
+
+ @Test
+ public void KorapSequenceQuery7 () {
+ KorapQuery kq = new KorapQuery("field");
+ SpanQuery sq = kq.seq(kq.seg("try1")).append(kq.seg("try2")).withConstraint(5,6).withConstraint(2,3,"s",true).toQuery();
+ assertEquals("spanMultipleDistance(field:try1, field:try2, [(w[5:6], ordered, notExcluded), (s[2:3], ordered, excluded)]])", sq.toString());
+ };
+
+ @Test
+ public void KorapSequenceQuery8 () {
+ KorapQuery kq = new KorapQuery("field");
+ SpanQuery sq = kq.seq(kq.seg("try1")).append(kq.seg("try2")).append("try3").withConstraint(5,6).withConstraint(2,3,"s",true).toQuery();
+ assertEquals("spanMultipleDistance(spanMultipleDistance(field:try1, field:try2, [(w[5:6], ordered, notExcluded), (s[2:3], ordered, excluded)]]), field:try3, [(w[5:6], ordered, notExcluded), (s[2:3], ordered, excluded)]])", sq.toString());
+ };
+
+
// kq.seg("a").append(kq.ANY).append("b:c");
// kq.repeat(kq.seg("a", "b"), 5)
};
diff --git a/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java b/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java
index 780a397..f4cc0a5 100644
--- a/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java
+++ b/src/test/java/de/ids_mannheim/korap/query/TestKorapQueryJSON.java
@@ -101,6 +101,15 @@
};
@Test
+ public void queryJSONBsp9b () {
+ SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp9b.jsonld").getFile());
+
+ // [base=Katze&orth=Katzen]
+ assertEquals(sqwi.toQuery().toString(), "spanSegment(tokens:mate/m:number:pl, tokens:tt/p:NN)");
+ };
+
+
+ @Test
public void queryJSONBsp10 () {
SpanQueryWrapperInterface sqwi = jsonQuery(getClass().getResource("/queries/bsp10.jsonld").getFile());
diff --git a/src/test/resources/queries/bsp9b.jsonld b/src/test/resources/queries/bsp9b.jsonld
new file mode 100644
index 0000000..d0f44b1
--- /dev/null
+++ b/src/test/resources/queries/bsp9b.jsonld
@@ -0,0 +1,24 @@
+{
+ "@context" : "http://ids-mannheim.de/ns/KorAP/json-ld/v0.1/context.jsonld",
+ "query" : {
+ "@type" : "korap:token",
+ "wrap" : {
+ "@type" : "korap:termGroup",
+ "relation" : "relation:and",
+ "operands" : [ {
+ "@type" : "korap:term",
+ "key" : "number",
+ "value" : "pl",
+ "layer" : "m",
+ "foundry" : "mate",
+ "match" : "match:eq"
+ }, {
+ "@type" : "korap:term",
+ "key" : "NN",
+ "layer" : "p",
+ "foundry" : "tt",
+ "match" : "match:eq"
+ } ]
+ }
+ }
+}
\ No newline at end of file