MtasSpanFullyAlignedWithSpans.java.html 15.5 KB
<?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> &gt; <a href="index.source.html" class="el_package">mtas.search.spans</a> &gt; <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.spans.SpanCollector;
import mtas.search.spans.MtasSpanFullyAlignedWithQuery.MtasSpanFullyAlignedWithQuerySpans;
import mtas.search.spans.util.MtasSpans;

/**
 * The Class MtasSpanFullyAlignedWithSpans.
 */
public class MtasSpanFullyAlignedWithSpans extends MtasSpans {

  /** 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&lt;Integer&gt; previousSpans2EndPositions;

  /** The called next start position. */
  private boolean calledNextStartPosition;

  /** The no more positions. */
  private boolean noMorePositions;
  private boolean noMorePositionsSpan2;

  /** The doc id. */
  private int docId;

  /**
   * Instantiates a new mtas span fully aligned with spans.
   *
   * @param mtasSpanFullyAlignedWithQuery
   *          the mtas span fully aligned with query
   * @param spans1
   *          the spans 1
   * @param spans2
   *          the spans 2
   */
  public MtasSpanFullyAlignedWithSpans(
      MtasSpanFullyAlignedWithQuerySpans spans1,
      MtasSpanFullyAlignedWithQuerySpans spans2) {
<span class="fc" id="L55">    super();</span>
<span class="fc" id="L56">    docId = -1;</span>
<span class="fc" id="L57">    this.spans1 = spans1;</span>
<span class="fc" id="L58">    this.spans2 = spans2;</span>
<span class="fc" id="L59">    previousSpans2EndPositions = new HashSet&lt;&gt;();</span>
<span class="fc" id="L60">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#nextStartPosition()
   */
  @Override
  public int nextStartPosition() throws IOException {
    // no document
<span class="pc bpc" id="L70" title="2 of 4 branches missed.">    if (docId == -1 || docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L71">      throw new IOException(&quot;no document&quot;);</span>
      // finished
<span class="pc bpc" id="L73" title="1 of 2 branches missed.">    } else if (noMorePositions) {</span>
<span class="nc" id="L74">      return NO_MORE_POSITIONS;</span>
      // littleSpans already at start match, because of check for matching
      // document
<span class="fc bfc" id="L77" title="All 2 branches covered.">    } else if (!calledNextStartPosition) {</span>
<span class="fc" id="L78">      calledNextStartPosition = true;</span>
<span class="fc" id="L79">      return spans1.spans.startPosition();</span>
      // compute next match
    } else {
<span class="fc bfc" id="L82" title="All 2 branches covered.">      if (goToNextStartPosition()) {</span>
        // match found
<span class="fc" id="L84">        return spans1.spans.startPosition();</span>
      } else {
        // no more matches: document finished
<span class="fc" id="L87">        return NO_MORE_POSITIONS;</span>
      }
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#startPosition()
   */
  @Override
  public int startPosition() {
<span class="pc bpc" id="L99" title="1 of 2 branches missed.">    if(calledNextStartPosition) {</span>
<span class="pc bpc" id="L100" title="1 of 2 branches missed.">      if(noMorePositions) {</span>
<span class="nc" id="L101">        return NO_MORE_POSITIONS;</span>
      } else {
<span class="fc" id="L103">        return spans1.spans.startPosition();</span>
      }
    } else {
<span class="nc" id="L106">      return -1;</span>
    }
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#endPosition()
   */
  @Override
  public int endPosition() {
<span class="pc bpc" id="L117" title="1 of 2 branches missed.">    if(calledNextStartPosition) {</span>
<span class="pc bpc" id="L118" title="1 of 2 branches missed.">      if(noMorePositions) {</span>
<span class="nc" id="L119">        return NO_MORE_POSITIONS;</span>
      } else { 
<span class="fc" id="L121">        return spans1.spans.endPosition();</span>
      }
    } else {
<span class="nc" id="L124">      return -1;</span>
    }    
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#width()
   */
  @Override
  public int width() {
<span class="nc bnc" id="L135" title="All 2 branches missed.">    if(calledNextStartPosition) {</span>
<span class="nc bnc" id="L136" title="All 2 branches missed.">      if(noMorePositions) {</span>
<span class="nc" id="L137">        return 0;</span>
      } else {
<span class="nc" id="L139">        return spans1.spans.endPosition() - spans1.spans.startPosition();</span>
      }
    } else {
<span class="nc" id="L142">      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="L155">    spans1.spans.collect(collector);</span>
<span class="nc" id="L156">    spans2.spans.collect(collector);</span>
<span class="nc" id="L157">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.spans.Spans#positionsCost()
   */
  @Override
  public float positionsCost() {
<span class="nc" id="L166">    return 0;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#docID()
   */
  @Override
  public int docID() {
<span class="fc" id="L176">    return docId;</span>
  }

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#nextDoc()
   */
  @Override
  public int nextDoc() throws IOException {
<span class="fc" id="L186">    reset();</span>
<span class="pc bpc" id="L187" title="1 of 2 branches missed.">    while (!goToNextDoc())</span>
<span class="nc" id="L188">      ;</span>
<span class="fc" id="L189">    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="L199">    reset();</span>
<span class="nc bnc" id="L200" title="All 2 branches missed.">    if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L201">      return docId;</span>
<span class="nc bnc" id="L202" title="All 2 branches missed.">    } else if (target &lt; docId) {</span>
      // should not happen
<span class="nc" id="L204">      docId = NO_MORE_DOCS;</span>
<span class="nc" id="L205">      return docId;</span>
    } else {
      // advance 1
<span class="nc" id="L208">      int spans1DocId = spans1.spans.docID();</span>
<span class="nc" id="L209">      int newTarget = target;</span>
<span class="nc bnc" id="L210" title="All 2 branches missed.">      if (spans1DocId &lt; newTarget) {</span>
<span class="nc" id="L211">        spans1DocId = spans1.spans.advance(target);</span>
<span class="nc bnc" id="L212" title="All 2 branches missed.">        if (spans1DocId == NO_MORE_DOCS) {</span>
<span class="nc" id="L213">          docId = NO_MORE_DOCS;</span>
<span class="nc" id="L214">          return docId;</span>
        }
<span class="nc" id="L216">        newTarget = Math.max(newTarget, spans1DocId);</span>
      }
<span class="nc" id="L218">      int spans2DocId = spans2.spans.docID();</span>
      // advance 2
<span class="nc bnc" id="L220" title="All 2 branches missed.">      if (spans2DocId &lt; newTarget) {</span>
<span class="nc" id="L221">        spans2DocId = spans2.spans.advance(newTarget);</span>
<span class="nc bnc" id="L222" title="All 2 branches missed.">        if (spans2DocId == NO_MORE_DOCS) {</span>
<span class="nc" id="L223">          docId = NO_MORE_DOCS;</span>
<span class="nc" id="L224">          return docId;</span>
        }
      }
      // check equal docId, otherwise next
<span class="nc bnc" id="L228" title="All 2 branches missed.">      if (spans1DocId == spans2DocId) {</span>
<span class="nc" id="L229">        docId = spans1DocId;</span>
        // check match
<span class="nc bnc" id="L231" title="All 2 branches missed.">        if (goToNextStartPosition()) {</span>
<span class="nc" id="L232">          return docId;</span>
        } else {
<span class="nc" id="L234">          return nextDoc();</span>
        }
      } else {
<span class="nc" id="L237">        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="L250" title="1 of 2 branches missed.">    if (docId == NO_MORE_DOCS) {</span>
<span class="nc" id="L251">      return true;</span>
    } else {
<span class="fc" id="L253">      int spans1DocId = spans1.spans.nextDoc();</span>
<span class="fc" id="L254">      int spans2DocId = spans2.spans.docID();</span>
<span class="fc" id="L255">      docId = Math.max(spans1DocId, spans2DocId);</span>
<span class="fc bfc" id="L256" title="All 4 branches covered.">      while (spans1DocId != spans2DocId &amp;&amp; docId != NO_MORE_DOCS) {</span>
<span class="pc bpc" id="L257" title="1 of 2 branches missed.">        if (spans1DocId &lt; spans2DocId) {</span>
<span class="nc" id="L258">          spans1DocId = spans1.spans.advance(spans2DocId);</span>
<span class="nc" id="L259">          docId = spans1DocId;</span>
        } else {
<span class="fc" id="L261">          spans2DocId = spans2.spans.advance(spans1DocId);</span>
<span class="fc" id="L262">          docId = spans2DocId;</span>
        }
      }
<span class="pc bpc" id="L265" title="1 of 4 branches missed.">      if (docId != NO_MORE_DOCS &amp;&amp; !goToNextStartPosition()) {</span>
<span class="nc" id="L266">        reset();</span>
<span class="nc" id="L267">        return false;</span>
      }
<span class="fc" id="L269">      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="L286">    while ((nextSpans1StartPosition = spans1.spans</span>
<span class="fc bfc" id="L287" title="All 2 branches covered.">        .nextStartPosition()) != NO_MORE_POSITIONS) {</span>
<span class="fc" id="L288">      nextSpans1EndPosition = spans1.spans.endPosition();</span>
<span class="fc bfc" id="L289" title="All 4 branches covered.">      if (noMorePositionsSpan2</span>
          &amp;&amp; nextSpans1StartPosition &gt; lastSpans2StartPosition) {
<span class="fc" id="L291">        noMorePositions = true;</span>
<span class="fc" id="L292">        return false;</span>
        // check if start/en span1 matches start/end span2 from last or previous
<span class="fc bfc" id="L294" title="All 6 branches covered.">      } else if ((nextSpans1StartPosition == lastSpans2StartPosition</span>
          &amp;&amp; nextSpans1EndPosition == lastSpans2EndPosition)
          || (nextSpans1StartPosition == previousSpans2StartPosition
<span class="fc bfc" id="L297" title="All 2 branches covered.">              &amp;&amp; previousSpans2EndPositions.contains(nextSpans1EndPosition))) {</span>
<span class="fc" id="L298">        return true;</span>
      } else {
        // try to find matching span2
<span class="fc bfc" id="L301" title="All 4 branches covered.">        while (!noMorePositionsSpan2</span>
            &amp;&amp; nextSpans1StartPosition &gt;= lastSpans2StartPosition) {
          // get new span2
<span class="fc" id="L304">          nextSpans2StartPosition = spans2.spans.nextStartPosition();</span>
          // check for finished span2
<span class="fc bfc" id="L306" title="All 2 branches covered.">          if (nextSpans2StartPosition == NO_MORE_POSITIONS) {</span>
<span class="fc" id="L307">            noMorePositionsSpan2 = true;</span>
          } else {
            // get end for new span2
<span class="fc" id="L310">            nextSpans2EndPosition = spans2.spans.endPosition();</span>
            // check for registering last span2 as previous
<span class="fc bfc" id="L312" title="All 2 branches covered.">            if (nextSpans1StartPosition &lt;= lastSpans2StartPosition) {</span>
<span class="pc bpc" id="L313" title="1 of 2 branches missed.">              if (previousSpans2StartPosition != lastSpans2StartPosition) {</span>
<span class="fc" id="L314">                previousSpans2StartPosition = lastSpans2StartPosition;</span>
<span class="fc" id="L315">                previousSpans2EndPositions.clear();</span>
              }
<span class="fc" id="L317">              previousSpans2EndPositions.add(lastSpans2EndPosition);</span>
            }
            // register span2 as last
<span class="fc" id="L320">            lastSpans2StartPosition = nextSpans2StartPosition;</span>
<span class="fc" id="L321">            lastSpans2EndPosition = nextSpans2EndPosition;</span>
            // check for match
<span class="fc bfc" id="L323" title="All 4 branches covered.">            if (nextSpans1StartPosition == nextSpans2StartPosition</span>
                &amp;&amp; nextSpans1EndPosition == nextSpans2EndPosition) {
<span class="fc" id="L325">              return true;</span>
            }
          }
        }

      }
    }
<span class="fc" id="L332">    noMorePositions = true;</span>
<span class="fc" id="L333">    return false;</span>
  }

  /**
   * Reset.
   */
  private void reset() {
<span class="fc" id="L340">    calledNextStartPosition = false;</span>
<span class="fc" id="L341">    noMorePositions = false;</span>
<span class="fc" id="L342">    noMorePositionsSpan2 = false;</span>
<span class="fc" id="L343">    lastSpans2StartPosition = -1;</span>
<span class="fc" id="L344">    lastSpans2EndPosition = -1;</span>
<span class="fc" id="L345">    previousSpans2StartPosition = -1;</span>
<span class="fc" id="L346">    previousSpans2EndPositions.clear();</span>
<span class="fc" id="L347">  }</span>

  /*
   * (non-Javadoc)
   * 
   * @see org.apache.lucene.search.DocIdSetIterator#cost()
   */
  @Override
  public long cost() {
<span class="nc" id="L356">    return 0;</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>