-
Notifications
You must be signed in to change notification settings - Fork 492
OPENNLP-1745: SentenceDetector - Add Junit test for useTokenEnd = false #792
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
983b183
e95168b
fc94e5f
038a6f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,12 +20,15 @@ | |
| import java.io.IOException; | ||
| import java.util.Locale; | ||
|
|
||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import opennlp.tools.dictionary.Dictionary; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertAll; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.fail; | ||
|
|
||
| /** | ||
| * Tests for the {@link SentenceDetectorME} class. | ||
| * <p> | ||
|
|
@@ -45,19 +48,25 @@ public class SentenceDetectorMEGermanTest extends AbstractSentenceDetectorTest { | |
|
|
||
| private static SentenceModel sentdetectModel; | ||
|
|
||
| @BeforeAll | ||
| public static void prepareResources() throws IOException { | ||
| Dictionary abbreviationDict = loadAbbDictionary(Locale.GERMAN); | ||
| SentenceDetectorFactory factory = new SentenceDetectorFactory( | ||
| "deu", true, abbreviationDict, EOS_CHARS); | ||
| sentdetectModel = train(factory, Locale.GERMAN); | ||
| Assertions.assertNotNull(sentdetectModel); | ||
| Assertions.assertEquals("deu", sentdetectModel.getLanguage()); | ||
| private void prepareResources(boolean useTokenEnd) { | ||
| try { | ||
| Dictionary abbreviationDict = loadAbbDictionary(Locale.GERMAN); | ||
|
||
| SentenceDetectorFactory factory = new SentenceDetectorFactory( | ||
| "deu", useTokenEnd, abbreviationDict, EOS_CHARS); | ||
|
|
||
| sentdetectModel = train(factory, Locale.GERMAN); | ||
| assertNotNull(sentdetectModel); | ||
| assertEquals("deu", sentdetectModel.getLanguage()); | ||
| } catch (IOException ex) { | ||
| fail("Couldn't train the SentenceModel using test data. Exception: " + ex.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| // Example taken from 'Sentences_DE.txt' | ||
| @Test | ||
| void testSentDetectWithInlineAbbreviationsEx1() { | ||
| prepareResources(true); | ||
|
|
||
| final String sent1 = "Ein Traum, zu dessen Bildung eine besonders starke Verdichtung beigetragen, " + | ||
| "wird für diese Untersuchung das günstigste Material sein."; | ||
| // Here we have two abbreviations "S. = Seite" and "ff. = folgende (Plural)" | ||
|
|
@@ -66,40 +75,63 @@ void testSentDetectWithInlineAbbreviationsEx1() { | |
| SentenceDetectorME sentDetect = new SentenceDetectorME(sentdetectModel); | ||
| String sampleSentences = sent1 + " " + sent2; | ||
| String[] sents = sentDetect.sentDetect(sampleSentences); | ||
| Assertions.assertEquals(2, sents.length); | ||
| Assertions.assertEquals(sent1, sents[0]); | ||
| Assertions.assertEquals(sent2, sents[1]); | ||
| assertEquals(2, sents.length); | ||
| assertEquals(sent1, sents[0]); | ||
| assertEquals(sent2, sents[1]); | ||
| double[] probs = sentDetect.getSentenceProbabilities(); | ||
| Assertions.assertEquals(2, probs.length); | ||
| assertEquals(2, probs.length); | ||
| } | ||
|
|
||
| // Reduced example taken from 'Sentences_DE.txt' | ||
| @Test | ||
| void testSentDetectWithInlineAbbreviationsEx2() { | ||
| prepareResources(true); | ||
|
|
||
| // Here we have three abbreviations: "S. = Seite", "vgl. = vergleiche", and "f. = folgende (Singular)" | ||
| final String sent1 = "Die farbige Tafel, die ich aufschlage, " + | ||
| "geht (vgl. die Analyse S. 185 f.) auf ein neues Thema ein."; | ||
|
|
||
| SentenceDetectorME sentDetect = new SentenceDetectorME(sentdetectModel); | ||
| String[] sents = sentDetect.sentDetect(sent1); | ||
| Assertions.assertEquals(1, sents.length); | ||
| Assertions.assertEquals(sent1, sents[0]); | ||
| assertEquals(1, sents.length); | ||
| assertEquals(sent1, sents[0]); | ||
| double[] probs = sentDetect.getSentenceProbabilities(); | ||
| Assertions.assertEquals(1, probs.length); | ||
| assertEquals(1, probs.length); | ||
| } | ||
|
|
||
| // Modified example deduced from 'Sentences_DE.txt' | ||
| @Test | ||
| void testSentDetectWithInlineAbbreviationsEx3() { | ||
| prepareResources(true); | ||
|
|
||
| // Here we have two abbreviations "z. B. = zum Beispiel" and "S. = Seite" | ||
| final String sent1 = "Die farbige Tafel, die ich aufschlage, " + | ||
| "geht (z. B. die Analyse S. 185) auf ein neues Thema ein."; | ||
|
|
||
| SentenceDetectorME sentDetect = new SentenceDetectorME(sentdetectModel); | ||
| String[] sents = sentDetect.sentDetect(sent1); | ||
| Assertions.assertEquals(1, sents.length); | ||
| Assertions.assertEquals(sent1, sents[0]); | ||
| assertEquals(1, sents.length); | ||
| assertEquals(sent1, sents[0]); | ||
| double[] probs = sentDetect.getSentenceProbabilities(); | ||
| assertEquals(1, probs.length); | ||
| } | ||
|
|
||
| @Test | ||
| void testSentDetectWithUseTokenEndFalse() { | ||
| prepareResources(false); | ||
|
|
||
| final String sent1 = "Träume sind eine Verbindung von Gedanken."; | ||
| final String sent2 = "Verschiedene Gedanken sind während der Traumformation aktiv."; | ||
|
|
||
| SentenceDetectorME sentDetect = new SentenceDetectorME(sentdetectModel); | ||
| //There is no blank space before start of the second sentence. | ||
| String[] sents = sentDetect.sentDetect(sent1 + sent2); | ||
| double[] probs = sentDetect.getSentenceProbabilities(); | ||
| Assertions.assertEquals(1, probs.length); | ||
| assertAll( | ||
|
||
| () -> assertEquals(2, sents.length), | ||
| () -> assertEquals(sent1, sents[0]), | ||
| () -> assertEquals(sent2, sents[1]), | ||
| () -> assertEquals(2, probs.length) | ||
| ); | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given
prepareResources(..)is not static any longer, this field should also avoid "static" and become an instance per Test case. Please consider removingstatichere.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks this is done.