Search in sources :

Example 1 with SpanAttributeQuery

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

the class TestAttributeIndex method testCase7.

/**
 * Arbitrary elements with an attribute and a not attribute.
 */
@Test
public void testCase7() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    List<SpanQuery> sql = new ArrayList<>();
    sql.add(new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=header")), true, true));
    sql.add(new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:class=book")), true));
    SpanWithAttributeQuery swaq = new SpanWithAttributeQuery(sql, true);
    kr = ki.search(swaq, (short) 10);
    assertEquals(4, kr.getTotalResults());
    assertEquals(0, kr.getMatch(0).getStartPos());
    assertEquals(5, kr.getMatch(0).getEndPos());
    assertEquals(1, kr.getMatch(1).getStartPos());
    assertEquals(2, kr.getMatch(1).getEndPos());
    assertEquals(2, kr.getMatch(2).getStartPos());
    assertEquals(5, kr.getMatch(2).getEndPos());
    assertEquals(6, kr.getMatch(3).getStartPos());
    assertEquals(7, kr.getMatch(3).getEndPos());
// for (int i = 0; i < kr.getTotalResults(); i++) {
// System.out.println(kr.getMatch(i).getLocalDocID() + " "
// + kr.getMatch(i).startPos + " " + kr.getMatch(i).endPos);
// }
}
Also used : SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) ArrayList(java.util.ArrayList) SpanWithAttributeQuery(de.ids_mannheim.korap.query.SpanWithAttributeQuery) Term(org.apache.lucene.index.Term) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 2 with SpanAttributeQuery

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

the class SpansWithAttribute method setAttributeList.

public void setAttributeList(SpanWithAttributeQuery swaq, LeafReaderContext context, Bits acceptDocs, Map<Term, TermContext> termContexts) throws IOException {
    attributeList = new ArrayList<AttributeSpans>();
    notAttributeList = new ArrayList<AttributeSpans>();
    List<SpanQuery> attributeList = swaq.getClauseList();
    if (swaq.isMultipleAttributes) {
        if (attributeList != null) {
            for (SpanQuery sq : attributeList) {
                addAttributes((SpanAttributeQuery) sq, context, acceptDocs, termContexts);
            }
        } else {
            throw new NullPointerException("Attribute list is null.");
        }
    } else if (swaq.getSecondClause() != null) {
        addAttributes((SpanAttributeQuery) swaq.getSecondClause(), context, acceptDocs, termContexts);
    } else if (swaq.getType().equals("spanWithAttribute") && swaq.getFirstClause() != null) {
        addAttributes((SpanAttributeQuery) swaq.getFirstClause(), context, acceptDocs, termContexts);
    } else {
        throw new NullPointerException("No attribute is defined.");
    }
}
Also used : SpanAttributeQuery(de.ids_mannheim.korap.query.SpanAttributeQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery)

Example 3 with SpanAttributeQuery

use of de.ids_mannheim.korap.query.SpanAttributeQuery 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 4 with SpanAttributeQuery

use of de.ids_mannheim.korap.query.SpanAttributeQuery 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 5 with SpanAttributeQuery

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

the class TestRelationIndex method testCase5.

/**
 * Dependency relations with attribute
 */
@Test
public void testCase5() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    // target of a dependency relation
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", "<:dep")), true, RelationDirection.LEFT);
    kr = ki.search(srq, (short) 10);
    assertEquals((long) 6, kr.getTotalResults());
    SpanFocusQuery fq = new SpanFocusQuery(srq, srq.getTempClassNumbers());
    fq.setMatchTemporaryClass(true);
    fq.setRemoveTemporaryClasses(true);
    // fq.setSorted(false);
    kr = ki.search(fq, (short) 10);
    assertEquals((long) 6, kr.getTotalResults());
    SpanAttributeQuery aq = new SpanAttributeQuery(new SpanTermQuery(new Term("base", "@:func=head")), true);
    kr = ki.search(aq, (short) 10);
    // dependency relation, which is also a head
    SpanWithAttributeQuery wq = new SpanWithAttributeQuery(fq, aq, true);
    kr = ki.search(wq, (short) 20);
    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 : 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)

Aggregations

SpanAttributeQuery (de.ids_mannheim.korap.query.SpanAttributeQuery)16 SpanWithAttributeQuery (de.ids_mannheim.korap.query.SpanWithAttributeQuery)15 Term (org.apache.lucene.index.Term)14 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)14 Test (org.junit.Test)14 SpanQuery (org.apache.lucene.search.spans.SpanQuery)9 ArrayList (java.util.ArrayList)7 SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)6 SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)5 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)4 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)1 SpanRelationMatchQuery (de.ids_mannheim.korap.query.SpanRelationMatchQuery)1 SpanSegmentQuery (de.ids_mannheim.korap.query.SpanSegmentQuery)1