|
| 1 | +package com.baeldung.opennlp; |
| 2 | + |
| 3 | +import java.io.File; |
| 4 | +import java.io.FileInputStream; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.InputStream; |
| 7 | +import java.util.Arrays; |
| 8 | +import java.util.logging.Logger; |
| 9 | + |
| 10 | +import opennlp.tools.chunker.ChunkerME; |
| 11 | +import opennlp.tools.chunker.ChunkerModel; |
| 12 | +import opennlp.tools.cmdline.postag.POSModelLoader; |
| 13 | +import opennlp.tools.doccat.DoccatFactory; |
| 14 | +import opennlp.tools.doccat.DoccatModel; |
| 15 | +import opennlp.tools.doccat.DocumentCategorizerME; |
| 16 | +import opennlp.tools.doccat.DocumentSample; |
| 17 | +import opennlp.tools.doccat.DocumentSampleStream; |
| 18 | +import opennlp.tools.namefind.NameFinderME; |
| 19 | +import opennlp.tools.namefind.TokenNameFinderModel; |
| 20 | +import opennlp.tools.postag.POSModel; |
| 21 | +import opennlp.tools.postag.POSSample; |
| 22 | +import opennlp.tools.postag.POSTaggerME; |
| 23 | +import opennlp.tools.sentdetect.SentenceDetectorME; |
| 24 | +import opennlp.tools.sentdetect.SentenceModel; |
| 25 | +import opennlp.tools.tokenize.Tokenizer; |
| 26 | +import opennlp.tools.tokenize.TokenizerME; |
| 27 | +import opennlp.tools.tokenize.TokenizerModel; |
| 28 | +import opennlp.tools.tokenize.WhitespaceTokenizer; |
| 29 | +import opennlp.tools.util.InputStreamFactory; |
| 30 | +import opennlp.tools.util.InvalidFormatException; |
| 31 | +import opennlp.tools.util.ObjectStream; |
| 32 | +import opennlp.tools.util.PlainTextByLineStream; |
| 33 | +import opennlp.tools.util.Span; |
| 34 | +import opennlp.tools.util.TrainingParameters; |
| 35 | + |
| 36 | +public class OpenNLP { |
| 37 | + |
| 38 | + 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."; |
| 40 | + private final static String sentence[] = new String[] { "James", "Jordan", "live", "in", "Oklahoma", "city", "." }; |
| 41 | + |
| 42 | + private DoccatModel docCatModel; |
| 43 | + |
| 44 | + public static void main(String[] args) { |
| 45 | + new OpenNLP(); |
| 46 | + } |
| 47 | + |
| 48 | + public OpenNLP() { |
| 49 | + try { |
| 50 | + sentenceDetector(); |
| 51 | + tokenizer(); |
| 52 | + nameFinder(); |
| 53 | + locationFinder(); |
| 54 | + trainDocumentCategorizer(); |
| 55 | + documentCategorizer(); |
| 56 | + partOfSpeechTagger(); |
| 57 | + chunker(); |
| 58 | + } catch (InvalidFormatException e) { |
| 59 | + e.printStackTrace(); |
| 60 | + } catch (IOException e) { |
| 61 | + e.printStackTrace(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + public void sentenceDetector() throws InvalidFormatException, IOException { |
| 66 | + |
| 67 | + InputStream is = new FileInputStream("OpenNLP/en-sent.bin"); |
| 68 | + SentenceModel model = new SentenceModel(is); |
| 69 | + SentenceDetectorME sdetector = new SentenceDetectorME(model); |
| 70 | + String sentences[] = sdetector.sentDetect(text); |
| 71 | + Arrays.stream(sentences).forEach(LOGGER::info); |
| 72 | + is.close(); |
| 73 | + } |
| 74 | + |
| 75 | + public void tokenizer() throws InvalidFormatException, IOException { |
| 76 | + InputStream is = new FileInputStream("OpenNLP/en-token.bin"); |
| 77 | + TokenizerModel model = new TokenizerModel(is); |
| 78 | + Tokenizer tokenizer = new TokenizerME(model); |
| 79 | + String tokens[] = tokenizer.tokenize(text); |
| 80 | + Arrays.stream(tokens).forEach(LOGGER::info); |
| 81 | + is.close(); |
| 82 | + } |
| 83 | + |
| 84 | + public static void nameFinder() throws IOException { |
| 85 | + InputStream is = new FileInputStream("OpenNLP/en-ner-person.bin"); |
| 86 | + TokenNameFinderModel model = new TokenNameFinderModel(is); |
| 87 | + is.close(); |
| 88 | + NameFinderME nameFinder = new NameFinderME(model); |
| 89 | + Span nameSpans[] = nameFinder.find(sentence); |
| 90 | + String[] names = Span.spansToStrings(nameSpans, sentence); |
| 91 | + Arrays.stream(names).forEach(LOGGER::info); |
| 92 | + } |
| 93 | + |
| 94 | + public static void locationFinder() throws IOException { |
| 95 | + InputStream is = new FileInputStream("OpenNLP/en-ner-location.bin"); |
| 96 | + TokenNameFinderModel model = new TokenNameFinderModel(is); |
| 97 | + is.close(); |
| 98 | + NameFinderME nameFinder = new NameFinderME(model); |
| 99 | + Span locationSpans[] = nameFinder.find(sentence); |
| 100 | + String[] locations = Span.spansToStrings(locationSpans, sentence); |
| 101 | + Arrays.stream(locations).forEach(LOGGER::info); |
| 102 | + } |
| 103 | + |
| 104 | + public void trainDocumentCategorizer() { |
| 105 | + |
| 106 | + try { |
| 107 | + InputStreamFactory isf = new InputStreamFactory() { |
| 108 | + public InputStream createInputStream() throws IOException { |
| 109 | + return new FileInputStream("OpenNLP/doc-cat.train"); |
| 110 | + } |
| 111 | + }; |
| 112 | + ObjectStream<String> lineStream = new PlainTextByLineStream(isf, "UTF-8"); |
| 113 | + ObjectStream<DocumentSample> sampleStream = new DocumentSampleStream(lineStream); |
| 114 | + DoccatFactory docCatFactory = new DoccatFactory(); |
| 115 | + docCatModel = DocumentCategorizerME.train("en", sampleStream, TrainingParameters.defaultParams(), docCatFactory); |
| 116 | + } catch (IOException e) { |
| 117 | + e.printStackTrace(); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + public void documentCategorizer() { |
| 122 | + DocumentCategorizerME myCategorizer = new DocumentCategorizerME(docCatModel); |
| 123 | + double[] outcomes = myCategorizer.categorize(sentence); |
| 124 | + String category = myCategorizer.getBestCategory(outcomes); |
| 125 | + |
| 126 | + if (category.equalsIgnoreCase("GOOD")) { |
| 127 | + LOGGER.info("Document is positive :) "); |
| 128 | + } else { |
| 129 | + LOGGER.info("Document is negative :( "); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + public static void partOfSpeechTagger() throws IOException { |
| 134 | + try { |
| 135 | + POSModel posModel = new POSModelLoader().load(new File("OpenNLP/en-pos-maxent.bin")); |
| 136 | + POSTaggerME posTaggerME = new POSTaggerME(posModel); |
| 137 | + InputStreamFactory isf = new InputStreamFactory() { |
| 138 | + public InputStream createInputStream() throws IOException { |
| 139 | + return new FileInputStream("OpenNLP/PartOfSpeechTag.txt"); |
| 140 | + } |
| 141 | + }; |
| 142 | + ObjectStream<String> lineStream = new PlainTextByLineStream(isf, "UTF-8"); |
| 143 | + String line; |
| 144 | + while ((line = lineStream.read()) != null) { |
| 145 | + String whitespaceTokenizerLine[] = WhitespaceTokenizer.INSTANCE.tokenize(line); |
| 146 | + String[] tags = posTaggerME.tag(whitespaceTokenizerLine); |
| 147 | + POSSample posSample = new POSSample(whitespaceTokenizerLine, tags); |
| 148 | + LOGGER.info(posSample.toString()); |
| 149 | + } |
| 150 | + lineStream.close(); |
| 151 | + } catch (IOException e) { |
| 152 | + e.printStackTrace(); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + public static void chunker() throws IOException { |
| 157 | + InputStream is = new FileInputStream("OpenNLP/en-chunker.bin"); |
| 158 | + ChunkerModel cModel = new ChunkerModel(is); |
| 159 | + ChunkerME chunkerME = new ChunkerME(cModel); |
| 160 | + String[] taggedSentence = new String[] {"Out", "of", "the", "night", "that", "covers", "me"}; |
| 161 | + String pos[] = new String[] { "IN", "IN", "DT", "NN", "WDT", "VBZ", "PRP"}; |
| 162 | + String chunks[] = chunkerME.chunk(taggedSentence, pos); |
| 163 | + Arrays.stream(chunks).forEach(LOGGER::info); |
| 164 | + } |
| 165 | + |
| 166 | +} |
0 commit comments