Search in sources :

Example 96 with SpanQueryWrapper

use of de.ids_mannheim.korap.query.wrap.SpanQueryWrapper 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 97 with SpanQueryWrapper

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

the class TestRegexWildcardIndex method indexRegexCaseInsensitive.

@Test
public void indexRegexCaseInsensitive() throws Exception {
    KrillIndex ki = new KrillIndex();
    // abcabcabac
    FieldDocument fd = new FieldDocument();
    fd.addTV("base", "AfFe aFfFE Baum Baumgarten SteinGarten franZ HaNs Haus Efeu effe", "[(0-4)s:AfFe|i:affe|_0$<i>0<i>4|-:t$<i>10]" + "[(5-10)s:aFfFE|i:afffe|_1$<i>5<i>10]" + "[(11-15)s:Baum|i:baum|_2$<i>11<i>15]" + "[(16-26)s:Baumgarten|i:baumgarten|_3$<i>16<i>26]" + "[(27-38)s:SteinGarten|i:steingarten|_4$<i>27<i>38]" + "[(39-44)s:franZ|i:franz|_5$<i>39<i>44]" + "[(45-49)s:HaNs|i:hans|_6$<i>45<i>49]" + "[(50-54)s:Haus|i:haus|_7$<i>50<i>54]" + "[(55-59)s:Efeu|i:efeu|_8$<i>55<i>59]" + "[(60-64)s:effe|i:effe|_9$<i>60<i>64]");
    ki.addDoc(fd);
    ki.commit();
    QueryBuilder kq = new QueryBuilder("base");
    SpanQueryWrapper sqw = kq.re("s:Af*e", true);
    assertEquals("SpanMultiTermQueryWrapper(base:/i:af*e/)", sqw.toQuery().toString());
    Krill ks = _newKrill(sqw);
    Result kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("[[AfFe]] aFfFE ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("AfFe [[aFfFE]] Baum ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").re("s:Af.*e"));
    kr = ki.search(ks);
    assertEquals((long) 1, kr.getTotalResults());
    assertEquals("[[AfFe]] aFfFE ...", kr.getMatch(0).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").re("s:baum.*", true));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("... aFfFE [[Baum]] Baumgarten ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... Baum [[Baumgarten]] SteinGarten ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").re("s:.*garten", true));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("... Baum [[Baumgarten]] SteinGarten ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... Baumgarten [[SteinGarten]] franZ ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").re("s:.*garten", false));
    kr = ki.search(ks);
    assertEquals((long) 1, kr.getTotalResults());
    assertEquals("... Baum [[Baumgarten]] SteinGarten ...", kr.getMatch(0).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").re("s:ha.s", true));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("... franZ [[HaNs]] Haus ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... HaNs [[Haus]] Efeu ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(new QueryBuilder("base").re("s:.*f*e", true));
    kr = ki.search(ks);
    assertEquals((long) 3, kr.getTotalResults());
    assertEquals("[[AfFe]] aFfFE ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("AfFe [[aFfFE]] Baum ...", kr.getMatch(1).getSnippetBrackets());
    assertEquals("... Efeu [[effe]]", kr.getMatch(2).getSnippetBrackets());
}
Also used : Krill(de.ids_mannheim.korap.Krill) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) KrillIndex(de.ids_mannheim.korap.KrillIndex) Result(de.ids_mannheim.korap.response.Result) Test(org.junit.Test)

Example 98 with SpanQueryWrapper

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

the class TestRegexWildcardIndex method indexRegex.

@Test
public void indexRegex() throws Exception {
    KrillIndex ki = new KrillIndex();
    // abcabcabac
    FieldDocument fd = new FieldDocument();
    fd.addTV("base", "affe afffe baum baumgarten steingarten franz hans haus efeu effe", "[(0-4)s:affe|_0$<i>0<i>4|-:t$<i>10]" + "[(5-10)s:afffe|_1$<i>5<i>10]" + "[(11-15)s:baum|_2$<i>11<i>15]" + "[(16-26)s:baumgarten|_3$<i>16<i>26]" + "[(27-38)s:steingarten|_4$<i>27<i>38]" + "[(39-44)s:franz|_5$<i>39<i>44]" + "[(45-49)s:hans|_6$<i>45<i>49]" + "[(50-54)s:haus|_7$<i>50<i>54]" + "[(55-59)s:efeu|_8$<i>55<i>59]" + "[(60-64)s:effe|_9$<i>60<i>64]");
    ki.addDoc(fd);
    ki.commit();
    QueryBuilder kq = new QueryBuilder("base");
    SpanQueryWrapper sqw = kq.re("s:af*e");
    assertEquals("SpanMultiTermQueryWrapper(base:/s:af*e/)", sqw.toQuery().toString());
    Krill ks = _newKrill(sqw);
    Result kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("[[affe]] afffe ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("affe [[afffe]] baum ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(kq.re("s:baum.*"));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("... afffe [[baum]] baumgarten ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... baum [[baumgarten]] steingarten ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(kq.re("s:.....?garten"));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("... baum [[baumgarten]] steingarten ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... baumgarten [[steingarten]] franz ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(kq.re("s:ha.s"));
    kr = ki.search(ks);
    assertEquals((long) 2, kr.getTotalResults());
    assertEquals("... franz [[hans]] haus ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("... hans [[haus]] efeu ...", kr.getMatch(1).getSnippetBrackets());
    ks = _newKrill(kq.re("s:.*ff.*"));
    kr = ki.search(ks);
    assertEquals((long) 3, kr.getTotalResults());
    assertEquals("[[affe]] afffe ...", kr.getMatch(0).getSnippetBrackets());
    assertEquals("affe [[afffe]] baum ...", kr.getMatch(1).getSnippetBrackets());
    assertEquals("... efeu [[effe]]", kr.getMatch(2).getSnippetBrackets());
}
Also used : Krill(de.ids_mannheim.korap.Krill) QueryBuilder(de.ids_mannheim.korap.query.QueryBuilder) SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) KrillIndex(de.ids_mannheim.korap.KrillIndex) Result(de.ids_mannheim.korap.response.Result) Test(org.junit.Test)

Example 99 with SpanQueryWrapper

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

the class TestSpanRelationQueryJSON method testTypedRelationWithAnnotationRegex.

@Test
public void testTypedRelationWithAnnotationRegex() throws QueryException {
    String filepath = getClass().getResource("/queries/relation/typed-relation-with-key-regex.json").getFile();
    SpanQueryWrapper sqwi = getJSONQuery(filepath);
    SpanQuery sq = sqwi.toQuery();
    assertEquals("focus(#[1,2]spanSegment(<tokens:corenlp/c:NP />, " + "focus(#2: spanSegment(spanRelation(SpanMultiTermQueryWrapper(tokens:/>:malt/d:.*/)), " + "<tokens:corenlp/c:VP />))))", sq.toString());
}
Also used : SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Example 100 with SpanQueryWrapper

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

the class TestSpanRelationQueryJSON method testMatchRelationSource.

@Test
public void testMatchRelationSource() throws QueryException {
    // 
    String filepath = getClass().getResource("/queries/relation/match-source.json").getFile();
    SpanQueryWrapper sqwi = getJSONQuery(filepath);
    SpanQuery sq = sqwi.toQuery();
    assertEquals("focus(#[1,2]spanSegment(spanRelation(tokens:>:mate/d:HEAD), <tokens:c:s />))", sq.toString());
}
Also used : SpanQueryWrapper(de.ids_mannheim.korap.query.wrap.SpanQueryWrapper) SpanQuery(org.apache.lucene.search.spans.SpanQuery) Test(org.junit.Test)

Aggregations

SpanQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanQueryWrapper)165 Test (org.junit.Test)150 SpanQuery (org.apache.lucene.search.spans.SpanQuery)65 KrillIndex (de.ids_mannheim.korap.KrillIndex)17 QueryException (de.ids_mannheim.korap.util.QueryException)14 KrillQuery (de.ids_mannheim.korap.KrillQuery)13 QueryBuilder (de.ids_mannheim.korap.query.QueryBuilder)11 Result (de.ids_mannheim.korap.response.Result)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)7 Krill (de.ids_mannheim.korap.Krill)5 SpanRepetitionQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanRepetitionQueryWrapper)3 TestSimple.getJsonString (de.ids_mannheim.korap.TestSimple.getJsonString)2 SpanClassQuery (de.ids_mannheim.korap.query.SpanClassQuery)2 SpanDistanceQuery (de.ids_mannheim.korap.query.SpanDistanceQuery)2 SpanWithinQuery (de.ids_mannheim.korap.query.SpanWithinQuery)2 SpanAlterQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanAlterQueryWrapper)2 SpanRegexQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanRegexQueryWrapper)2 SpanSegmentQueryWrapper (de.ids_mannheim.korap.query.wrap.SpanSegmentQueryWrapper)2 Term (org.apache.lucene.index.Term)2 SpanOrQuery (org.apache.lucene.search.spans.SpanOrQuery)2