use of de.ids_mannheim.korap.query.SpanFocusQuery in project Krill by KorAP.
the class TestMatchIndex method indexExampleExtend.
@Test
public void indexExampleExtend() 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]" + "[(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 SpanFocusQuery(new SpanNextQuery(new SpanClassQuery(new SpanTermQuery(new Term("base", "s:a")), (byte) 2), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:b")), (byte) 3)), (byte) 3);
kr = ki.search(sq, (short) 10);
assertEquals("totalResults", kr.getTotalResults(), 3);
Match km = kr.getMatch(0);
assertEquals("StartPos (0)", 1, km.startPos);
assertEquals("EndPos (0)", 2, km.endPos);
assertEquals("SnippetBrackets (0)", "a[[{3:b}]]cabcab ...", km.getSnippetBrackets());
sq = new SpanFocusQuery(new SpanFocusQuery(new SpanNextQuery(new SpanClassQuery(new SpanTermQuery(new Term("base", "s:a")), (byte) 2), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:b")), (byte) 3)), (byte) 3), (byte) 2);
kr = ki.search(sq, (short) 10);
km = kr.getMatch(0);
assertEquals("StartPos (0)", 0, km.startPos);
assertEquals("EndPos (0)", 1, km.endPos);
assertEquals("SnippetBrackets (0)", "[[{2:a}]]bcabca ...", km.getSnippetBrackets());
// TODO: Check ID
}
use of de.ids_mannheim.korap.query.SpanFocusQuery in project Krill by KorAP.
the class TestMatchIndex method indexExample1.
@Test
public void indexExample1() 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]" + "[(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:b")), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:a"))));
kr = ki.search(sq, (short) 10);
assertEquals("totalResults", kr.getTotalResults(), 1);
assertEquals("StartPos (0)", 7, kr.getMatch(0).startPos);
assertEquals("EndPos (0)", 9, kr.getMatch(0).endPos);
assertEquals("SnippetBrackets (0)", "... bcabca[[b{1:a}]]c", kr.getMatch(0).getSnippetBrackets());
assertEquals("Test no 'more' context", "<span class=\"context-left\"><span class=\"more\"></span>bcabca</span><span class=\"match\"><mark>b<mark class=\"class-1 level-0\">a</mark></mark></span><span class=\"context-right\">c</span>", kr.getMatch(0).getSnippetHTML());
sq = new SpanFocusQuery(new SpanNextQuery(new SpanTermQuery(new Term("base", "s:b")), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:a")))));
kr = ki.search(sq, (short) 10);
assertEquals("totalResults", kr.getTotalResults(), 1);
assertEquals("StartPos (0)", 8, kr.getMatch(0).startPos);
assertEquals("EndPos (0)", 9, kr.getMatch(0).endPos);
assertEquals("SnippetBrackets (0)", "... cabcab[[{1:a}]]c", kr.getMatch(0).getSnippetBrackets());
sq = new SpanFocusQuery(new SpanNextQuery(new SpanClassQuery(new SpanTermQuery(new Term("base", "s:a")), (byte) 2), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:b")), (byte) 3)), (byte) 3);
kr = ki.search(sq, (short) 10);
assertEquals("totalResults", kr.getTotalResults(), 3);
assertEquals("StartPos (0)", 1, kr.getMatch(0).startPos);
assertEquals("EndPos (0)", 2, kr.getMatch(0).endPos);
assertEquals("SnippetBrackets (0)", "a[[{3:b}]]cabcab ...", kr.getMatch(0).getSnippetBrackets());
assertEquals("<span class=\"context-left\">a</span><span class=\"match\"><mark><mark class=\"class-3 level-0\">b</mark></mark></span><span class=\"context-right\">cabcab<span class=\"more\"></span></span>", kr.getMatch(0).getSnippetHTML());
assertEquals("StartPos (1)", 4, kr.getMatch(1).startPos);
assertEquals("EndPos (1)", 5, kr.getMatch(1).endPos);
assertEquals("SnippetBrackets (1)", "abca[[{3:b}]]cabac", kr.getMatch(1).getSnippetBrackets());
assertEquals("<span class=\"context-left\">abca</span><span class=\"match\"><mark><mark class=\"class-3 level-0\">b</mark></mark></span><span class=\"context-right\">cabac</span>", kr.getMatch(1).getSnippetHTML());
assertEquals("StartPos (2)", 7, kr.getMatch(2).startPos);
assertEquals("EndPos (2)", 8, kr.getMatch(2).endPos);
assertEquals("SnippetBrackets (2)", "... bcabca[[{3:b}]]ac", kr.getMatch(2).getSnippetBrackets());
// abcabcabac
sq = new SpanFocusQuery(new SpanNextQuery(new SpanTermQuery(new Term("base", "s:a")), new SpanClassQuery(new SpanNextQuery(new SpanTermQuery(new Term("base", "s:b")), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:a")))), (byte) 2)), (byte) 2);
kr = ki.search(sq, (short) 10);
assertEquals("totalResults", kr.getTotalResults(), 1);
assertEquals("SnippetBrackets (0)", "... bcabca[[{2:b{1:a}}]]c", kr.getMatch(0).getSnippetBrackets());
assertEquals("SnippetHTML (0) 1", "<span class=\"context-left\"><span class=\"more\"></span>bcabca</span><span class=\"match\"><mark><mark class=\"class-2 level-0\">b<mark class=\"class-1 level-1\">a</mark></mark></mark></span><span class=\"context-right\">c</span>", kr.getMatch(0).getSnippetHTML());
// Offset tokens
kr = ki.search(sq, 0, (short) 10, true, (short) 2, true, (short) 2);
assertEquals("totalResults", kr.getTotalResults(), 1);
assertEquals("SnippetBrackets (0)", "... ca[[{2:b{1:a}}]]c", kr.getMatch(0).getSnippetBrackets());
// Offset Characters
kr = ki.search(sq, 0, (short) 10, false, (short) 1, false, (short) 0);
assertEquals("totalResults", kr.getTotalResults(), 1);
assertEquals("SnippetBrackets (0)", "... a[[{2:b{1:a}}]] ...", kr.getMatch(0).getSnippetBrackets());
assertEquals("SnippetHTML (0) 2", "<span class=\"context-left\"><span class=\"more\"></span>a</span><span class=\"match\"><mark><mark class=\"class-2 level-0\">b<mark class=\"class-1 level-1\">a</mark></mark></mark></span><span class=\"context-right\"><span class=\"more\"></span></span>", kr.getMatch(0).getSnippetHTML());
// Don't match the expected class!
sq = new SpanFocusQuery(new SpanNextQuery(new SpanClassQuery(new SpanTermQuery(new Term("base", "s:b")), (byte) 1), new SpanClassQuery(new SpanTermQuery(new Term("base", "s:c")), (byte) 2)), (byte) 3);
kr = ki.search(sq, (short) 10);
assertEquals("totalResults", kr.getTotalResults(), 0);
sq = new SpanFocusQuery(new SpanNextQuery(new SpanTermQuery(new Term("base", "s:a")), new SpanClassQuery(new SpanNextQuery(new SpanTermQuery(new Term("base", "s:b")), new SpanTermQuery(new Term("base", "s:c"))))));
kr = ki.search(sq, (short) 2);
assertEquals("totalResults", kr.getTotalResults(), 2);
assertEquals("StartPos (0)", 1, kr.getMatch(0).startPos);
assertEquals("EndPos (0)", 3, kr.getMatch(0).endPos);
assertEquals("SnippetBrackets (0)", "a[[{1:bc}]]abcaba ...", kr.getMatch(0).getSnippetBrackets());
assertEquals("StartPos (1)", 4, kr.getMatch(1).startPos);
assertEquals("EndPos (1)", 6, kr.getMatch(1).endPos);
assertEquals("SnippetBrackets (1)", "abca[[{1:bc}]]abac", kr.getMatch(1).getSnippetBrackets());
assertEquals(1, ki.numberOf("base", "documents"));
assertEquals(10, ki.numberOf("base", "t"));
}
use of de.ids_mannheim.korap.query.SpanFocusQuery in project Krill by KorAP.
the class TestFocusIndex method testCase12.
/**
* Check Skipto focus spans
*/
@Test
public void testCase12() throws IOException {
ki.addDoc(TestRelationIndex.createFieldDoc0());
ki.addDoc(TestRelationIndex.createFieldDoc1());
ki.commit();
SpanRelationQuery sq = new SpanRelationQuery(new SpanTermQuery(new Term("base", ">:xip/syntax-dep_rel")), true, RelationDirection.RIGHT);
sq.setSourceClass((byte) 1);
SpanFocusQuery sfq = new SpanFocusQuery(sq, (byte) 1);
sfq.setSorted(false);
SpanTermQuery stq = new SpanTermQuery(new Term("base", "s:c"));
SpanNextQuery snq = new SpanNextQuery(stq, sfq);
kr = ki.search(snq, (short) 20);
assertEquals(0, kr.getMatch(0).getStartPos());
assertEquals(2, kr.getMatch(0).getEndPos());
assertEquals(5, kr.getMatch(1).getStartPos());
assertEquals(9, kr.getMatch(1).getEndPos());
// for (Match m : kr.getMatches()) {
// System.out.println(m.getStartPos() + " " + m.getEndPos());
// }
}
use of de.ids_mannheim.korap.query.SpanFocusQuery 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;
}
use of de.ids_mannheim.korap.query.SpanFocusQuery 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());
}
Aggregations