| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.query.spans; |
| 2 | |
| 3 | import java.io.IOException; |
| Eliza Margaretha | 1c3bf27 | 2014-06-11 11:50:39 +0000 | [diff] [blame] | 4 | import java.util.ArrayList; |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 5 | import java.util.Collection; |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 6 | import java.util.Map; |
| 7 | |
| Akron | 700c1eb | 2015-09-25 16:57:30 +0200 | [diff] [blame] | 8 | import org.apache.lucene.index.LeafReaderContext; |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 9 | import org.apache.lucene.index.Term; |
| 10 | import org.apache.lucene.index.TermContext; |
| Eliza Margaretha | 05bff46 | 2015-02-18 18:18:26 +0000 | [diff] [blame] | 11 | import org.apache.lucene.search.spans.SpanQuery; |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 12 | import org.apache.lucene.search.spans.Spans; |
| 13 | import org.apache.lucene.util.Bits; |
| Eliza Margaretha | dc98dc1 | 2016-11-16 14:33:42 +0100 | [diff] [blame] | 14 | import org.slf4j.Logger; |
| 15 | import org.slf4j.LoggerFactory; |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 16 | |
| 17 | import de.ids_mannheim.korap.query.SimpleSpanQuery; |
| 18 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 19 | /** |
| 20 | * An abstract class for Span enumeration including span match |
| Eliza Margaretha | dc98dc1 | 2016-11-16 14:33:42 +0100 | [diff] [blame] | 21 | * properties and basic methods. |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 22 | * |
| 23 | * @author margaretha |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 24 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 25 | public abstract class SimpleSpans extends Spans { |
| Eliza Margaretha | dc98dc1 | 2016-11-16 14:33:42 +0100 | [diff] [blame] | 26 | protected final Logger log = LoggerFactory.getLogger(SimpleSpans.class); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 27 | private SimpleSpanQuery query; |
| 28 | protected boolean isStartEnumeration; |
| 29 | protected boolean collectPayloads; |
| 30 | |
| 31 | protected boolean hasMoreSpans; |
| Eliza Margaretha | dc98dc1 | 2016-11-16 14:33:42 +0100 | [diff] [blame] | 32 | // Enumeration of Spans |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 33 | protected Spans firstSpans, secondSpans; |
| 34 | |
| 35 | protected int matchDocNumber, matchStartPosition, matchEndPosition; |
| 36 | protected Collection<byte[]> matchPayload; |
| 37 | |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 38 | protected short spanId; |
| 39 | protected boolean hasSpanId = false; |
| 40 | |
| Akron | 4299355 | 2016-02-04 13:24:24 +0100 | [diff] [blame] | 41 | protected byte payloadTypeIdentifier; |
| 42 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 43 | |
| 44 | public SimpleSpans () { |
| 45 | collectPayloads = true; |
| 46 | matchDocNumber = -1; |
| 47 | matchStartPosition = -1; |
| 48 | matchEndPosition = -1; |
| 49 | matchPayload = new ArrayList<byte[]>(); |
| 50 | isStartEnumeration = true; |
| Eliza Margaretha | dc98dc1 | 2016-11-16 14:33:42 +0100 | [diff] [blame] | 51 | } |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 52 | |
| 53 | |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 54 | public SimpleSpans (SimpleSpanQuery simpleSpanQuery, |
| Akron | 700c1eb | 2015-09-25 16:57:30 +0200 | [diff] [blame] | 55 | LeafReaderContext context, Bits acceptDocs, |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 56 | Map<Term, TermContext> termContexts) |
| 57 | throws IOException { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 58 | this(); |
| 59 | query = simpleSpanQuery; |
| 60 | collectPayloads = query.isCollectPayloads(); |
| 61 | // Get the enumeration of the two spans to match |
| 62 | SpanQuery sq; |
| 63 | if ((sq = simpleSpanQuery.getFirstClause()) != null) |
| 64 | firstSpans = sq.getSpans(context, acceptDocs, termContexts); |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 65 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 66 | if ((sq = simpleSpanQuery.getSecondClause()) != null) |
| 67 | secondSpans = sq.getSpans(context, acceptDocs, termContexts); |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 68 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 69 | } |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 70 | |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 71 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 72 | /** |
| 73 | * If the current x and y are not in the same document, to skip |
| 74 | * the |
| 75 | * span with the smaller document number, to the same OR a greater |
| 76 | * document number than, the document number of the other span. Do |
| 77 | * this until the x and the y are in the same doc, OR until the |
| 78 | * last |
| 79 | * document. |
| 80 | * |
| 81 | * @return true iff such a document exists. |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 82 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 83 | protected boolean ensureSameDoc (Spans x, Spans y) throws IOException { |
| 84 | while (x.doc() != y.doc()) { |
| 85 | if (x.doc() < y.doc()) { |
| 86 | if (!x.skipTo(y.doc())) { |
| 87 | hasMoreSpans = false; |
| 88 | return false; |
| 89 | } |
| 90 | } |
| 91 | else { |
| 92 | if (!y.skipTo(x.doc())) { |
| 93 | hasMoreSpans = false; |
| 94 | return false; |
| 95 | } |
| 96 | } |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | |
| 102 | /** |
| 103 | * Find the same doc shared by element, firstspan and secondspan. |
| 104 | * |
| 105 | * @return true iff such a doc is found. |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 106 | */ |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 107 | protected boolean findSameDoc (Spans x, Spans y, Spans e) |
| 108 | throws IOException { |
| 109 | |
| 110 | while (hasMoreSpans) { |
| 111 | if (ensureSameDoc(x, y) && e.doc() == x.doc()) { |
| 112 | return true; |
| 113 | } |
| 114 | if (!ensureSameDoc(e, y)) { |
| 115 | return false; |
| 116 | }; |
| 117 | } |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | |
| 122 | @Override |
| 123 | public int doc () { |
| 124 | return matchDocNumber; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | @Override |
| 129 | public int start () { |
| 130 | return matchStartPosition; |
| 131 | } |
| 132 | |
| 133 | |
| Akron | 4299355 | 2016-02-04 13:24:24 +0100 | [diff] [blame] | 134 | @Override |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 135 | public int end () { |
| 136 | return matchEndPosition; |
| Akron | 4299355 | 2016-02-04 13:24:24 +0100 | [diff] [blame] | 137 | } |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 138 | |
| 139 | |
| 140 | @Override |
| 141 | public Collection<byte[]> getPayload () throws IOException { |
| 142 | return matchPayload; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | @Override |
| 147 | public boolean isPayloadAvailable () throws IOException { |
| 148 | return !matchPayload.isEmpty(); |
| 149 | } |
| 150 | |
| 151 | |
| 152 | @Override |
| 153 | public String toString () { |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 154 | return getClass().getName() + "(" + query.toString() + ")@" |
| 155 | + (isStartEnumeration ? "START" |
| 156 | : (hasMoreSpans ? (doc() + ":" + start() + "-" + end()) |
| 157 | : "END")); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| margaretha | 50c7633 | 2015-03-19 10:10:39 +0100 | [diff] [blame] | 160 | |
| 161 | |
| 162 | /** |
| 163 | * Returns the span id of the current span |
| 164 | * |
| 165 | * @return the span id of the current span |
| 166 | */ |
| 167 | public short getSpanId () { |
| 168 | return spanId; |
| 169 | } |
| 170 | |
| 171 | |
| 172 | /** |
| 173 | * Sets the span id of the current span |
| 174 | * |
| 175 | * @param spanId |
| 176 | * span id |
| 177 | */ |
| 178 | public void setSpanId (short spanId) { |
| 179 | this.spanId = spanId; |
| 180 | } |
| 181 | |
| margaretha | 69726b1 | 2015-12-10 12:03:19 +0100 | [diff] [blame] | 182 | |
| Akron | 4299355 | 2016-02-04 13:24:24 +0100 | [diff] [blame] | 183 | /** |
| 184 | * Gets the payload type identifier (PTI) of the current span |
| 185 | * |
| 186 | * @return a payload type identifier |
| 187 | */ |
| 188 | public byte getPayloadTypeIdentifier () { |
| 189 | return payloadTypeIdentifier; |
| 190 | } |
| 191 | |
| 192 | |
| 193 | /** |
| 194 | * Sets the payload type identifier (PTI) of the current span |
| 195 | * |
| 196 | * @param payloadTypeIdentifier |
| 197 | */ |
| 198 | public void setPayloadTypeIdentifier (byte payloadTypeIdentifier) { |
| 199 | this.payloadTypeIdentifier = payloadTypeIdentifier; |
| 200 | } |
| margaretha | 69726b1 | 2015-12-10 12:03:19 +0100 | [diff] [blame] | 201 | |
| Eliza Margaretha | ed3bb3b | 2014-01-14 10:53:56 +0000 | [diff] [blame] | 202 | } |