MtasSpanFullyAlignedWithSpans.java.html
16.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" lang="en"><head><meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/><link rel="stylesheet" href="../jacoco-resources/report.css" type="text/css"/><link rel="shortcut icon" href="../jacoco-resources/report.gif" type="image/gif"/><title>MtasSpanFullyAlignedWithSpans.java</title><link rel="stylesheet" href="../jacoco-resources/prettify.css" type="text/css"/><script type="text/javascript" src="../jacoco-resources/prettify.js"></script></head><body onload="window['PR_TAB_WIDTH']=4;prettyPrint()"><div class="breadcrumb" id="breadcrumb"><span class="info"><a href="../jacoco-sessions.html" class="el_session">Sessions</a></span><a href="../index.html" class="el_report">MTAS</a> > <a href="index.source.html" class="el_package">mtas.search.spans</a> > <span class="el_source">MtasSpanFullyAlignedWithSpans.java</span></div><h1>MtasSpanFullyAlignedWithSpans.java</h1><pre class="source lang-java linenums">package mtas.search.spans;
import java.io.IOException;
import java.util.HashSet;
import org.apache.lucene.search.TwoPhaseIterator;
import org.apache.lucene.search.spans.SpanCollector;
import mtas.search.spans.MtasSpanFullyAlignedWithQuery.MtasSpanFullyAlignedWithQuerySpans;
import mtas.search.spans.util.MtasSpans;
/**
* The Class MtasSpanFullyAlignedWithSpans.
*/
public class MtasSpanFullyAlignedWithSpans extends MtasSpans {
/** The query. */
private MtasSpanFullyAlignedWithQuery query;
/** The spans 1. */
private MtasSpanFullyAlignedWithQuerySpans spans1;
/** The spans 2. */
private MtasSpanFullyAlignedWithQuerySpans spans2;
/** The last spans 2 start position. */
private int lastSpans2StartPosition;
/** The last spans 2 end position. */
private int lastSpans2EndPosition;
/** The previous spans 2 start position. */
private int previousSpans2StartPosition;
/** The previous spans 2 end positions. */
private HashSet<Integer> previousSpans2EndPositions;
/** The called next start position. */
private boolean calledNextStartPosition;
/** The no more positions. */
private boolean noMorePositions;
/** The no more positions span 2. */
private boolean noMorePositionsSpan2;
/** The doc id. */
private int docId;
/**
* Instantiates a new mtas span fully aligned with spans.
*
* @param query the query
* @param spans1 the spans 1
* @param spans2 the spans 2
*/
public MtasSpanFullyAlignedWithSpans(MtasSpanFullyAlignedWithQuery query,
MtasSpanFullyAlignedWithQuerySpans spans1,
MtasSpanFullyAlignedWithQuerySpans spans2) {
<span class="fc" id="L59"> super();</span>
<span class="fc" id="L60"> docId = -1;</span>
<span class="fc" id="L61"> this.query = query;</span>
<span class="fc" id="L62"> this.spans1 = spans1;</span>
<span class="fc" id="L63"> this.spans2 = spans2;</span>
<span class="fc" id="L64"> previousSpans2EndPositions = new HashSet<>();</span>
<span class="fc" id="L65"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.Spans#nextStartPosition()
*/
@Override
public int nextStartPosition() throws IOException {
// no document
<span class="pc bpc" id="L75" title="2 of 4 branches missed."> if (docId == -1 || docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L76"> throw new IOException("no document");</span>
// finished
<span class="pc bpc" id="L78" title="1 of 2 branches missed."> } else if (noMorePositions) {</span>
<span class="nc" id="L79"> return NO_MORE_POSITIONS;</span>
// littleSpans already at start match, because of check for matching
// document
<span class="fc bfc" id="L82" title="All 2 branches covered."> } else if (!calledNextStartPosition) {</span>
<span class="fc" id="L83"> calledNextStartPosition = true;</span>
<span class="fc" id="L84"> return spans1.spans.startPosition();</span>
// compute next match
} else {
<span class="fc bfc" id="L87" title="All 2 branches covered."> if (goToNextStartPosition()) {</span>
// match found
<span class="fc" id="L89"> return spans1.spans.startPosition();</span>
} else {
// no more matches: document finished
<span class="fc" id="L92"> return NO_MORE_POSITIONS;</span>
}
}
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.Spans#startPosition()
*/
@Override
public int startPosition() {
<span class="pc bpc" id="L104" title="1 of 2 branches missed."> if (calledNextStartPosition) {</span>
<span class="pc bpc" id="L105" title="1 of 2 branches missed."> if (noMorePositions) {</span>
<span class="nc" id="L106"> return NO_MORE_POSITIONS;</span>
} else {
<span class="fc" id="L108"> return spans1.spans.startPosition();</span>
}
} else {
<span class="nc" id="L111"> return -1;</span>
}
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.Spans#endPosition()
*/
@Override
public int endPosition() {
<span class="pc bpc" id="L122" title="1 of 2 branches missed."> if (calledNextStartPosition) {</span>
<span class="pc bpc" id="L123" title="1 of 2 branches missed."> if (noMorePositions) {</span>
<span class="nc" id="L124"> return NO_MORE_POSITIONS;</span>
} else {
<span class="fc" id="L126"> return spans1.spans.endPosition();</span>
}
} else {
<span class="nc" id="L129"> return -1;</span>
}
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.Spans#width()
*/
@Override
public int width() {
<span class="nc bnc" id="L140" title="All 2 branches missed."> if (calledNextStartPosition) {</span>
<span class="nc bnc" id="L141" title="All 2 branches missed."> if (noMorePositions) {</span>
<span class="nc" id="L142"> return 0;</span>
} else {
<span class="nc" id="L144"> return spans1.spans.endPosition() - spans1.spans.startPosition();</span>
}
} else {
<span class="nc" id="L147"> return 0;</span>
}
}
/*
* (non-Javadoc)
*
* @see
* org.apache.lucene.search.spans.Spans#collect(org.apache.lucene.search.spans
* .SpanCollector)
*/
@Override
public void collect(SpanCollector collector) throws IOException {
<span class="nc" id="L160"> spans1.spans.collect(collector);</span>
<span class="nc" id="L161"> spans2.spans.collect(collector);</span>
<span class="nc" id="L162"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.spans.Spans#positionsCost()
*/
@Override
public float positionsCost() {
<span class="nc" id="L171"> return 0;</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.DocIdSetIterator#docID()
*/
@Override
public int docID() {
<span class="fc" id="L181"> return docId;</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.DocIdSetIterator#nextDoc()
*/
@Override
public int nextDoc() throws IOException {
<span class="fc" id="L191"> reset();</span>
<span class="pc bpc" id="L192" title="1 of 2 branches missed."> while (!goToNextDoc())</span>
<span class="nc" id="L193"> ;</span>
<span class="fc" id="L194"> return docId;</span>
}
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.DocIdSetIterator#advance(int)
*/
@Override
public int advance(int target) throws IOException {
<span class="nc" id="L204"> reset();</span>
<span class="nc bnc" id="L205" title="All 2 branches missed."> if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L206"> return docId;</span>
<span class="nc bnc" id="L207" title="All 2 branches missed."> } else if (target < docId) {</span>
// should not happen
<span class="nc" id="L209"> docId = NO_MORE_DOCS;</span>
<span class="nc" id="L210"> return docId;</span>
} else {
// advance 1
<span class="nc" id="L213"> int spans1DocId = spans1.spans.docID();</span>
<span class="nc" id="L214"> int newTarget = target;</span>
<span class="nc bnc" id="L215" title="All 2 branches missed."> if (spans1DocId < newTarget) {</span>
<span class="nc" id="L216"> spans1DocId = spans1.spans.advance(target);</span>
<span class="nc bnc" id="L217" title="All 2 branches missed."> if (spans1DocId == NO_MORE_DOCS) {</span>
<span class="nc" id="L218"> docId = NO_MORE_DOCS;</span>
<span class="nc" id="L219"> return docId;</span>
}
<span class="nc" id="L221"> newTarget = Math.max(newTarget, spans1DocId);</span>
}
<span class="nc" id="L223"> int spans2DocId = spans2.spans.docID();</span>
// advance 2
<span class="nc bnc" id="L225" title="All 2 branches missed."> if (spans2DocId < newTarget) {</span>
<span class="nc" id="L226"> spans2DocId = spans2.spans.advance(newTarget);</span>
<span class="nc bnc" id="L227" title="All 2 branches missed."> if (spans2DocId == NO_MORE_DOCS) {</span>
<span class="nc" id="L228"> docId = NO_MORE_DOCS;</span>
<span class="nc" id="L229"> return docId;</span>
}
}
// check equal docId, otherwise next
<span class="nc bnc" id="L233" title="All 2 branches missed."> if (spans1DocId == spans2DocId) {</span>
<span class="nc" id="L234"> docId = spans1DocId;</span>
// check match
<span class="nc bnc" id="L236" title="All 2 branches missed."> if (goToNextStartPosition()) {</span>
<span class="nc" id="L237"> return docId;</span>
} else {
<span class="nc" id="L239"> return nextDoc();</span>
}
} else {
<span class="nc" id="L242"> return nextDoc();</span>
}
}
}
/**
* Go to next doc.
*
* @return true, if successful
* @throws IOException Signals that an I/O exception has occurred.
*/
private boolean goToNextDoc() throws IOException {
<span class="pc bpc" id="L254" title="1 of 2 branches missed."> if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L255"> return true;</span>
} else {
<span class="fc" id="L257"> int spans1DocId = spans1.spans.nextDoc();</span>
<span class="fc" id="L258"> int spans2DocId = spans2.spans.docID();</span>
<span class="fc" id="L259"> docId = Math.max(spans1DocId, spans2DocId);</span>
<span class="fc bfc" id="L260" title="All 4 branches covered."> while (spans1DocId != spans2DocId && docId != NO_MORE_DOCS) {</span>
<span class="pc bpc" id="L261" title="1 of 2 branches missed."> if (spans1DocId < spans2DocId) {</span>
<span class="nc" id="L262"> spans1DocId = spans1.spans.advance(spans2DocId);</span>
<span class="nc" id="L263"> docId = spans1DocId;</span>
} else {
<span class="fc" id="L265"> spans2DocId = spans2.spans.advance(spans1DocId);</span>
<span class="fc" id="L266"> docId = spans2DocId;</span>
}
}
<span class="pc bpc" id="L269" title="1 of 4 branches missed."> if (docId != NO_MORE_DOCS && !goToNextStartPosition()) {</span>
<span class="nc" id="L270"> reset();</span>
<span class="nc" id="L271"> return false;</span>
}
<span class="fc" id="L273"> return true;</span>
}
}
/**
* Go to next start position.
*
* @return true, if successful
* @throws IOException Signals that an I/O exception has occurred.
*/
private boolean goToNextStartPosition() throws IOException {
int nextSpans1StartPosition;
int nextSpans1EndPosition;
int nextSpans2StartPosition;
int nextSpans2EndPosition;
// loop over span1
<span class="fc" id="L289"> while ((nextSpans1StartPosition = spans1.spans</span>
<span class="fc bfc" id="L290" title="All 2 branches covered."> .nextStartPosition()) != NO_MORE_POSITIONS) {</span>
<span class="fc" id="L291"> nextSpans1EndPosition = spans1.spans.endPosition();</span>
<span class="fc bfc" id="L292" title="All 4 branches covered."> if (noMorePositionsSpan2</span>
&& nextSpans1StartPosition > lastSpans2StartPosition) {
<span class="fc" id="L294"> noMorePositions = true;</span>
<span class="fc" id="L295"> return false;</span>
// check if start/en span1 matches start/end span2 from last or previous
<span class="fc bfc" id="L297" title="All 6 branches covered."> } else if ((nextSpans1StartPosition == lastSpans2StartPosition</span>
&& nextSpans1EndPosition == lastSpans2EndPosition)
|| (nextSpans1StartPosition == previousSpans2StartPosition
<span class="fc bfc" id="L300" title="All 2 branches covered."> && previousSpans2EndPositions.contains(nextSpans1EndPosition))) {</span>
<span class="fc" id="L301"> return true;</span>
} else {
// try to find matching span2
<span class="fc bfc" id="L304" title="All 4 branches covered."> while (!noMorePositionsSpan2</span>
&& nextSpans1StartPosition >= lastSpans2StartPosition) {
// get new span2
<span class="fc" id="L307"> nextSpans2StartPosition = spans2.spans.nextStartPosition();</span>
// check for finished span2
<span class="fc bfc" id="L309" title="All 2 branches covered."> if (nextSpans2StartPosition == NO_MORE_POSITIONS) {</span>
<span class="fc" id="L310"> noMorePositionsSpan2 = true;</span>
} else {
// get end for new span2
<span class="fc" id="L313"> nextSpans2EndPosition = spans2.spans.endPosition();</span>
// check for registering last span2 as previous
<span class="fc bfc" id="L315" title="All 2 branches covered."> if (nextSpans1StartPosition <= lastSpans2StartPosition) {</span>
<span class="pc bpc" id="L316" title="1 of 2 branches missed."> if (previousSpans2StartPosition != lastSpans2StartPosition) {</span>
<span class="fc" id="L317"> previousSpans2StartPosition = lastSpans2StartPosition;</span>
<span class="fc" id="L318"> previousSpans2EndPositions.clear();</span>
}
<span class="fc" id="L320"> previousSpans2EndPositions.add(lastSpans2EndPosition);</span>
}
// register span2 as last
<span class="fc" id="L323"> lastSpans2StartPosition = nextSpans2StartPosition;</span>
<span class="fc" id="L324"> lastSpans2EndPosition = nextSpans2EndPosition;</span>
// check for match
<span class="fc bfc" id="L326" title="All 4 branches covered."> if (nextSpans1StartPosition == nextSpans2StartPosition</span>
&& nextSpans1EndPosition == nextSpans2EndPosition) {
<span class="fc" id="L328"> return true;</span>
}
}
}
}
}
<span class="fc" id="L335"> noMorePositions = true;</span>
<span class="fc" id="L336"> return false;</span>
}
/**
* Reset.
*/
private void reset() {
<span class="fc" id="L343"> calledNextStartPosition = false;</span>
<span class="fc" id="L344"> noMorePositions = false;</span>
<span class="fc" id="L345"> noMorePositionsSpan2 = false;</span>
<span class="fc" id="L346"> lastSpans2StartPosition = -1;</span>
<span class="fc" id="L347"> lastSpans2EndPosition = -1;</span>
<span class="fc" id="L348"> previousSpans2StartPosition = -1;</span>
<span class="fc" id="L349"> previousSpans2EndPositions.clear();</span>
<span class="fc" id="L350"> }</span>
/*
* (non-Javadoc)
*
* @see org.apache.lucene.search.DocIdSetIterator#cost()
*/
@Override
public long cost() {
<span class="nc" id="L359"> return 0;</span>
}
/* (non-Javadoc)
* @see mtas.search.spans.util.MtasSpans#asTwoPhaseIterator()
*/
@Override
public TwoPhaseIterator asTwoPhaseIterator() {
<span class="nc bnc" id="L367" title="All 6 branches missed."> if (spans1 == null || spans2 == null || !query.twoPhaseIteratorAllowed()) {</span>
<span class="nc" id="L368"> return null;</span>
} else {
// TODO
<span class="nc" id="L371"> return null;</span>
}
}
}
</pre><div class="footer"><span class="right">Created with <a href="http://www.jacoco.org/jacoco">JaCoCo</a> 0.7.9.201702052155</span></div></body></html>