Update span distance query with isOrdered option
diff --git a/src/main/java/de/ids_mannheim/korap/query/SpanDistanceQuery.java b/src/main/java/de/ids_mannheim/korap/query/SpanDistanceQuery.java
index 5ce6bb5..0ae5be3 100644
--- a/src/main/java/de/ids_mannheim/korap/query/SpanDistanceQuery.java
+++ b/src/main/java/de/ids_mannheim/korap/query/SpanDistanceQuery.java
@@ -12,6 +12,7 @@
import de.ids_mannheim.korap.query.spans.ElementDistanceSpan;
import de.ids_mannheim.korap.query.spans.TokenDistanceSpan;
+import de.ids_mannheim.korap.query.spans.UnorderedDistanceSpans;
/** Match two ordered Spans with minimum and maximum distance constraints.
* The distance unit can be word (token), sentence or paragraph.
@@ -21,27 +22,28 @@
public class SpanDistanceQuery extends SimpleSpanQuery {
protected int minDistance, maxDistance;
- protected boolean collectPayloads;
+ protected boolean isOrdered, collectPayloads;
protected SpanQuery firstClause, secondClause;
private SpanQuery elementQuery; // element distance unit
-
+
public SpanDistanceQuery(SpanQuery firstClause, SpanQuery secondClause,
- int minDistance, int maxDistance, boolean collectPayloads) {
- super(firstClause, secondClause, "spanDistance");
+ int minDistance, int maxDistance, boolean isOrdered,
+ boolean collectPayloads) {
+ super(firstClause, secondClause, "spanDistance");
this.firstClause=firstClause;
this.secondClause=secondClause;
this.minDistance =minDistance;
this.maxDistance = maxDistance;
+ this.isOrdered = isOrdered;
this.collectPayloads = collectPayloads;
}
- public SpanDistanceQuery(SpanQuery elementQuery,
- SpanQuery firstClause, SpanQuery secondClause,
- int minDistance, int maxDistance,
- boolean collectPayloads) {
- this(firstClause, secondClause,minDistance, maxDistance,
+ public SpanDistanceQuery(SpanQuery elementQuery, SpanQuery firstClause,
+ SpanQuery secondClause, int minDistance, int maxDistance,
+ boolean isOrdered, boolean collectPayloads) {
+ this(firstClause, secondClause, minDistance, maxDistance, isOrdered,
collectPayloads);
- this.elementQuery = elementQuery;
+ this.elementQuery = elementQuery;
}
@Override
@@ -51,6 +53,7 @@
(SpanQuery) secondClause.clone(),
this.minDistance,
this.maxDistance,
+ this.isOrdered,
this.collectPayloads
);
@@ -66,10 +69,16 @@
public Spans getSpans(AtomicReaderContext context, Bits acceptDocs,
Map<Term, TermContext> termContexts) throws IOException {
- if (this.elementQuery != null)
- return new ElementDistanceSpan(this, context, acceptDocs, termContexts);
-
- return new TokenDistanceSpan(this, context, acceptDocs, termContexts);
+ if (isOrdered){
+ if (this.elementQuery != null) {
+ return new ElementDistanceSpan(this, context, acceptDocs, termContexts);
+ }
+ return new TokenDistanceSpan(this, context, acceptDocs, termContexts);
+ }
+ else if (this.elementQuery != null) {
+ //return new ElementDistanceSpan(this, context, acceptDocs, termContexts);
+ }
+ return new UnorderedDistanceSpans(this, context, acceptDocs, termContexts);
}
public int getMinDistance() {
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/DistanceSpan.java b/src/main/java/de/ids_mannheim/korap/query/spans/DistanceSpan.java
index 103fd14..2f48020 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/DistanceSpan.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/DistanceSpan.java
@@ -127,9 +127,4 @@
return advance();
}
- @Override
- public long cost() {
- CandidateSpan candidateSpan = candidateList.get(candidateListIndex);
- return candidateSpan.getCost() + secondSpans.cost();
- }
}
diff --git a/src/main/java/de/ids_mannheim/korap/query/spans/TokenDistanceSpan.java b/src/main/java/de/ids_mannheim/korap/query/spans/TokenDistanceSpan.java
index 5990029..a4f26fc 100644
--- a/src/main/java/de/ids_mannheim/korap/query/spans/TokenDistanceSpan.java
+++ b/src/main/java/de/ids_mannheim/korap/query/spans/TokenDistanceSpan.java
@@ -92,5 +92,11 @@
}
return false;
}
+
+ @Override
+ public long cost() {
+ CandidateSpan candidateSpan = candidateList.get(candidateListIndex);
+ return candidateSpan.getCost() + secondSpans.cost();
+ }
}
diff --git a/src/test/java/de/ids_mannheim/korap/index/TestDistanceQuery.java b/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
similarity index 86%
rename from src/test/java/de/ids_mannheim/korap/index/TestDistanceQuery.java
rename to src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
index 11f5a04..8e131d5 100644
--- a/src/test/java/de/ids_mannheim/korap/index/TestDistanceQuery.java
+++ b/src/test/java/de/ids_mannheim/korap/index/TestDistanceIndex.java
@@ -18,11 +18,11 @@
import de.ids_mannheim.korap.query.SpanSegmentQuery;
@RunWith(JUnit4.class)
-public class TestDistanceQuery {
+public class TestDistanceIndex {
KorapResult kr;
KorapIndex ki;
- private FieldDocument createFieldDoc0() {
+ protected FieldDocument createFieldDoc0() {
FieldDocument fd = new FieldDocument();
fd.addString("ID", "doc-0");
fd.addTV("base",
@@ -36,7 +36,7 @@
return fd;
}
- private FieldDocument createFieldDoc1(){
+ protected FieldDocument createFieldDoc1(){
FieldDocument fd = new FieldDocument();
fd.addString("ID", "doc-1");
fd.addTV("base",
@@ -54,7 +54,7 @@
return fd;
}
- private FieldDocument createFieldDoc2() {
+ protected FieldDocument createFieldDoc2() {
FieldDocument fd = new FieldDocument();
fd.addString("ID", "doc-2");
fd.addTV("base",
@@ -68,23 +68,25 @@
return fd;
}
- private SpanQuery createQuery(String x, String y, int min, int max){
+ protected SpanQuery createQuery(String x, String y, int min, int max, boolean isOrdered){
SpanQuery sq = new SpanDistanceQuery(
new SpanTermQuery(new Term("base",x)),
new SpanTermQuery(new Term("base",y)),
min,
max,
+ isOrdered,
true
);
return sq;
}
- private SpanQuery createElementQuery(String x, String y, int min, int max){
+ protected SpanQuery createElementQuery(String x, String y, int min, int max, boolean isOrdered){
SpanQuery sq = new SpanDistanceQuery(
new SpanElementQuery("base",x),
new SpanElementQuery("base",y),
min,
max,
+ isOrdered,
true
);
return sq;
@@ -101,7 +103,7 @@
ki.commit();
SpanQuery sq;
// ---- Distance 0 to 1
- sq = createQuery("s:b","s:c",0,1);
+ sq = createQuery("s:b","s:c",0,1,true);
kr = ki.search(sq, (short) 10);
assertEquals(2, kr.totalResults());
@@ -111,7 +113,7 @@
assertEquals(3, kr.match(1).endPos);
// ---- Distance 2 to 2
- sq = createQuery("s:b","s:c",2,2);
+ sq = createQuery("s:b","s:c",2,2,true);
kr = ki.search(sq, (short) 10);
assertEquals(2, kr.totalResults());
@@ -121,7 +123,7 @@
assertEquals(4, kr.match(1).endPos);
// ---- Distance 2 to 3
- sq = createQuery("s:b","s:c",2,3);
+ sq = createQuery("s:b","s:c",2,3,true);
kr = ki.search(sq, (short) 10);
assertEquals(3, kr.totalResults());
@@ -142,7 +144,7 @@
// ---- Distance 1 to 3
// Candidate list for the current secondspan, is empty
- SpanQuery sq = createQuery("s:c","s:d",1,3);
+ SpanQuery sq = createQuery("s:c","s:d",1,3,true);
kr = ki.search(sq, (short) 10);
assertEquals(4, kr.getTotalResults());
@@ -156,7 +158,7 @@
// ---- Distance 3 to 3
// Candidate list is empty, but there are secondspans in the other doc
- sq = createQuery("s:c","s:d",3,3);
+ sq = createQuery("s:c","s:d",3,3,true);
kr = ki.search(sq, (short) 10);
assertEquals(2, kr.getTotalResults());
@@ -176,7 +178,7 @@
ki.commit();
SpanQuery sq;
- sq = createQuery("s:c","s:d",3,3);
+ sq = createQuery("s:c","s:d",3,3,true);
kr = ki.search(sq, (short) 10);
assertEquals(2, kr.totalResults());
@@ -197,7 +199,7 @@
ki.commit();
// ---- Distance 1 to 2
- SpanQuery sq = createQuery("s:b","s:c",1,2);
+ SpanQuery sq = createQuery("s:b","s:c",1,2,true);
kr = ki.search(sq, (short) 10);
assertEquals(3, kr.totalResults());
@@ -218,7 +220,7 @@
ki.commit();
// Intersection ---- Distance 0:0
- SpanQuery sq = createElementQuery("x","y",0,0);
+ SpanQuery sq = createElementQuery("x","y",0,0,true);
kr = ki.search(sq, (short) 10);
assertEquals(4, kr.totalResults());
@@ -230,7 +232,7 @@
assertEquals(8, kr.getMatch(2).endPos);
// Next to ---- Distance 1:1
- sq = createElementQuery("y","x",1,1);
+ sq = createElementQuery("y","x",1,1,true);
kr = ki.search(sq, (short) 10);
assertEquals(1, kr.totalResults());
@@ -238,7 +240,7 @@
assertEquals(10, kr.getMatch(0).endPos);
// ---- Distance 1:2
- sq = createElementQuery("y","x",1,2);
+ sq = createElementQuery("y","x",1,2,true);
kr = ki.search(sq, (short) 10);
assertEquals(2, kr.totalResults());
@@ -248,17 +250,10 @@
assertEquals(10, kr.getMatch(1).endPos);
// The same element type ---- Distance 1:2
- sq = createElementQuery("x","x",1,2);
+ sq = createElementQuery("x","x",1,2,true);
kr = ki.search(sq, (short) 10);
assertEquals(2, kr.totalResults());
-
-// System.out.print(kr.getTotalResults()+"\n");
-// for (int i=0; i< kr.getTotalResults(); i++){
-// System.out.println(
-// kr.match(i).startPos + " " +kr.match(i).endPos
-// );
-// }
}
/** Skip to */
@@ -269,7 +264,7 @@
ki.addDoc(createFieldDoc1());
ki.commit();
- SpanQuery firstClause = createQuery("s:d", "s:e", 3, 4);
+ SpanQuery firstClause = createQuery("s:d", "s:e", 3, 4,true);
kr = ki.search(firstClause, (short) 10);
assertEquals(3, kr.totalResults());
@@ -284,8 +279,8 @@
// The secondspans is skipped to doc# of the current firstspans
SpanQuery sq = new SpanSegmentQuery(
- createQuery("s:d","s:e",3,4),
- createElementQuery("y","x",1,2)
+ createQuery("s:d","s:e",3,4,true),
+ createElementQuery("y","x",1,2,true)
);
kr = ki.search(sq, (short) 10);