Search in sources :

Example 41 with SpanElementQuery

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

the class TestNextIndex method indexExample2.

@Test
public void indexExample2() throws IOException {
    KrillIndex ki = new KrillIndex();
    // abcabcabac
    FieldDocument fd = new FieldDocument();
    fd.addTV("base", "abcabcabac", "[(0-1)s:a|i:a|_0$<i>0<i>1|-:t$<i>10]" + "[(1-2)s:b|i:b|_1$<i>1<i>2]" + "[(2-3)s:c|i:c|_2$<i>2<i>3]" + "[(3-4)s:a|i:a|_3$<i>3<i>4|<>:x$<b>64<i>3<i>4<i>4<b>0|<>:x$<b>64<i>3<i>7<i>7<b>0]" + "[(4-5)s:b|i:b|_4$<i>4<i>5]" + "[(5-6)s:c|i:c|_5$<i>5<i>6]" + "[(6-7)s:a|i:a|_6$<i>6<i>7]" + "[(7-8)s:b|i:b|_7$<i>7<i>8]" + "[(8-9)s:a|i:a|_8$<i>8<i>9]" + "[(9-10)s:c|i:c|_9$<i>9<i>10]");
    ki.addDoc(fd);
    ki.commit();
    SpanQuery sq;
    Result kr;
    sq = new SpanNextQuery(new SpanTermQuery(new Term("base", "s:c")), new SpanElementQuery("base", "x"));
    kr = ki.search(sq, (short) 10);
    assertEquals("ab[[cabca]]bac", kr.getMatch(1).getSnippetBrackets());
}
Also used : SpanTermQuery(org.apache.lucene.search.spans.SpanTermQuery) Term(org.apache.lucene.index.Term) KrillIndex(de.ids_mannheim.korap.KrillIndex) SpanElementQuery(de.ids_mannheim.korap.query.SpanElementQuery) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Result(de.ids_mannheim.korap.response.Result) SpanNextQuery(de.ids_mannheim.korap.query.SpanNextQuery) Test(org.junit.Test)

Example 42 with SpanElementQuery

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

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

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

the class TestRelationIndex method testCase6.

/**
 * Relation directions <br/>
 * Relation with specific sources, return the sources
 */
@Test
public void testCase6() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    // return 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);
    SpanRelationMatchQuery rm = new SpanRelationMatchQuery(srq, scq1, true);
    SpanFocusQuery rv = new SpanFocusQuery(rm, (byte) 1);
    rv.setSorted(false);
    assertEquals("focus(1: focus(#[1,2]spanSegment(spanRelation(base:>:child-of), {1: <base:np />})))", rv.toString());
    kr = ki.search(rv, (short) 10);
    assertEquals(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());
    // return all parents that are NP
    srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", "<:child-of")), true, RelationDirection.LEFT);
    rm = new SpanRelationMatchQuery(srq, scq1, true);
    rv = new SpanFocusQuery(rm, (byte) 1);
    kr = ki.search(rv, (short) 10);
    assertEquals(7, 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(4, kr.getMatch(2).getEndPos());
    assertEquals(2, kr.getMatch(3).getStartPos());
    assertEquals(7, kr.getMatch(3).getEndPos());
    assertEquals(2, kr.getMatch(4).getStartPos());
    assertEquals(7, kr.getMatch(4).getEndPos());
    assertEquals(5, kr.getMatch(5).getStartPos());
    assertEquals(7, kr.getMatch(5).getEndPos());
    assertEquals(5, 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 45 with SpanElementQuery

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

the class TestRelationIndex method testCase9b.

@Test
public void testCase9b() throws IOException {
    ki.addDoc(createFieldDoc2());
    ki.commit();
    SpanTermWithIdQuery tiq = new SpanTermWithIdQuery(new Term("base", "pos:ART"), true);
    SpanClassQuery scq1 = new SpanClassQuery(tiq, (byte) 1);
    SpanElementQuery seq = new SpanElementQuery("base", "np");
    SpanClassQuery scq2 = new SpanClassQuery(seq, (byte) 2);
    SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:child-of")), true, RelationDirection.RIGHT);
    srq.setSourceClass((byte) 1);
    srq.setTargetClass((byte) 2);
    SpanRelationMatchQuery rm = new SpanRelationMatchQuery(srq, scq1, scq2, true);
    // return all nps whose children are articles
    SpanFocusQuery rv = new SpanFocusQuery(rm, (byte) 2);
    rv.setSorted(false);
    kr = ki.search(rv, (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());
// 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) 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)

Aggregations

SpanElementQuery (de.ids_mannheim.korap.query.SpanElementQuery)57 Test (org.junit.Test)53 KrillIndex (de.ids_mannheim.korap.KrillIndex)37 Term (org.apache.lucene.index.Term)36 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)35 SpanQuery (org.apache.lucene.search.spans.SpanQuery)32 Result (de.ids_mannheim.korap.response.Result)26 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)16 SpanWithinQuery (de.ids_mannheim.korap.query.SpanWithinQuery)16 DistanceConstraint (de.ids_mannheim.korap.query.DistanceConstraint)11 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)11 SpanFocusQuery (de.ids_mannheim.korap.query.SpanFocusQuery)10 SpanNextQuery (de.ids_mannheim.korap.query.SpanNextQuery)9 SpanRelationQuery (de.ids_mannheim.korap.query.SpanRelationQuery)8 SpanAttributeQuery (de.ids_mannheim.korap.query.SpanAttributeQuery)6 SpanClassFilterQuery (de.ids_mannheim.korap.query.SpanClassFilterQuery)6 SpanRelationMatchQuery (de.ids_mannheim.korap.query.SpanRelationMatchQuery)6 SpanWithAttributeQuery (de.ids_mannheim.korap.query.SpanWithAttributeQuery)6 SpanSegmentQuery (de.ids_mannheim.korap.query.SpanSegmentQuery)4 ArrayList (java.util.ArrayList)4