Skip to content

Commit 1028ad7

Browse files
jboadasmaibin
authored andcommitted
OpenNLP (eugenp#2058)
* Introduction to OpenNLP * Introduction to OpenNLP * OpenNLP fix code
1 parent b6d47b0 commit 1028ad7

1 file changed

Lines changed: 26 additions & 4 deletions

File tree

‎libraries/src/main/java/com/baeldung/opennlp/OpenNLP.java‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
public class OpenNLP {
3737

3838
private final static Logger LOGGER = Logger.getLogger(OpenNLP.class.getName());
39-
private final static String text = "To get to the south: Go to the store. Buy a compass. Use the compass. Then walk to the south.";
39+
private final static String text = buildString();
4040
private final static String sentence[] = new String[] { "James", "Jordan", "live", "in", "Oklahoma", "city", "." };
4141

4242
private DoccatModel docCatModel;
@@ -45,6 +45,16 @@ public static void main(String[] args) {
4545
new OpenNLP();
4646
}
4747

48+
public static String buildString(){
49+
StringBuilder sb = new StringBuilder();
50+
sb.append("To get to the south:");
51+
sb.append(" Go to the store.");
52+
sb.append(" Buy a compass.");
53+
sb.append(" Use the compass.");
54+
sb.append(" Then walk to the south.");
55+
return sb.toString();
56+
}
57+
4858
public OpenNLP() {
4959
try {
5060
sentenceDetector();
@@ -68,7 +78,9 @@ public void sentenceDetector() throws InvalidFormatException, IOException {
6878
SentenceModel model = new SentenceModel(is);
6979
SentenceDetectorME sdetector = new SentenceDetectorME(model);
7080
String sentences[] = sdetector.sentDetect(text);
71-
Arrays.stream(sentences).forEach(LOGGER::info);
81+
for (String sentence : sentences) {
82+
LOGGER.info(sentence);
83+
}
7284
is.close();
7385
}
7486

@@ -77,7 +89,9 @@ public void tokenizer() throws InvalidFormatException, IOException {
7789
TokenizerModel model = new TokenizerModel(is);
7890
Tokenizer tokenizer = new TokenizerME(model);
7991
String tokens[] = tokenizer.tokenize(text);
80-
Arrays.stream(tokens).forEach(LOGGER::info);
92+
for (String token : tokens) {
93+
LOGGER.info(token);
94+
}
8195
is.close();
8296
}
8397

@@ -89,6 +103,9 @@ public static void nameFinder() throws IOException {
89103
Span nameSpans[] = nameFinder.find(sentence);
90104
String[] names = Span.spansToStrings(nameSpans, sentence);
91105
Arrays.stream(names).forEach(LOGGER::info);
106+
for (String name : names) {
107+
LOGGER.info(name);
108+
}
92109
}
93110

94111
public static void locationFinder() throws IOException {
@@ -99,6 +116,9 @@ public static void locationFinder() throws IOException {
99116
Span locationSpans[] = nameFinder.find(sentence);
100117
String[] locations = Span.spansToStrings(locationSpans, sentence);
101118
Arrays.stream(locations).forEach(LOGGER::info);
119+
for (String location : locations) {
120+
LOGGER.info(location);
121+
}
102122
}
103123

104124
public void trainDocumentCategorizer() {
@@ -160,7 +180,9 @@ public static void chunker() throws IOException {
160180
String[] taggedSentence = new String[] {"Out", "of", "the", "night", "that", "covers", "me"};
161181
String pos[] = new String[] { "IN", "IN", "DT", "NN", "WDT", "VBZ", "PRP"};
162182
String chunks[] = chunkerME.chunk(taggedSentence, pos);
163-
Arrays.stream(chunks).forEach(LOGGER::info);
183+
for (String chunk : chunks) {
184+
LOGGER.info(chunk);
185+
}
164186
}
165187

166188
}

0 commit comments

Comments
 (0)