Search in sources :

Example 1 with SpanRelationMatchQuery

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

the class TestRelationIndex method testCase8.

/**
 * Relations whose source/target do not embed
 * its counterparts.
 */
@Test
public void testCase8() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    // return source of dep relations to pos:NN
    SpanTermWithIdQuery tq = new SpanTermWithIdQuery(new Term("base", "pos:NN"), true);
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", "<:dep")), true, RelationDirection.LEFT);
    srq.setSourceClass((byte) 1);
    SpanRelationMatchQuery rm = new SpanRelationMatchQuery(srq, tq, true);
    SpanQuery rv = new SpanFocusQuery(rm, (byte) 1);
    kr = ki.search(rv, (short) 10);
    assertEquals((long) 3, kr.getTotalResults());
    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(4, kr.getMatch(2).getStartPos());
    assertEquals(5, kr.getMatch(2).getEndPos());
    // return target of dep relations from pos:NN
    srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:dep")), true, RelationDirection.RIGHT);
    srq.setTargetClass((byte) 1);
    rm = new SpanRelationMatchQuery(srq, tq, true);
    rv = new SpanFocusQuery(rm, (byte) 1);
    kr = ki.search(rv, (short) 10);
    assertEquals((long) 3, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(3, kr.getMatch(0).getEndPos());
    assertEquals(4, kr.getMatch(1).getStartPos());
    assertEquals(5, kr.getMatch(1).getEndPos());
    assertEquals(5, kr.getMatch(2).getStartPos());
    assertEquals(6, kr.getMatch(2).getEndPos());
}
Also used : SpanTermWithIdQuery(de.ids_mannheim.korap.query.SpanTermWithIdQuery) 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) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 2 with SpanRelationMatchQuery

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

the class TestRelationIndex method testCase9.

/**
 * Relation with specific sources and return any targets <br/>
 * Relation with specific sources and targets, return the targets
 * <br/>
 * Relation with specific sources and targets, return the sources
 *
 * @throws IOException
 */
@Test
public void testCase9() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    // return all children of 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.LEFT);
    srq.setSourceClass((byte) 2);
    SpanRelationMatchQuery rm = new SpanRelationMatchQuery(srq, scq1, true);
    SpanFocusQuery rv = new SpanFocusQuery(rm, (byte) 2);
    rv.setSorted(false);
    assertEquals("focus(2: focus(#[1,2]spanSegment({2: source:spanRelation(base:<:child-of)}, {1: <base:np />})))", rv.toString());
    kr = ki.search(rv, (short) 10);
    assertEquals((long) 7, kr.getTotalResults());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(1, kr.getMatch(0).getEndPos());
    assertEquals(2, kr.getMatch(1).getStartPos());
    assertEquals(3, kr.getMatch(1).getEndPos());
    assertEquals(2, kr.getMatch(2).getStartPos());
    assertEquals(4, kr.getMatch(2).getEndPos());
    assertEquals(3, kr.getMatch(3).getStartPos());
    assertEquals(4, kr.getMatch(3).getEndPos());
    assertEquals(4, kr.getMatch(4).getStartPos());
    assertEquals(7, kr.getMatch(4).getEndPos());
    assertEquals(5, kr.getMatch(5).getStartPos());
    assertEquals(6, kr.getMatch(5).getEndPos());
    assertEquals(6, kr.getMatch(6).getStartPos());
    assertEquals(7, kr.getMatch(6).getEndPos());
}
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 3 with SpanRelationMatchQuery

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

the class TestRelationIndex method testCase11.

/**
 * Matching both relation nodes whose a specific attribute
 */
