Search in sources :

Example 21 with DistanceConstraint

use of de.ids_mannheim.korap.query.DistanceConstraint in project Krill by KorAP.

the class TestMultipleDistanceIndex method testCase2.

/**
 * Ordered
 * Unordered
 * Two constraints
 * Three constraints
 */
@Test
public void testCase2() throws IOException {
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc0());
    ki.commit();
    // Ordered - two constraints
    List<DistanceConstraint> constraints = new ArrayList<DistanceConstraint>();
    constraints.add(createConstraint("w", 0, 2, true, false));
    constraints.add(createConstraint("s", 1, 1, true, false));
    SpanQuery mdq;
    mdq = createQuery("s:b", "s:c", constraints, true);
    kr = ki.search(mdq, (short) 10);
    assertEquals((long) 3, kr.getTotalResults());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(3, kr.getMatch(0).getEndPos());
    assertEquals(1, kr.getMatch(1).getStartPos());
    assertEquals(3, kr.getMatch(1).getEndPos());
    assertEquals(3, kr.getMatch(2).getStartPos());
    assertEquals(5, kr.getMatch(2).getEndPos());
    // Three constraints
    constraints.add(createConstraint("p", 0, 0, true, false));
    mdq = createQuery("s:b", "s:c", constraints, true);
    kr = ki.search(mdq, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    // Unordered - two constraints
    constraints.clear();
    constraints.add(createConstraint("w", 0, 2, false, false));
    constraints.add(createConstraint("s", 1, 1, false, false));
    mdq = createQuery("s:c", "s:b", constraints, false);
    kr = ki.search(mdq, (short) 10);
    assertEquals((long) 4, kr.getTotalResults());
    assertEquals(1, kr.getMatch(2).getStartPos());
    assertEquals(4, kr.getMatch(2).getEndPos());
    // Three constraints
    constraints.add(createConstraint("p", 0, 0, false, false));
    mdq = createQuery("s:b", "s:c", constraints, false);
    kr = ki.search(mdq, (short) 10);
    assertEquals((long) 3, kr.getTotalResults());
}
Also used : ArrayList(java.util.ArrayList) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 22 with DistanceConstraint

use of de.ids_mannheim.korap.query.DistanceConstraint in project Krill by KorAP.

the class TestReferenceIndex method testCase1.

@Test
public void testCase1() throws IOException {
    ki = new KrillIndex();
    ki.addDoc(TestRelationIndex.createFieldDoc2());
    ki.commit();
    SpanTermQuery stq = new SpanTermQuery(new Term("base", "pos:V"));
    SpanElementQuery seq = new SpanElementQuery("base", "np");
    SpanClassQuery scq1 = new SpanClassQuery(stq, (byte) 1);
    SpanClassQuery scq2 = new SpanClassQuery(seq, (byte) 2);
    SpanNextQuery snq1 = new SpanNextQuery(scq1, scq2);
    SpanFocusQuery sfq1 = new SpanFocusQuery(snq1, (byte) 2);
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", "<:child-of")), true, RelationDirection.LEFT);
    SpanElementQuery seq2 = new SpanElementQuery("base", "pp");
    SpanClassQuery scq3 = new SpanClassQuery(seq2, (byte) 3);
    SpanRelationMatchQuery rq = new SpanRelationMatchQuery(srq, sfq1, scq3, true);
    // focus on np
    SpanFocusQuery sfq2 = new SpanFocusQuery(rq, (byte) 1);
    DistanceConstraint constraint = new DistanceConstraint(3, 3, true, false);
    SpanDistanceQuery sdq = new SpanDistanceQuery(sfq2, scq3, constraint, true);
    SpanReferenceQuery ref = new SpanReferenceQuery(sdq, (byte) 3, true);
    kr = ki.search(ref, (short) 10);
    /*
         * for (Match km : kr.getMatches()) {
         * 
         * System.out.println(km.getStartPos() + "," + km.getEndPos()
         * + " " + km.getSnippetBrackets()); }
         * System.out.println(kr.getTotalResults());
         */
    // cat=V & cat=np & cat=pp & #1 . #2 & #3 ->child-of #2 & #1 .{3,3} #3
    assertEquals("spanReference(spanDistance(focus(1: focus(#[1,2]spanSegment(" + "focus(#1: spanSegment(spanRelation(base:<:child-of), focus(2: spanNext(" + "{1: base:pos:V}, {2: <base:np />})))), {3: <base:pp />}))), " + "{3: <base:pp />}, [(w[3:3], ordered, notExcluded)]), 3)", ref.toString());
    assertEquals(1, kr.getMatch(0).getStartPos());
    assertEquals(7, kr.getMatch(0).getEndPos());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) SpanDistanceQuery(de.ids_mannheim.korap.query.SpanDistanceQuery) SpanRelationMatchQuery(de.ids_mannheim.korap.query.SpanRelationMatchQuery) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) Term(org.apache.lucene.index.Term) SpanReferenceQuery(de.ids_mannheim.korap.query.SpanReferenceQuery) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery) Test(org.junit.Test)

