Search in sources :

Example 16 with SpanRelationQuery

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

the class TestRelationIndex method testCase10a.

/**
 * Relation whose nodes have a specific attribute.
 */
@Test
public void testCase10a() 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
    SpanFocusQuery sfq1 = new SpanFocusQuery(srq, (byte) 1);
    SpanWithAttributeQuery swaq = new SpanWithAttributeQuery(sfq1, aq, true);
    kr = ki.search(swaq, (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());
    // Returning relations whose source has a specific attribute
    SpanFocusQuery fqr = new SpanFocusQuery(swaq, srq.getTempClassNumbers());
    fqr.setMatchTemporaryClass(true);
    fqr.setRemoveTemporaryClasses(true);
    assertEquals("focus(#[1,2]spanRelationWithAttribute(focus(1: " + "{1: source:{2: target:spanRelation(base:>:child-of)}}), " + "spanAttribute(base:@:case=accusative)))", fqr.toString());
    kr = ki.search(fqr, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(7, kr.getMatch(0).getEndPos());
    assertEquals(4, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    // Matching relation target nodes with an attribute
    SpanFocusQuery sfq2 = new SpanFocusQuery(srq, (byte) 2);
    sfq2.setSorted(false);
    SpanWithAttributeQuery swaq2 = new SpanWithAttributeQuery(sfq2, aq, true);
    kr = ki.search(aq, (short) 20);
    // for (Match m : kr.getMatches()) {
    // System.out.println(m.getStartPos() + " " + m.getEndPos());
    // }
    kr = ki.search(swaq2, (short) 10);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals(5, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    // Returning relations whose target has a specific attribute
    SpanFocusQuery fqr2 = new SpanFocusQuery(swaq2, srq.getTempClassNumbers());
    fqr2.setMatchTemporaryClass(true);
    fqr2.setRemoveTemporaryClasses(true);
    kr = ki.search(fqr2, (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());
}
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) Term(org.apache.lucene.index.Term) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) Test(org.junit.Test)

Example 17 with SpanRelationQuery

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

the class TestRelationIndex method testCase7.

/**
 * Relation with specific sources, return any targets
 */
@Test
public void testCase7() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    // match all children that are NP
    SpanElementQuery seq1 = new SpanElementQuery("base", "np");
    SpanClassQuery scq1 = new SpanClassQuery(seq1, (byte) 1);
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:child-of")), true, RelationDirection.RIGHT);
    srq.setTargetClass((byte) 2);
    SpanRelationMatchQuery rm = new SpanRelationMatchQuery(srq, scq1, true);
    // SpanQuery rv = new SpanFocusQuery(rm, (byte) 1);
    // return all parents of np
    SpanFocusQuery rv2 = new SpanFocusQuery(rm, (byte) 2);
    rv2.setSorted(false);
    assertEquals("focus(2: focus(#[1,2]spanSegment({2: target:spanRelation(base:>:child-of)}, {1: <base:np />})))", rv2.toString());
    kr = ki.search(rv2, (short) 10);
    assertEquals((long) 4, kr.getTotalResults());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(7, kr.getMatch(0).getEndPos());
    assertEquals(1, kr.getMatch(1).getStartPos());
    assertEquals(7, kr.getMatch(1).getEndPos());
    assertEquals(2, kr.getMatch(2).getStartPos());
    assertEquals(7, kr.getMatch(2).getEndPos());
    assertEquals(4, kr.getMatch(3).getStartPos());
    assertEquals(7, kr.getMatch(3).getEndPos());
// id problem (solved)
// return all parents of np that are PP
}
Also used : SpanClassQuery(de.ids_mannheim.korap.query.SpanClassQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) SpanRelationMatchQuery(de.ids_mannheim.korap.query.SpanRelationMatchQuery) Term(org.apache.lucene.index.Term) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) Test(org.junit.Test)

Example 18 with SpanRelationQuery

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

the class TestRelationIndex method testCase1.

/**
 * Relations: token to token, token to span, span to span
 */
@Test
public void testCase1() throws IOException {
    ki.addDoc(createFieldDoc0());
    ki.addDoc(createFieldDoc1());
    ki.commit();
    SpanRelationQuery sq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:xip/syntax-dep_rel")), true, RelationDirection.RIGHT);
    kr = ki.search(sq, (short) 10);
    assertEquals((long) 7, kr.getTotalResults());
    // token to token
    assertEquals(0, kr.getMatch(0).getLocalDocID());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(1, kr.getMatch(0).getEndPos());
    assertEquals(1, kr.getMatch(1).getStartPos());
    assertEquals(2, kr.getMatch(1).getEndPos());
    assertEquals(9, kr.getMatch(2).getStartPos());
    assertEquals(10, kr.getMatch(2).getEndPos());
    assertEquals(9, kr.getMatch(3).getStartPos());
    assertEquals(10, kr.getMatch(3).getEndPos());
    // token to span
    assertEquals(1, kr.getMatch(4).getLocalDocID());
    assertEquals(0, kr.getMatch(4).getStartPos());
    assertEquals(1, kr.getMatch(4).getEndPos());
    assertEquals(0, kr.getMatch(5).getStartPos());
    assertEquals(3, kr.getMatch(5).getEndPos());
    // span to span
    assertEquals(6, kr.getMatch(6).getStartPos());
    assertEquals(9, kr.getMatch(6).getEndPos());
// check target
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) Term(org.apache.lucene.index.Term) Test(org.junit.Test)

