diff --git a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/sentdetect/SentenceDetectorME.java b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/sentdetect/SentenceDetectorME.java index ddcc3388c..9b113e120 100644 --- a/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/sentdetect/SentenceDetectorME.java +++ b/opennlp-core/opennlp-runtime/src/main/java/opennlp/tools/sentdetect/SentenceDetectorME.java @@ -345,7 +345,8 @@ protected boolean isAcceptableBreak(CharSequence s, int fromIndex, int candidate 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-core/opennlp-runtime/src/test/java/opennlp/tools/sentdetect/SentenceDetectorMEGermanTest.java b/opennlp-core/opennlp-runtime/src/test/java/opennlp/tools/sentdetect/SentenceDetectorMEGermanTest.java index d95a1eeca..5417fe170 100644 --- a/opennlp-core/opennlp-runtime/src/test/java/opennlp/tools/sentdetect/SentenceDetectorMEGermanTest.java +++ b/opennlp-core/opennlp-runtime/src/test/java/opennlp/tools/sentdetect/SentenceDetectorMEGermanTest.java @@ -151,6 +151,24 @@ void testSentDetectWithUseTokenEndFalse() { () -> assertEquals(2, probs.length)); } + /* + * A reproducer and test for OPENNLP-1781. + */ + @Test + void testSentDetectWithAbbreviationsAtSentenceStart() { + prepareResources(true); + + final String sent1 = "S. Träume sind eine Verbindung von Gedanken."; + + final String[] sents = sentenceDetector.sentDetect(sent1); + final 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, @@ -163,6 +181,7 @@ void testSentDetectWithUseTokenEndFalse() { "Der Auto stand schief. Wer hat es dort geparkt?", "Es lag am DBMS. Die Performance muss verbessert werden.", "Siehe Buch S. 17f. Dort ist es zu finden.", + "S. Buch S. 17f. Dort ist es zu finden.", // OPENNLP-1781 "Sie trank einen Mocca. Er schmeckte ihr!", "Der Anker hängt zu Beginn des Bugs. Es ist vertaut.", "Das Verfahren testet auf HIV. Es ist präzise."