| Eliza Margaretha | a2603fa | 2014-01-22 10:59:25 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.spans; |
| 2 | |
| 3 | import java.io.IOException; |
| Eliza Margaretha | a2603fa | 2014-01-22 10:59:25 +0000 | [diff] [blame] | 4 | import java.util.Map; |
| 5 | |
| Akron | 700c1eb | 2015-09-25 16:57:30 +0200 | [diff] [blame] | 6 | import org.apache.lucene.index.LeafReaderContext; |
| Eliza Margaretha | a2603fa | 2014-01-22 10:59:25 +0000 | [diff] [blame] | 7 | import org.apache.lucene.index.Term; |
| 8 | import org.apache.lucene.index.TermContext; |
| Eliza Margaretha | a2603fa | 2014-01-22 10:59:25 +0000 | [diff] [blame] | 9 | import org.apache.lucene.util.Bits; |
| Eliza Margaretha | a2603fa | 2014-01-22 10:59:25 +0000 | [diff] [blame] | 10 | |
| Eliza Margaretha | 609fcc6 | 2014-02-13 14:10:20 +0000 | [diff] [blame] | 11 | import de.ids_mannheim.korap.query.SpanDistanceQuery; |
| 12 | import de.ids_mannheim.korap.query.SpanMultipleDistanceQuery; |
| Eliza Margaretha | a2603fa | 2014-01-22 10:59:25 +0000 | [diff] [blame] | 13 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 14 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 15 | * DistanceSpan is a base class for enumeration of span matches, whose |
| Eliza Margaretha | dc98dc1 | 2016-11-16 14:33:42 +0100 | [diff] [blame] | 16 | * two child spans have a specific range of distance (within a min and |
| 17 | * a max distance) and other constraints (i.e. order and |
| 18 | * co-occurrence) depending on the {@link SpanDistanceQuery}. All |
| 19 | * distance related spans extends this class. |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 20 | * |
| 21 | * @see DistanceExclusionSpans |
| 22 | * @see ElementDistanceExclusionSpans |
| 23 | * @see OrderedDistanceSpans |
| 24 | * @see UnorderedDistanceSpans |
| 25 | * @see MultipleDistanceSpans |
| Eliza Margaretha | 8e274e3 | 2014-01-28 15:09:30 +0000 | [diff] [blame] | 26 | * |
| 27 | * @author margaretha |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 28 | */ |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 29 | public abstract class DistanceSpans extends SimpleSpans { |
| Eliza Margaretha | e18d62e | 2014-02-11 11:30:48 +0000 | [diff] [blame] | 30 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 31 | protected CandidateSpan matchFirstSpan, matchSecondSpan; |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 32 | protected boolean exclusion; // for MultipleDistanceQuery |
| Eliza Margaretha | 198e4ef | 2014-02-10 13:50:50 +0000 | [diff] [blame] | 33 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 34 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 35 | /** |
| 36 | * Constructs a DistanceSpans enumeration for the given |
| 37 | * {@link SpanDistanceQuery}. |
| 38 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 39 | * @param query |
| 40 | * a SpanDistanceQuery |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 41 | * @param context |
| 42 | * @param acceptDocs |
| 43 | * @param termContexts |
| 44 | * @throws IOException |
| 45 | */ |
| Akron | 700c1eb | 2015-09-25 16:57:30 +0200 | [diff] [blame] | 46 | public DistanceSpans (SpanDistanceQuery query, LeafReaderContext context, |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 47 | Bits acceptDocs, Map<Term, TermContext> termContexts) |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 48 | throws IOException { |
| 49 | super(query, context, acceptDocs, termContexts); |
| 50 | exclusion = query.isExclusion(); |
| 51 | } |
| Eliza Margaretha | 198e4ef | 2014-02-10 13:50:50 +0000 | [diff] [blame] | 52 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 53 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 54 | /** |
| 55 | * Constructs a DistanceSpans enumeration for the given |
| 56 | * {@link SpanMultipleDistanceQuery}. |
| 57 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 58 | * @param query |
| 59 | * a SpanMultipleDistanceQuery |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 60 | * @param context |
| 61 | * @param acceptDocs |
| 62 | * @param termContexts |
| 63 | * @throws IOException |
| 64 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 65 | public DistanceSpans (SpanMultipleDistanceQuery query, |
| Akron | 700c1eb | 2015-09-25 16:57:30 +0200 | [diff] [blame] | 66 | LeafReaderContext context, Bits acceptDocs, |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 67 | Map<Term, TermContext> termContexts) |
| 68 | throws IOException { |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 69 | super(query, context, acceptDocs, termContexts); |
| 70 | } |
| Eliza Margaretha | a2603fa | 2014-01-22 10:59:25 +0000 | [diff] [blame] | 71 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 72 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 73 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 74 | public boolean next () throws IOException { |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 75 | isStartEnumeration = false; |
| 76 | matchPayload.clear(); |
| 77 | return advance(); |
| 78 | } |
| Eliza Margaretha | 198e4ef | 2014-02-10 13:50:50 +0000 | [diff] [blame] | 79 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 80 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 81 | /** |
| 82 | * Advances the current span enumeration to the next span match. |
| 83 | * |
| 84 | * @return <code>true</code> if a span match is available, |
| 85 | * <code>false</code> otherwise. |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 86 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 87 | protected abstract boolean advance () throws IOException; |
| 88 | |
| Eliza Margaretha | 198e4ef | 2014-02-10 13:50:50 +0000 | [diff] [blame] | 89 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 90 | /** |
| 91 | * Returns the first span of the current match. |
| 92 | * |
| 93 | * @return the first span of the current match. |
| 94 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 95 | public CandidateSpan getMatchFirstSpan () { |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 96 | return matchFirstSpan; |
| 97 | } |
| Eliza Margaretha | a2603fa | 2014-01-22 10:59:25 +0000 | [diff] [blame] | 98 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 99 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 100 | /** |
| 101 | * Sets the first span of the current match. |
| 102 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 103 | * @param matchFirstSpan |
| 104 | * the first span of the current match. |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 105 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 106 | public void setMatchFirstSpan (CandidateSpan matchFirstSpan) { |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 107 | this.matchFirstSpan = matchFirstSpan; |
| 108 | } |
| Eliza Margaretha | 0192918 | 2014-02-19 11:48:59 +0000 | [diff] [blame] | 109 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 110 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 111 | /** |
| 112 | * Returns the second span of the current match. |
| 113 | * |
| 114 | * @return the second span of the current match. |
| 115 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 116 | public CandidateSpan getMatchSecondSpan () { |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 117 | return matchSecondSpan; |
| 118 | } |
| 119 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 120 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 121 | /** |
| 122 | * Sets the second span of the current match. |
| 123 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 124 | * @param matchSecondSpan |
| 125 | * the second span of the current match. |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 126 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 127 | public void setMatchSecondSpan (CandidateSpan matchSecondSpan) { |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 128 | this.matchSecondSpan = matchSecondSpan; |
| 129 | } |
| 130 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 131 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 132 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 133 | * Tells if the second span must occur together with the first |
| 134 | * span, or not. |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 135 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 136 | * @return <code>true</code> if the second span must <em>not</em> |
| 137 | * occur |
| 138 | * together with the first span, <code>false</code> |
| 139 | * otherwise. |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 140 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 141 | public boolean isExclusion () { |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 142 | return exclusion; |
| 143 | } |
| 144 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 145 | |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 146 | /** |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 147 | * Sets <code>true</code> if the second span must <em>not</em> |
| 148 | * occur |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 149 | * together with the first span, <code>false</code> otherwise. |
| 150 | * |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 151 | * @param exclusion |
| 152 | * a boolean with the value <code>true</code> if the |
| 153 | * second |
| 154 | * span must <em>not</em> occur together with the first |
| 155 | * span, |
| 156 | * <code>false</code> otherwise. |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 157 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 158 | public void setExclusion (boolean exclusion) { |
| Eliza Margaretha | c8d5920 | 2014-12-16 16:21:16 +0000 | [diff] [blame] | 159 | this.exclusion = exclusion; |
| 160 | } |
| Eliza Margaretha | 0192918 | 2014-02-19 11:48:59 +0000 | [diff] [blame] | 161 | |
| Eliza Margaretha | a2603fa | 2014-01-22 10:59:25 +0000 | [diff] [blame] | 162 | } |