use of de.ids_mannheim.korap.query.SpanRelationQuery 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());
// }
}
use of de.ids_mannheim.korap.query.SpanRelationQuery in project Krill by KorAP.
the class TestRelationIndex method testRelationWithRegex.
@Test
public void testRelationWithRegex() throws IOException {
ki.addDoc(createFieldDoc0());
ki.addDoc(createFieldDoc3());
ki.commit();
SpanQuery sq;
sq = new SpanRelationQuery(new SpanMultiTermQueryWrapper<RegexQuery>(new RegexQuery(new Term("base", ">:xip/.*"))), true, RelationDirection.RIGHT);
kr = ki.search(sq, (short) 10);
assertEquals((long) 7, kr.getTotalResults());
sq = new SpanRelationQuery(new SpanMultiTermQueryWrapper<RegexQuery>(new RegexQuery(new Term("base", "<:xip/.*"))), true, RelationDirection.LEFT);
kr = ki.search(sq, (short) 10);
assertEquals((long) 7, kr.getTotalResults());
}
use of de.ids_mannheim.korap.query.SpanRelationQuery 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());
}
use of de.ids_mannheim.korap.query.SpanRelationQuery 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());
// }
}
use of de.ids_mannheim.korap.query.SpanRelationQuery in project Krill by KorAP.
the class TestRelationIndex method testCase9c.
@Test
public void testCase9c() throws IOException {
ki.addDoc(createFieldDoc2());
ki.commit();
SpanTermWithIdQuery tiq = new SpanTermWithIdQuery(new Term("base", "pos:ART"), true);
SpanClassQuery scq1 = new SpanClassQuery(tiq, (byte) 1);
kr = ki.search(scq1, (short) 10);
assertEquals((long) 2, kr.getTotalResults());
assertEquals(2, kr.getMatch(0).getStartPos());
assertEquals(3, kr.getMatch(0).getEndPos());
assertEquals(5, kr.getMatch(1).getStartPos());
assertEquals(6, kr.getMatch(1).getEndPos());
SpanRelationQuery srq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:child-of")), true, RelationDirection.RIGHT);
srq.setSourceClass((byte) 1);
srq.setTargetClass((byte) 2);
// match articles as relation sources (left side)
SpanSegmentQuery ssq1 = new SpanSegmentQuery(srq, scq1);
// NOTE: SegmentSpans can only always adopt the relation left
// side id.
kr = ki.search(ssq1, (short) 10);
assertEquals(2, kr.getMatch(0).getStartPos());
assertEquals(3, kr.getMatch(0).getEndPos());
assertEquals(5, kr.getMatch(1).getStartPos());
assertEquals(6, kr.getMatch(1).getEndPos());
// return all parents of articles
SpanFocusQuery sfq = new SpanFocusQuery(ssq1, (byte) 2);
sfq.setSorted(false);
kr = ki.search(sfq, (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());
SpanElementQuery seq = new SpanElementQuery("base", "np");
SpanClassQuery scq2 = new SpanClassQuery(seq, (byte) 2);
kr = ki.search(scq2, (short) 10);
assertEquals((long) 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());
// match nps as relation targets (right side)
// return all nps whose children are articles
SpanSegmentQuery ssq2 = new SpanSegmentQuery(sfq, scq2);
kr = ki.search(ssq2, (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());
// NOTE: cannot match the span id of the embedded relation
// target and the np. So the segment match here is only by
// positions. All nps in the same position will be matches.
// return the articles whos parents are nps
SpanFocusQuery sfq2 = new SpanFocusQuery(ssq2, (byte) 1);
sfq2.setSorted(false);
assertEquals("focus(1: spanSegment(focus(2: spanSegment({1: source:" + "{2: target:spanRelation(base:>:child-of)}}, " + "{1: spanTermWithId(base:pos:ART)})), " + "{2: <base:np />}))", sfq2.toString());
kr = ki.search(sfq2, (short) 10);
assertEquals((long) 2, kr.getTotalResults());
assertEquals(2, kr.getMatch(0).getStartPos());
assertEquals(3, kr.getMatch(0).getEndPos());
assertEquals(5, kr.getMatch(1).getStartPos());
assertEquals(6, kr.getMatch(1).getEndPos());
// for (Match m : kr.getMatches()) {
// System.out.println(m.getStartPos() + " " + m.getEndPos());
// }
}
Aggregations