This is an automated email from the ASF dual-hosted git repository.

rzo1 pushed a commit to branch 
OPENNLP-1781-SentenceDetectorME-throws-StringIndexOutOfBoundsException-when-sentence-starts-with-an-abbreviation
in repository https://gitbox.apache.org/repos/asf/opennlp.git

commit c0d0c15104fbc12fb69973f57a6ebd7e36e7db16
Author: Richard Zowalla <[email protected]>
AuthorDate: Tue Oct 14 11:19:40 2025 +0200

    OPENNLP-1781 - SentenceDetectorME throws StringIndexOutOfBoundsException 
when sentence starts with an abbreviation
---
 .../opennlp/tools/sentdetect/SentenceDetectorME.java  |  3 ++-
 .../sentdetect/SentenceDetectorMEGermanTest.java      | 19 +++++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git 
a/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorME.java 
b/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorME.java
index 26146c67..f64768df 100644
--- 
a/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorME.java
+++ 
b/opennlp-tools/src/main/java/opennlp/tools/sentdetect/SentenceDetectorME.java
@@ -345,7 +345,8 @@ public class SentenceDetectorME implements 
SentenceDetector, Probabilistic {
       if (tokenPosition == -1) {
         continue; // skip fast
       }
-      final char prevChar = s.charAt(tokenPosition - 1);
+
+      final char prevChar = s.charAt(tokenPosition == 0 ? tokenPosition : 
tokenPosition - 1);
       int tokenLength = token.length();
       if (tokenPosition + tokenLength < candidateIndex || tokenPosition > 
candidateIndex ||
         /*
diff --git 
a/opennlp-tools/src/test/java/opennlp/tools/sentdetect/SentenceDetectorMEGermanTest.java
 
b/opennlp-tools/src/test/java/opennlp/tools/sentdetect/SentenceDetectorMEGermanTest.java
index b34285dc..3bdfc3e3 100644
--- 
a/opennlp-tools/src/test/java/opennlp/tools/sentdetect/SentenceDetectorMEGermanTest.java
+++ 
b/opennlp-tools/src/test/java/opennlp/tools/sentdetect/SentenceDetectorMEGermanTest.java
@@ -151,6 +151,25 @@ public class SentenceDetectorMEGermanTest extends 
AbstractSentenceDetectorTest {
             () -> assertEquals(2, probs.length));
   }
 
+  /*
+    * A reproducer and test for OPENNLP-1767.
+   */
+  @Test
+  void testSentDetectWithAbbreviationsAtSentenceStart() {
+    prepareResources(true);
+
+    final String sent1 = "S. Träume sind eine Verbindung von Gedanken.";
+
+    //There is no blank space before start of the second sentence.
+    String[] sents = sentenceDetector.sentDetect(sent1);
+    double[] probs = sentenceDetector.probs();
+
+    assertAll(
+        () -> assertEquals(1, sents.length),
+        () -> assertEquals(sent1, sents[0]),
+        () -> assertEquals(1, probs.length));
+  }
+
   /*
    * A reproducer and test for OPENNLP-1767.
    * It checks that sentence detection with common abbreviations works 
correctly,

Reply via email to