Example 23 with DistanceConstraint

use of de.ids_mannheim.korap.query.DistanceConstraint in project Krill by KorAP.

the class TestReferenceIndex method testCase2.

@Test
public void testCase2() throws IOException, QueryException {
    String filepath = getClass().getResource("/queries/reference/distance-reference.jsonld").getFile();
    SpanQueryWrapper sqwi = getJSONQuery(filepath);
    SpanQuery sq = sqwi.toQuery();
    // cat="vb" & cat="prp" & cat="nn" & #1 .{0,1} #2 & #1
    // .{0,2} #3 & #3 -> #2
    assertEquals("spanReference(focus(#[1,2]spanSegment({2: <tokens:prp />}, focus(#2: " + "spanSegment(spanRelation(tokens:>:stanford/d:tag), " + "focus(3: spanDistance(focus(1: spanDistance({1: <tokens:vb />}, " + "{2: <tokens:prp />}, [(w[1:2], notOrdered, notExcluded)])), " + "{3: <tokens:nn />}, [(w[1:3], notOrdered, notExcluded)]))))" + ")), 2)", sq.toString());
    SpanElementQuery seq1 = new SpanElementQuery("tokens", "vb");
    // new SpanTermQuery(new Term("tokens", "c:vb"));
    SpanElementQuery seq2 = new SpanElementQuery("tokens", "prp");
    // new SpanTermQuery(new Term("tokens", "c:prp"));
    SpanElementQuery seq3 = new SpanElementQuery("tokens", "nn");
    // new SpanTermQuery(new Term("tokens", "c:nn"));
    SpanClassQuery scq1 = new SpanClassQuery(seq1, (byte) 1);
    SpanClassQuery scq2 = new SpanClassQuery(seq2, (byte) 2);
    SpanClassQuery scq3 = new SpanClassQuery(seq3, (byte) 3);
    SpanDistanceQuery sdq1 = new SpanDistanceQuery(scq1, scq2, new DistanceConstraint(1, 2, false, false), true);
    SpanFocusQuery sfq1 = new SpanFocusQuery(sdq1, (byte) 1);
    SpanDistanceQuery sdq2 = new SpanDistanceQuery(sfq1, scq3, new DistanceConstraint(1, 3, false, false), true);
    SpanFocusQuery sfq2 = new SpanFocusQuery(sdq2, (byte) 3);
    // nn -> prp
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("tokens", ">:stanford/d:tag")), true, RelationDirection.RIGHT);
    SpanRelationMatchQuery rq = new SpanRelationMatchQuery(srq, sfq2, scq2, true);
    SpanReferenceQuery ref = new SpanReferenceQuery(rq, (byte) 2, true);
    ki = new KrillIndex();
    ki.addDoc(createFieldDoc0());
    ki.commit();
    kr = ki.search(ref, (short) 10);
    // for (Match km : kr.getMatches()) {
    // System.out.println(km.getStartPos() + "," + km.getEndPos()
    // + " "
    // + km.getSnippetBrackets());
    // }
    assertEquals(sq.toString(), ref.toString());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(4, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(9, kr.getMatch(1).getEndPos());
    assertEquals(11, kr.getMatch(2).getStartPos());
    assertEquals(13, kr.getMatch(2).getEndPos());
    // multiple references
    SpanFocusQuery sfq3 = new SpanFocusQuery(ref, (byte) 1);
    // vp -> nn
    SpanRelationMatchQuery rq2 = new SpanRelationMatchQuery(srq, sfq3, scq3, true);
    SpanReferenceQuery ref2 = new SpanReferenceQuery(rq2, (byte) 3, true);
    kr = ki.search(ref2, (short) 10);
    assertEquals(1, kr.getMatch(0).getStartPos());
    assertEquals(4, kr.getMatch(0).getEndPos());
    assertEquals(10, kr.getMatch(1).getStartPos());
    assertEquals(13, kr.getMatch(1).getEndPos());
// for (Match km : kr.getMatches()) {
// System.out.println(km.getStartPos() + "," + km.getEndPos()
// + " "
// + km.getSnippetBrackets());
// }
}
Also used : SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) Term(org.apache.lucene.index.Term) SpanReferenceQuery(de.ids_mannheim.korap.query.SpanReferenceQuery) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanQuery(org.apache.lucene.search.spans.SpanQuery) SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanDistanceQuery(de.ids_mannheim.korap.query.SpanDistanceQuery) SpanRelationMatchQuery(de.ids_mannheim.korap.query.SpanRelationMatchQuery) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) Test(org.junit.Test)

