* Introduction to OpenNLP

* Introduction to OpenNLP

* OpenNLP fix code
This commit is contained in:
Jesus Boadas 2017-06-12 11:55:12 -04:00 committed by maibin
parent b6d47b00a3
commit 1028ad79fd
1 changed files with 26 additions and 4 deletions

View File

@ -36,7 +36,7 @@ import opennlp.tools.util.TrainingParameters;
public class OpenNLP { public class OpenNLP {
private final static Logger LOGGER = Logger.getLogger(OpenNLP.class.getName()); private final static Logger LOGGER = Logger.getLogger(OpenNLP.class.getName());
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."; private final static String text = buildString();
private final static String sentence[] = new String[] { "James", "Jordan", "live", "in", "Oklahoma", "city", "." }; private final static String sentence[] = new String[] { "James", "Jordan", "live", "in", "Oklahoma", "city", "." };
private DoccatModel docCatModel; private DoccatModel docCatModel;
@ -45,6 +45,16 @@ public class OpenNLP {
new OpenNLP(); new OpenNLP();
} }
public static String buildString(){
StringBuilder sb = new StringBuilder();
sb.append("To get to the south:");
sb.append(" Go to the store.");
sb.append(" Buy a compass.");
sb.append(" Use the compass.");
sb.append(" Then walk to the south.");
return sb.toString();
}
public OpenNLP() { public OpenNLP() {
try { try {
sentenceDetector(); sentenceDetector();
@ -68,7 +78,9 @@ public class OpenNLP {
SentenceModel model = new SentenceModel(is); SentenceModel model = new SentenceModel(is);
SentenceDetectorME sdetector = new SentenceDetectorME(model); SentenceDetectorME sdetector = new SentenceDetectorME(model);
String sentences[] = sdetector.sentDetect(text); String sentences[] = sdetector.sentDetect(text);
Arrays.stream(sentences).forEach(LOGGER::info); for (String sentence : sentences) {
LOGGER.info(sentence);
}
is.close(); is.close();
} }
@ -77,7 +89,9 @@ public class OpenNLP {
TokenizerModel model = new TokenizerModel(is); TokenizerModel model = new TokenizerModel(is);
Tokenizer tokenizer = new TokenizerME(model); Tokenizer tokenizer = new TokenizerME(model);
String tokens[] = tokenizer.tokenize(text); String tokens[] = tokenizer.tokenize(text);
Arrays.stream(tokens).forEach(LOGGER::info); for (String token : tokens) {
LOGGER.info(token);
}
is.close(); is.close();
} }
@ -89,6 +103,9 @@ public class OpenNLP {
Span nameSpans[] = nameFinder.find(sentence); Span nameSpans[] = nameFinder.find(sentence);
String[] names = Span.spansToStrings(nameSpans, sentence); String[] names = Span.spansToStrings(nameSpans, sentence);
Arrays.stream(names).forEach(LOGGER::info); Arrays.stream(names).forEach(LOGGER::info);
for (String name : names) {
LOGGER.info(name);
}
} }
public static void locationFinder() throws IOException { public static void locationFinder() throws IOException {
@ -99,6 +116,9 @@ public class OpenNLP {
Span locationSpans[] = nameFinder.find(sentence); Span locationSpans[] = nameFinder.find(sentence);
String[] locations = Span.spansToStrings(locationSpans, sentence); String[] locations = Span.spansToStrings(locationSpans, sentence);
Arrays.stream(locations).forEach(LOGGER::info); Arrays.stream(locations).forEach(LOGGER::info);
for (String location : locations) {
LOGGER.info(location);
}
} }
public void trainDocumentCategorizer() { public void trainDocumentCategorizer() {
@ -160,7 +180,9 @@ public class OpenNLP {
String[] taggedSentence = new String[] {"Out", "of", "the", "night", "that", "covers", "me"}; String[] taggedSentence = new String[] {"Out", "of", "the", "night", "that", "covers", "me"};
String pos[] = new String[] { "IN", "IN", "DT", "NN", "WDT", "VBZ", "PRP"}; String pos[] = new String[] { "IN", "IN", "DT", "NN", "WDT", "VBZ", "PRP"};
String chunks[] = chunkerME.chunk(taggedSentence, pos); String chunks[] = chunkerME.chunk(taggedSentence, pos);
Arrays.stream(chunks).forEach(LOGGER::info); for (String chunk : chunks) {
LOGGER.info(chunk);
}
} }
} }