Example 19 with SpanRelationQuery

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

the class TestRelationIndex method testCase4.

/**
 * Relations only with/out attribute
 */
@Test
public void testCase4() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:child-of")), true, RelationDirection.RIGHT);
    SpanFocusQuery fq = new SpanFocusQuery(srq, srq.getTempClassNumbers());
    fq.setMatchTemporaryClass(true);
    fq.setRemoveTemporaryClasses(true);
    fq.setSorted(false);
    // kr = ki.search(fq, (short) 20);
    // for (Match m : kr.getMatches()) {
    // System.out.println(m.getStartPos() + " " + m.getEndPos());
    // }
    SpanAttributeQuery saq = new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:func=sbj")), true);
    // kr = ki.search(saq, (short) 20);
    // child-of with attr func=sbj
    SpanWithAttributeQuery wq;
    wq = new SpanWithAttributeQuery(fq, saq, true);
    // kr = ki.search(wq, (short) 20);
    // assertEquals((long) 1, kr.getTotalResults());
    // assertEquals(0, kr.getMatch(0).getStartPos()); // token
    // assertEquals(7, kr.getMatch(0).getEndPos());
    // child-of without attr func=sbj
    wq = new SpanWithAttributeQuery(fq, new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:func=sbj")), true, true), true);
    kr = ki.search(wq, (short) 20);
    assertEquals((long) 12, kr.getTotalResults());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) SpanWithAttributeQuery(de.ids_mannheim.korap.query.SpanWithAttributeQuery) Term(org.apache.lucene.index.Term) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) Test(org.junit.Test)

Example 20 with SpanRelationQuery

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

the class TestRelationIndex method testCase2.

/**
 * Relation span to token
 */
@Test
public void testCase2() throws IOException {
    ki.addDoc(createFieldDoc0());
    ki.addDoc(createFieldDoc1());
    ki.commit();
    SpanRelationQuery sq = new SpanRelationQuery(new SpanTermQuery(new Term("base", "<:xip/syntax-dep_rel")), true, RelationDirection.LEFT);
    kr = ki.search(sq, (short) 10);
    assertEquals((long) 7, kr.getTotalResults());
    // token to token
    assertEquals(0, kr.getMatch(0).getLocalDocID());
    assertEquals(1, kr.getMatch(0).getStartPos());
    assertEquals(2, kr.getMatch(0).getEndPos());
    assertEquals(3, kr.getMatch(1).getStartPos());
    assertEquals(4, kr.getMatch(1).getEndPos());
    assertEquals(4, kr.getMatch(2).getStartPos());
    assertEquals(5, kr.getMatch(2).getEndPos());
    assertEquals(6, kr.getMatch(3).getStartPos());
    assertEquals(7, kr.getMatch(3).getEndPos());
    assertEquals(1, kr.getMatch(4).getLocalDocID());
    // span to token
    assertEquals(6, kr.getMatch(4).getStartPos());
    assertEquals(9, kr.getMatch(4).getEndPos());
    assertEquals(6, kr.getMatch(5).getStartPos());
    assertEquals(9, kr.getMatch(5).getEndPos());
    // span to span
    assertEquals(9, kr.getMatch(6).getStartPos());
    assertEquals(10, kr.getMatch(6).getEndPos());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) Term(org.apache.lucene.index.Term) Test(org.junit.Test)

Aggregations

SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)20 Term (org.apache.lucene.index.Term)19 Test (org.junit.Test)19 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)18 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)15 SpanRelationMatchQuery (de.ids_mannheim.korap.query.SpanRelationMatchQuery)9 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)8 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)7 SpanAttributeQuery (de.ids_mannheim.korap.query.SpanAttributeQuery)5 SpanWithAttributeQuery (de.ids_mannheim.korap.query.SpanWithAttributeQuery)5 SpanQuery (org.apache.lucene.search.spans.SpanQuery)4 SpanTermWithIdQuery (de.ids_mannheim.korap.query.SpanTermWithIdQuery)3 KrillIndex (de.ids_mannheim.korap.KrillIndex)2 DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)2 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)2 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)2 SpanReferenceQuery (de.ids_mannheim.korap.query.SpanReferenceQuery)2 SpanSegmentQuery (de.ids_mannheim.korap.query.SpanSegmentQuery)2 SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)1 RegexQuery (org.apache.lucene.sandbox.queries.regex.RegexQuery)1