Example 24 with DistanceConstraint

use of de.ids_mannheim.korap.query.DistanceConstraint in project Krill by KorAP.

the class TestSubSpanIndex method testCase2.

@Test
public void testCase2() {
    SpanDistanceQuery sdq = new SpanDistanceQuery(new SpanTermQuery(new Term("tokens", "tt/p:NN")), new SpanTermQuery(new Term("tokens", "tt/p:VAFIN")), new DistanceConstraint(5, 5, true, false), true);
    // the subspan length is longer than the span length
    SpanSubspanQuery ssq = new SpanSubspanQuery(sdq, 0, 7, true);
    kr = ki.search(ssq, (short) 10);
    assertEquals(35, kr.getMatch(0).getStartPos());
    assertEquals(41, kr.getMatch(0).getEndPos());
    assertEquals(179, kr.getMatch(1).getStartPos());
    assertEquals(185, kr.getMatch(1).getEndPos());
    // the subspan start is before the span start
    ssq = new SpanSubspanQuery(sdq, -7, 4, true);
    kr = ki.search(ssq, (short) 10);
    assertEquals((long) 8, kr.getTotalResults());
    assertEquals(35, kr.getMatch(0).getStartPos());
    assertEquals(39, kr.getMatch(0).getEndPos());
    assertEquals(179, kr.getMatch(1).getStartPos());
    assertEquals(183, kr.getMatch(1).getEndPos());
}
Also used : SpanSubspanQuery(de.ids_mannheim.korap.query.SpanSubspanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanDistanceQuery(de.ids_mannheim.korap.query.SpanDistanceQuery) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) Term(org.apache.lucene.index.Term) Test(org.junit.Test)

Example 25 with DistanceConstraint

use of de.ids_mannheim.korap.query.DistanceConstraint in project Krill by KorAP.

the class TestSubSpanIndex method testCase3.

// Length 0
@Test
public void testCase3() {
    SpanDistanceQuery sdq = new SpanDistanceQuery(new SpanTermQuery(new Term("tokens", "tt/p:NN")), new SpanTermQuery(new Term("tokens", "tt/p:VAFIN")), new DistanceConstraint(5, 5, true, false), true);
    SpanSubspanQuery ssq = new SpanSubspanQuery(sdq, 3, 0, true);
    kr = ki.search(ssq, (short) 10);
    assertEquals(38, kr.getMatch(0).getStartPos());
    assertEquals(41, kr.getMatch(0).getEndPos());
    assertEquals(182, kr.getMatch(1).getStartPos());
    assertEquals(185, kr.getMatch(1).getEndPos());
    ssq = new SpanSubspanQuery(sdq, -2, 0, true);
    kr = ki.search(ssq, (short) 10);
    assertEquals(39, kr.getMatch(0).getStartPos());
    assertEquals(41, kr.getMatch(0).getEndPos());
    assertEquals(183, kr.getMatch(1).getStartPos());
    assertEquals(185, kr.getMatch(1).getEndPos());
// for (Match km : kr.getMatches()) {
// System.out.println(km.getStartPos() + "," + km.getEndPos()
// + km.getSnippetBrackets());
// }
}
Also used : SpanSubspanQuery(de.ids_mannheim.korap.query.SpanSubspanQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanDistanceQuery(de.ids_mannheim.korap.query.SpanDistanceQuery) DistanceConstraint(de.ids_mannheim.korap.query.DistanceConstraint) Term(org.apache.lucene.index.Term) Test(org.junit.Test)

Aggregations

DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)26 Test (org.junit.Test)23 KrillIndex (de.ids_mannheim.korap.KrillIndex)20 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)19 Term (org.apache.lucene.index.Term)13 SpanQuery (org.apache.lucene.search.spans.SpanQuery)13 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)13 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)11 ArrayList (java.util.ArrayList)10 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)8 SpanClassFilterQuery (de.ids_mannheim.korap.query.SpanClassFilterQuery)6 SpanMultipleDistanceQuery (de.ids_mannheim.korap.query.SpanMultipleDistanceQuery)3 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)3 SpanSubspanQuery (de.ids_mannheim.korap.query.SpanSubspanQuery)3 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)2 SpanReferenceQuery (de.ids_mannheim.korap.query.SpanReferenceQuery)2 SpanRelationMatchQuery (de.ids_mannheim.korap.query.SpanRelationMatchQuery)2 SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)2 Krill (de.ids_mannheim.korap.Krill)1 QueryBuilder (de.ids_mannheim.korap.query.QueryBuilder)1