Search in sources :

Example 6 with SpanSegmentQuery

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

the class TestRelationIndex method testCase10b.

/**
 * Relation whose nodes have a specific attribute. Alternative
 * query (actually used in serialization)
 */
@Test
public void testCase10b() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    SpanAttributeQuery aq = new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:case=accusative")), true);
    kr = ki.search(aq, (short) 10);
    assertEquals((long) 3, kr.getTotalResults());
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:child-of")), true, RelationDirection.RIGHT);
    srq.setSourceClass((byte) 1);
    srq.setTargetClass((byte) 2);
    kr = ki.search(srq, (short) 20);
    assertEquals((long) 13, kr.getTotalResults());
    // Matching relation source node with an attribute
    SpanWithAttributeQuery swaq = new SpanWithAttributeQuery(aq, true);
    SpanSegmentQuery ssq = new SpanSegmentQuery(srq, swaq);
    assertEquals("spanSegment({1: source:{2: target:spanRelation(base:>:child-of)}}, " + "spanWithAttribute(spanAttribute(base:@:case=accusative)))", ssq.toString());
    kr = ki.search(ssq, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(4, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    // Matching relation target nodes with an attribute
    // NOTE: swaq must be the first parameter
    SpanFocusQuery sfq2 = new SpanFocusQuery(srq, (byte) 2);
    sfq2.setSorted(false);
    SpanSegmentQuery ssq2 = new SpanSegmentQuery(swaq, sfq2);
    kr = ki.search(ssq2, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(7, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    // Matching specific relation source node with an attribute
    SpanElementQuery seq = new SpanElementQuery("base", "np");
    swaq = new SpanWithAttributeQuery(seq, aq, true);
    ssq = new SpanSegmentQuery(srq, swaq);
    assertEquals("spanSegment({1: source:{2: target:spanRelation(base:>:child-of)}}, " + "spanElementWithAttribute(<base:np />, " + "spanAttribute(base:@:case=accusative)))", ssq.toString());
    kr = ki.search(ssq, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(4, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
}
Also used : SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) SpanWithAttributeQuery(de.ids_mannheim.korap.query.SpanWithAttributeQuery) SpanSegmentQuery(de.ids_mannheim.korap.query.SpanSegmentQuery) Term(org.apache.lucene.index.Term) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) Test(org.junit.Test)

Example 7 with SpanSegmentQuery

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

the class TestRelationIndex method testCase9c.

@Test
public void testCase9c() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    SpanTermWithIdQuery tiq = new SpanTermWithIdQuery(new Term("base", "pos:ART"), true);
    SpanClassQuery scq1 = new SpanClassQuery(tiq, (byte) 1);
    kr = ki.search(scq1, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(3, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(6, kr.getMatch(1).getEndPos());
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:child-of")), true, RelationDirection.RIGHT);
    srq.setSourceClass((byte) 1);
    srq.setTargetClass((byte) 2);
    // match articles as relation sources (left side)
    SpanSegmentQuery ssq1 = new SpanSegmentQuery(srq, scq1);
    // NOTE: SegmentSpans can only always adopt the relation left
    // side id.
    kr = ki.search(ssq1, (short) 10);
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(3, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(6, kr.getMatch(1).getEndPos());
    // return all parents of articles
    SpanFocusQuery sfq = new SpanFocusQuery(ssq1, (byte) 2);
    sfq.setSorted(false);
    kr = ki.search(sfq, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(4, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    SpanElementQuery seq = new SpanElementQuery("base", "np");
    SpanClassQuery scq2 = new SpanClassQuery(seq, (byte) 2);
    kr = ki.search(scq2, (short) 10);
    assertEquals((long) 4, kr.getTotalResults());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(1, kr.getMatch(0).getEndPos());
    assertEquals(2, kr.getMatch(1).getStartPos());
    assertEquals(4, kr.getMatch(1).getEndPos());
    assertEquals(2, kr.getMatch(2).getStartPos());
    assertEquals(7, kr.getMatch(2).getEndPos());
    assertEquals(5, kr.getMatch(3).getStartPos());
    assertEquals(7, kr.getMatch(3).getEndPos());
    // match nps as relation targets (right side)
    // return all nps whose children are articles
    SpanSegmentQuery ssq2 = new SpanSegmentQuery(sfq, scq2);
    kr = ki.search(ssq2, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(4, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    // NOTE: cannot match the span id of the embedded relation
    // target and the np. So the segment match here is only by
    // positions. All nps in the same position will be matches.
    // return the articles whos parents are nps
    SpanFocusQuery sfq2 = new SpanFocusQuery(ssq2, (byte) 1);
    sfq2.setSorted(false);
    assertEquals("focus(1: spanSegment(focus(2: spanSegment({1: source:" + "{2: target:spanRelation(base:>:child-of)}}, " + "{1: spanTermWithId(base:pos:ART)})), " + "{2: <base:np />}))", sfq2.toString());
    kr = ki.search(sfq2, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(3, kr.getMatch(0).getEndPos());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(6, kr.getMatch(1).getEndPos());
// for (Match m : kr.getMatches()) {
// System.out.println(m.getStartPos() + " " + m.getEndPos());
// }
}
Also used : SpanTermWithIdQuery(de.ids_mannheim.korap.query.SpanTermWithIdQuery) SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) SpanSegmentQuery(de.ids_mannheim.korap.query.SpanSegmentQuery) Term(org.apache.lucene.index.Term) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) Test(org.junit.Test)

Example 8 with SpanSegmentQuery

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

the class TestSegmentIndex method testCase3.

/**
 * Ensure the same document, skip to a greater doc number
 */
@Test
public void testCase3() throws IOException {
    // log.trace("Testcase3");
    sq = new SpanSegmentQuery(new SpanTermQuery(new Term("base", "s:d")), new SpanTermQuery(new Term("base", "s:b")));
    kr = ki.search(sq, (short) 10);
    ki.close();
    assertEquals("totalResults", kr.getTotalResults(), 1);
    assertEquals("doc-number", 2, kr.getMatch(0).getLocalDocID());
    assertEquals("StartPos (0)", 1, kr.getMatch(0).startPos);
    assertEquals("EndPos (0)", 2, kr.getMatch(0).endPos);
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanSegmentQuery(de.ids_mannheim.korap.query.SpanSegmentQuery) Term(org.apache.lucene.index.Term) Test(org.junit.Test)

Example 9 with SpanSegmentQuery

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

the class TestSegmentIndex method testcase6.

/**
 * Skip to SegmentSpan
 */
@Test
public void testcase6() throws IOException {
    ki.addDoc(createFieldDoc4());
    ki.commit();
    sq = new SpanNextQuery(new SpanSegmentQuery(new SpanTermQuery(new Term("base", "s:b")), new SpanTermQuery(new Term("base", "s:c"))), new SpanTermQuery(new Term("base", "s:d")));
    kr = ki.search(sq, (short) 10);
    ki.close();
    assertEquals("totalResults", kr.getTotalResults(), 2);
    // Match #0
    assertEquals("doc-number", 0, kr.getMatch(0).getLocalDocID());
    assertEquals("StartPos (0)", 4, kr.getMatch(0).startPos);
    assertEquals("EndPos (0)", 6, kr.getMatch(0).endPos);
    // Match #1 in the other atomic index
    assertEquals("doc-number", 0, kr.getMatch(1).getLocalDocID());
    assertEquals("StartPos (0)", 0, kr.getMatch(1).startPos);
    assertEquals("EndPos (0)", 2, kr.getMatch(1).endPos);
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanSegmentQuery(de.ids_mannheim.korap.query.SpanSegmentQuery) Term(org.apache.lucene.index.Term) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery) Test(org.junit.Test)

Aggregations

SpanSegmentQuery (de.ids_mannheim.korap.query.SpanSegmentQuery)9 Test (org.junit.Test)9 Term (org.apache.lucene.index.Term)7 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)7 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)4 KrillIndex (de.ids_mannheim.korap.KrillIndex)2 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)2 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)2 SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)2 SpanAttributeQuery (de.ids_mannheim.korap.query.SpanAttributeQuery)1 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)1 SpanTermWithIdQuery (de.ids_mannheim.korap.query.SpanTermWithIdQuery)1 SpanWithAttributeQuery (de.ids_mannheim.korap.query.SpanWithAttributeQuery)1 SpanQuery (org.apache.lucene.search.spans.SpanQuery)1