@Test
public void testCase11() 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());
    SpanAttributeQuery aq2 = new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:case=accusative")), true);
    kr = ki.search(aq2, (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());
    SpanWithAttributeQuery swaq1 = new SpanWithAttributeQuery(aq, true);
    SpanWithAttributeQuery swaq2 = new SpanWithAttributeQuery(aq2, true);
    kr = ki.search(swaq1, (short) 10);
    assertEquals((long) 3, kr.getTotalResults());
    SpanRelationMatchQuery srmq = new SpanRelationMatchQuery(srq, swaq1, swaq2, true);
    assertEquals("focus(#[1,2]spanSegment(spanWithAttribute(spanAttribute(base:@:case=accusative)), " + "focus(#2: spanSegment({1: source:{2: target:spanRelation(base:>:child-of)}}, " + "spanWithAttribute(spanAttribute(base:@:case=accusative))))))", srmq.toString());
    kr = ki.search(srmq, (short) 10);
    assertEquals((long) 1, kr.getTotalResults());
    assertEquals(2, kr.getMatch(0).getStartPos());
    assertEquals(7, kr.getMatch(0).getEndPos());
}
Also used : SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) SpanRelationMatchQuery(de.ids_mannheim.korap.query.SpanRelationMatchQuery) SpanWithAttributeQuery(de.ids_mannheim.korap.query.SpanWithAttributeQuery) Term(org.apache.lucene.index.Term) Test(org.junit.Test)

Example 4 with SpanRelationMatchQuery

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

the class SpanRelationWrapper method toFragmentQuery.

@Override
public SpanQuery toFragmentQuery() throws QueryException {
    if (this.isNull() || this.isEmpty()) {
        return null;
    }
    SpanQuery relationTermQuery = relationQuery.retrieveNode(this.retrieveNode).toFragmentQuery();
    // .retrieveNode(this.retrieveNode).toFragmentQuery();
    if (relationTermQuery == null)
        return null;
    SpanRelationQuery rq = new SpanRelationQuery(relationTermQuery, true, direction);
    SpanQuery subq1, subq2;
    if (subQuery1.isEmpty) {
        if (!subQuery2.isEmpty) {
            // match target
            subq2 = subQuery2.retrieveNode(this.retrieveNode).toFragmentQuery();
            if (subQuery1.hasClass) {
                rq.setSourceClass(subQuery1.getClassNumber());
            }
            return new SpanRelationMatchQuery(rq, subq2, true);
        }
    } else if (subQuery2.isEmpty) {
        if (!subQuery1.isEmpty) {
            // match source
            subq1 = subQuery1.retrieveNode(this.retrieveNode).toFragmentQuery();
            if (subQuery2.hasClass) {
                rq.setTargetClass(subQuery2.getClassNumber());
            }
            return new SpanRelationMatchQuery(rq, subq1, true);
        }
    } else {
        // match both
        subq1 = subQuery1.retrieveNode(this.retrieveNode).toFragmentQuery();
        subq2 = subQuery2.retrieveNode(this.retrieveNode).toFragmentQuery();
        return new SpanRelationMatchQuery(rq, subq1, subq2, true);
    }
    // both empty
    if (subQuery1.hasClass) {
        rq.setSourceClass(subQuery1.getClassNumber());
    }
    if (subQuery2.hasClass) {
        rq.setTargetClass(subQuery2.getClassNumber());
    }
    SpanFocusQuery fq = new SpanFocusQuery(rq, rq.getTempClassNumbers());
    fq.setMatchTemporaryClass(true);
    fq.setRemoveTemporaryClasses(true);
    fq.setSorted(false);
    return fq;
}
Also used : SpanRelationQuery(de.ids_mannheim.korap.query.SpanRelationQuery) SpanRelationMatchQuery(de.ids_mannheim.korap.query.SpanRelationMatchQuery) SpanFocusQuery(de.ids_mannheim.korap.query.SpanFocusQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 5 with SpanRelationMatchQuery

use of de.ids_mannheim.korap.query.SpanRelationMatchQuery 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)

Aggregations

SpanRelationMatchQuery (de.ids_mannheim.korap.query.SpanRelationMatchQuery)9 SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)9 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)8 Term (org.apache.lucene.index.Term)8 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)8 Test (org.junit.Test)8 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)6 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)6 SpanQuery (org.apache.lucene.search.spans.SpanQuery)3 KrillIndex (de.ids_mannheim.korap.KrillIndex)2 DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)2 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)2 SpanReferenceQuery (de.ids_mannheim.korap.query.SpanReferenceQuery)2 SpanTermWithIdQuery (de.ids_mannheim.korap.query.SpanTermWithIdQuery)2 SpanAttributeQuery (de.ids_mannheim.korap.query.SpanAttributeQuery)1 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)1 SpanWithAttributeQuery (de.ids_mannheim.korap.query.SpanWithAttributeQuery)1 SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)1