use of de.ids_mannheim.korap.query.SpanDistanceQuery in project Krill by KorAP.
the class SpanSequenceQueryWrapper method toFragmentQuery.
/**
* Serialize the wrapped sequence to a {@link SpanQuery} object.
*
* @return A {@link SpanQuery} object.
* @throws QueryException
*/
public SpanQuery toFragmentQuery() throws QueryException {
// There was a serialization failure not yet reported
if (this.constraintException != null)
throw constraintException;
int size = this.segments.size();
// Nothing to do
if (size == 0 || this.isNull())
return (SpanQuery) null;
// No real sequence - only one element
if (size == 1) {
// But the element may be expanded
if (this.segments.get(0).isExtended() && (this.hasConstraints() || !this.isInOrder())) {
throw new QueryException(613, limitationError);
}
;
// Unproblematic single query
if (this.segments.get(0).maybeAnchor())
return (SpanQuery) this.segments.get(0).retrieveNode(this.retrieveNode).toFragmentQuery();
if (this.segments.get(0).isEmpty())
throw new QueryException(613, "Sequence is not allowed to be empty");
if (this.segments.get(0).isOptional())
throw new QueryException(613, "Sequence is not allowed to be optional");
if (this.segments.get(0).isNegative())
throw new QueryException(613, "Sequence is not allowed to be negative");
}
;
if (!this.isSolved) {
if (!_solveProblematicSequence()) {
if (this.segments.get(0).maybeExtension()) {
throw new QueryException(613, "Sequence contains unresolvable " + "empty, optional, or negative segments");
}
;
}
;
}
;
// The element may be expanded
if (this.segments.size() == 1 && this.segments.get(0).isExtended() && (this.hasConstraints() || !this.isInOrder())) {
throw new QueryException(613, limitationError);
}
;
// Create the initial query
SpanQuery query = null;
int i = 0;
// Get the first valid segment
while (query == null && i < this.segments.size()) {
query = this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
i++;
}
;
// No valid segment found
if (query == null)
return (SpanQuery) null;
// NextQueries
if (!this.hasConstraints() && this.isInOrder()) {
for (; i < this.segments.size(); i++) {
// Get the first query for next sequence
SpanQuery second = this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
if (second == null)
continue;
query = new SpanNextQuery(query, second);
}
;
return (SpanQuery) query;
}
;
// DistanceQueries with problems
if (this.hasConstraints() && this.isProblematic) {
throw new QueryException(613, "Distance constraints not supported with empty, optional or negative operands");
}
;
// DistanceQueries
if (this.constraints.size() == 1) {
DistanceConstraint constraint = this.constraints.get(0);
// Create spanElementDistance query
if (!constraint.getUnit().equals("w")) {
for (i = 1; i < this.segments.size(); i++) {
// No support for extended spans in constraints
if (this.segments.get(i).isExtended())
throw new QueryException(613, limitationError);
/* Maybe important
if (this.segments.get(i).isOptional())
throw new QueryException(613, limitationError);
*/
SpanQuery sq = (SpanQuery) this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
if (sq == null)
continue;
SpanDistanceQuery sdquery = new SpanDistanceQuery(query, sq, constraint, true);
query = (SpanQuery) sdquery;
}
;
} else // Create spanDistance query
{
for (i = 1; i < this.segments.size(); i++) {
// No support for extended spans in constraints
if (this.segments.get(i).isExtended())
throw new QueryException(613, limitationError);
/* May be necessary
if (this.segments.get(i).isOptional())
throw new QueryException(613, limitationError);
*/
SpanQuery sq = (SpanQuery) this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
if (sq == null)
continue;
SpanDistanceQuery sdquery = new SpanDistanceQuery(query, sq, constraint, true);
query = (SpanQuery) sdquery;
}
;
}
;
return (SpanQuery) query;
}
;
// MultipleDistanceQueries
for (i = 1; i < this.segments.size(); i++) {
// No support for extended spans in constraints
if (this.segments.get(i).isExtended())
throw new QueryException(613, limitationError);
SpanQuery sq = (SpanQuery) this.segments.get(i).retrieveNode(this.retrieveNode).toFragmentQuery();
if (sq == null)
continue;
query = new SpanMultipleDistanceQuery(query, sq, this.constraints, isInOrder, true);
}
;
return (SpanQuery) query;
}
use of de.ids_mannheim.korap.query.SpanDistanceQuery in project Krill by KorAP.
the class TestUnorderedDistanceIndex method testCase7.
/**
* Nested distance queries
*/
@Test
public void testCase7() throws IOException {
// System.out.println("testcase 7");
ki = new KrillIndex();
ki.addDoc(createFieldDoc0());
ki.addDoc(createFieldDoc1());
ki.commit();
SpanQuery sq = createQuery("s:c", "s:d", 1, 2, false);
SpanQuery sq2 = new SpanDistanceQuery(sq, new SpanTermQuery(new Term("base", "s:e")), new DistanceConstraint(1, 2, true, false), true);
kr = ki.search(sq2, (short) 10);
assertEquals(kr.getTotalResults(), 3);
assertEquals(5, kr.getMatch(0).getStartPos());
assertEquals(9, kr.getMatch(0).getEndPos());
assertEquals(1, kr.getMatch(1).getLocalDocID());
assertEquals(0, kr.getMatch(1).getStartPos());
assertEquals(3, kr.getMatch(1).getEndPos());
assertEquals(0, kr.getMatch(2).getStartPos());
assertEquals(4, kr.getMatch(2).getEndPos());
}
use of de.ids_mannheim.korap.query.SpanDistanceQuery in project Krill by KorAP.
the class TestWPDIndex method createElementDistanceQuery.
private SpanDistanceQuery createElementDistanceQuery(String e, String x, String y, int min, int max, boolean isOrdered, boolean exclusion) {
SpanElementQuery eq = new SpanElementQuery("tokens", e);
SpanDistanceQuery sq = new SpanDistanceQuery(new SpanTermQuery(new Term("tokens", x)), new SpanTermQuery(new Term("tokens", y)), new DistanceConstraint(eq, min, max, isOrdered, exclusion), true);
return sq;
}
use of de.ids_mannheim.korap.query.SpanDistanceQuery in project Krill by KorAP.
the class TestWPDIndex method testCase1.
/**
* Token distance spans
*/
@Test
public void testCase1() throws IOException {
SpanDistanceQuery sq;
// ordered
sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, true, false);
ks = new Krill(sq);
kr = ks.apply(ki);
assertEquals(kr.getTotalResults(), 8);
// unordered
sq = createDistanceQuery("s:Wir", "s:kommen", 1, 1, false, false);
ks = new Krill(sq);
kr = ks.apply(ki);
assertEquals(kr.getTotalResults(), 11);
sq = createDistanceQuery("s:kommen", "s:Wir", 1, 1, false, false);
ks = new Krill(sq);
kr = ks.apply(ki);
assertEquals(kr.getTotalResults(), 11);
// System.out.println(kr.getTotalResults());
// for (Match km : kr.getMatches()){
// System.out.println(km.getDocID() +" "+km.getStartPos() +" "+ km.getEndPos());
// System.out.println(km.getSnippetBrackets());
// System.out.println(km.toJSON());
// }
}
use of de.ids_mannheim.korap.query.SpanDistanceQuery in project Krill by KorAP.
the class TestSampleIndex method testMultipleDistanceWithWildcards.
@Test
public void testMultipleDistanceWithWildcards() throws IOException, QueryException {
WildcardQuery wcquery = new WildcardQuery(new Term("tokens", "s:meine*"));
SpanMultiTermQueryWrapper<WildcardQuery> mtq = new SpanMultiTermQueryWrapper<WildcardQuery>(wcquery);
// meine* /+w1:2 &Erfahrung
SpanQuery tdq = new SpanDistanceQuery(mtq, sq, TestMultipleDistanceIndex.createConstraint("w", 1, 2, true, false), true);
kr = sample.search(tdq, (short) 10);
assertEquals(4, kr.getMatches().size());
assertEquals(107, kr.getMatch(0).getStartPos());
assertEquals(109, kr.getMatch(0).getEndPos());
assertEquals(132566, kr.getMatch(1).getStartPos());
assertEquals(132569, kr.getMatch(1).getEndPos());
assertEquals(161393, kr.getMatch(2).getStartPos());
assertEquals(161396, kr.getMatch(2).getEndPos());
assertEquals(10298, kr.getMatch(3).getStartPos());
assertEquals(10301, kr.getMatch(3).getEndPos());
// meine* /+s0 &Erfahrung
SpanQuery edq = new SpanDistanceQuery(mtq, sq, TestMultipleDistanceIndex.createConstraint("tokens", "base/s:s", 0, 0, true, false), true);
kr = sample.search(edq, (short) 20);
assertEquals(18, kr.getMatches().size());
// meine* /+w1:2,s0 &Erfahrung
SpanQuery mdsq = new SpanMultipleDistanceQuery(new SpanClassQuery(mtq, (byte) 129), new SpanClassQuery(sq, (byte) 129), constraints, true, true);
kr = sample.search(mdsq, (short) 10);
assertEquals(4, kr.getMatches().size());
// check SpanQueryWrapper generated query
SpanQueryWrapper sqwi = getJSONQuery(getClass().getResource("/queries/bugs/cosmas_wildcards.jsonld").getFile());
SpanQuery jsq = sqwi.toQuery();
assertEquals(mdsq.toString(), jsq.toString());
}
Aggregations