blob: 6bc0c23967b770425e12497e8701785e6cf7572d [file] [log] [blame]
Eliza Margarethaa2603fa2014-01-22 10:59:25 +00001package de.ids_mannheim.korap.query.spans;
2
3import java.io.IOException;
4import java.util.ArrayList;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +00005import java.util.List;
6import java.util.Map;
7
8import org.apache.lucene.index.AtomicReaderContext;
9import org.apache.lucene.index.Term;
10import org.apache.lucene.index.TermContext;
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000011import org.apache.lucene.search.spans.Spans;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000012import org.apache.lucene.util.Bits;
13import org.slf4j.Logger;
14import org.slf4j.LoggerFactory;
15
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000016import de.ids_mannheim.korap.query.SimpleSpanQuery;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000017import de.ids_mannheim.korap.query.SpanDistanceQuery;
18
Eliza Margaretha8e274e32014-01-28 15:09:30 +000019/** DistanceSpan is a base class for enumeration of span matches,
20 * whose two child spans have a specific range of distance (within
Eliza Margaretha9738c392014-02-03 17:04:53 +000021 * a min and a max distance) and must be in order (a firstspan is
22 * followed by a secondspan).
Eliza Margaretha8e274e32014-01-28 15:09:30 +000023 *
24 * @author margaretha
25 * */
Eliza Margaretha795937c2014-02-06 13:08:28 +000026public abstract class DistanceSpans extends SimpleSpans{
Eliza Margaretha83b95372014-01-23 09:18:07 +000027
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000028 protected CandidateSpan matchFirstSpan,matchSecondSpan;
29 protected Logger log = LoggerFactory.getLogger(DistanceSpans.class);
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000030
Eliza Margaretha795937c2014-02-06 13:08:28 +000031 public DistanceSpans(SpanDistanceQuery query,
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000032 AtomicReaderContext context, Bits acceptDocs,
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000033 Map<Term, TermContext> termContexts) throws IOException {
Eliza Margaretha83b95372014-01-23 09:18:07 +000034 super(query, context, acceptDocs, termContexts);
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000035 }
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000036
37 @Override
38 public boolean next() throws IOException {
39 isStartEnumeration=false;
Eliza Margaretha83b95372014-01-23 09:18:07 +000040 matchPayload.clear();
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000041 return advance();
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000042 }
43
44
45 /** Find the next span match.
46 * @return true iff a span match is available.
Eliza Margaretha8e274e32014-01-28 15:09:30 +000047 * */
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000048 protected abstract boolean advance() throws IOException;
49
50 /** Find the same doc shared by element, firstspan and secondspan.
51 * @return true iff such a doc is found.
52 * */
53 protected boolean findSameDoc(Spans x,
54 Spans y, Spans e) throws IOException{
55
56 while (hasMoreSpans) {
57 if (ensureSameDoc(x, y) &&
58 e.doc() == x.doc()){
59 return true;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000060 }
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000061 if (!ensureSameDoc(e,y)){
62 return false;
63 };
64 }
65 return false;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000066 }
67
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000068 public CandidateSpan getMatchFirstSpan() {
69 return matchFirstSpan;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000070 }
71
Eliza Margaretha198e4ef2014-02-10 13:50:50 +000072 public void setMatchFirstSpan(CandidateSpan matchFirstSpan) {
73 this.matchFirstSpan = matchFirstSpan;
74 }
75
76 public CandidateSpan getMatchSecondSpan() {
77 return matchSecondSpan;
78 }
79
80 public void setMatchSecondSpan(CandidateSpan matchSecondSpan) {
81 this.matchSecondSpan = matchSecondSpan;
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000082 }
83
Eliza Margarethaa2603fa2014-01-22 10:59:25 +000084}