Skip to content

Apache Lucene open-source search software

Adjust middle of sentence calculation and add test verifying correct behavior (#13170)

Tim Grein44fa35b65ff4
diff --git a/lucene/analysis/opennlp/src/java/org/apache/lucene/analysis/opennlp/OpenNLPSentenceBreakIterator.java b/lucene/analysis/opennlp/src/java/org/apache/lucene/analysis/opennlp/OpenNLPSentenceBreakIterator.java
index e3c2fb78320..d81b06fd90d 100644
--- a/lucene/analysis/opennlp/src/java/org/apache/lucene/analysis/opennlp/OpenNLPSentenceBreakIterator.java
+++ b/lucene/analysis/opennlp/src/java/org/apache/lucene/analysis/opennlp/OpenNLPSentenceBreakIterator.java
@@ -148,7 +148,7 @@ public final class OpenNLPSentenceBreakIterator extends BreakIterator {
currentSentence = 0;
return DONE;
} else {
- currentSentence = sentenceStarts.length / 2; // start search from the middle
+ currentSentence = (sentenceStarts.length - 1) / 2; // start search from the middle
moveToSentenceAt(pos, 0, sentenceStarts.length - 1);
if (0 == currentSentence) {
text.setIndex(text.getBeginIndex());
diff --git a/lucene/analysis/opennlp/src/test/org/apache/lucene/analysis/opennlp/TestOpenNLPSentenceBreakIterator.java b/lucene/analysis/opennlp/src/test/org/apache/lucene/analysis/opennlp/TestOpenNLPSentenceBreakIterator.java
index acd13da0037..465d067070e 100644
--- a/lucene/analysis/opennlp/src/test/org/apache/lucene/analysis/opennlp/TestOpenNLPSentenceBreakIterator.java
+++ b/lucene/analysis/opennlp/src/test/org/apache/lucene/analysis/opennlp/TestOpenNLPSentenceBreakIterator.java
@@ -203,6 +203,17 @@ public class TestOpenNLPSentenceBreakIterator extends LuceneTestCase {
test0Sentences(bi);
}
+ public void testPrecedingWithTwoSentences() throws IOException {
+ NLPSentenceDetectorOp sentenceDetectorOp =
+ OpenNLPOpsFactory.getSentenceDetector(sentenceModelFile);
+ BreakIterator bi = new OpenNLPSentenceBreakIterator(sentenceDetectorOp);
+ bi.setText("This is sentence one. This is sentence two.");
+
+ // set pos to somewhere in the second sentence
+ int precedingSentence = bi.preceding(25);
+ assertEquals(0, precedingSentence);
+ }
+
private void test0Sentences(BreakIterator bi) {
assertEquals(0, bi.current());
assertEquals(0, bi.first());