Java Code Examples for opennlp.tools.tokenize.Tokenizer#tokenize()
The following examples show how to use
opennlp.tools.tokenize.Tokenizer#tokenize() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Chapter1.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 6 votes |
private static void nameFinderExample() { try { String[] sentences = { "Tim was a good neighbor. Perhaps not as good a Bob " + "Haywood, but still pretty good. Of course Mr. Adam " + "took the cake!"}; Tokenizer tokenizer = SimpleTokenizer.INSTANCE; TokenNameFinderModel model = new TokenNameFinderModel(new File( "C:\\OpenNLP Models", "en-ner-person.bin")); NameFinderME finder = new NameFinderME(model); for (String sentence : sentences) { // Split the sentence into tokens String[] tokens = tokenizer.tokenize(sentence); // Find the names in the tokens and return Span objects Span[] nameSpans = finder.find(tokens); // Print the names extracted from the tokens using the Span data System.out.println(Arrays.toString( Span.spansToStrings(nameSpans, tokens))); } } catch (IOException ex) { ex.printStackTrace(); } }
Example 2
Source File: Chapter2.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 6 votes |
private static void trainingATokenizer() { createOpenNLPModel(); try { paragraph = "A demonstration of how to train a tokenizer."; InputStream modelInputStream = new FileInputStream(new File( ".", "mymodel.bin")); TokenizerModel model = new TokenizerModel(modelInputStream); Tokenizer tokenizer = new TokenizerME(model); String tokens[] = tokenizer.tokenize(paragraph); for (String token : tokens) { System.out.println(token); } } catch (IOException ex) { ex.printStackTrace(); } }
Example 3
Source File: OpenNLPAnnotator.java From Stargraph with MIT License | 6 votes |
@Override public List<Word> doRun(Language language, String sentence) { Tokenizer tokenizer = new TokenizerME(getTokenizerModel(language)); POSTaggerME tagger = new POSTaggerME(getPOSModel(language)); String[] tokens = tokenizer.tokenize(sentence); String[] tags = tagger.tag(tokens); PartOfSpeechSet posSet = PartOfSpeechSet.getPOSSet(language); List<Word> words = new ArrayList<>(); for (int i = 0; i < tokens.length; i++) { words.add(new Word(posSet.valueOf(tags[i]), tokens[i])); } return words; }
Example 4
Source File: Chapter4.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 5 votes |
private static void usingMultipleNERModels() { // Models - en-ner-person.bin en-ner-location.bin en-ner-money.bin // en-ner-organization.bin en-ner-time.bin try { InputStream tokenStream = new FileInputStream( new File(getModelDir(), "en-token.bin")); TokenizerModel tokenModel = new TokenizerModel(tokenStream); Tokenizer tokenizer = new TokenizerME(tokenModel); String modelNames[] = {"en-ner-person.bin", "en-ner-location.bin", "en-ner-organization.bin"}; ArrayList<String> list = new ArrayList(); for (String name : modelNames) { TokenNameFinderModel entityModel = new TokenNameFinderModel( new FileInputStream( new File(getModelDir(), name))); NameFinderME nameFinder = new NameFinderME(entityModel); for (int index = 0; index < sentences.length; index++) { String tokens[] = tokenizer.tokenize(sentences[index]); Span nameSpans[] = nameFinder.find(tokens); for (Span span : nameSpans) { list.add("Sentence: " + index + " Span: " + span.toString() + " Entity: " + tokens[span.getStart()]); } } } System.out.println("Multiple Entities"); for (String element : list) { System.out.println(element); } } catch (Exception ex) { ex.printStackTrace(); } }
Example 5
Source File: Chapter4.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 5 votes |
private static void usingMultipleNERModels() { // Models - en-ner-person.bin en-ner-location.bin en-ner-money.bin // en-ner-organization.bin en-ner-time.bin try { InputStream tokenStream = new FileInputStream( new File(getModelDir(), "en-token.bin")); TokenizerModel tokenModel = new TokenizerModel(tokenStream); Tokenizer tokenizer = new TokenizerME(tokenModel); String modelNames[] = {"en-ner-person.bin", "en-ner-location.bin", "en-ner-organization.bin"}; ArrayList<String> list = new ArrayList(); for (String name : modelNames) { TokenNameFinderModel entityModel = new TokenNameFinderModel( new FileInputStream( new File(getModelDir(), name))); NameFinderME nameFinder = new NameFinderME(entityModel); for (int index = 0; index < sentences.length; index++) { String tokens[] = tokenizer.tokenize(sentences[index]); Span nameSpans[] = nameFinder.find(tokens); for (Span span : nameSpans) { list.add("Sentence: " + index + " Span: " + span.toString() + " Entity: " + tokens[span.getStart()]); } } } System.out.println("Multiple Entities"); for (String element : list) { System.out.println(element); } } catch (Exception ex) { ex.printStackTrace(); } }
Example 6
Source File: Chapter2.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 5 votes |
private static void usingTheTokenizerMEClass() { try { InputStream modelIn = new FileInputStream(new File( getModelDir(), "en-token.bin")); TokenizerModel model = new TokenizerModel(modelIn); Tokenizer tokenizer = new TokenizerME(model); String tokens[] = tokenizer.tokenize(paragraph); for (String token : tokens) { System.out.println(token); } } catch (IOException ex) { ex.printStackTrace(); } }
Example 7
Source File: BasicActions.java From knowledge-extraction with Apache License 2.0 | 5 votes |
public String[] testTokenizer(){ String[] tokens = {}; try (InputStream modelIn = BasicActions.class.getClassLoader() .getResourceAsStream(Consts.EN_TOKEN_MODEL);) { TokenizerModel tokenModel = new TokenizerModel(modelIn); Tokenizer tokenizer = new TokenizerME(tokenModel); tokens = tokenizer.tokenize(TEST_PHRASE); System.out.println(Arrays.toString(tokens)); } catch (IOException e) { e.printStackTrace(); } return tokens; }
Example 8
Source File: NERDemo.java From Natural-Language-Processing-with-Java-Second-Edition with MIT License | 4 votes |
public static void main(String args[]){ String sentences[] = {"Joe was the last person to see Fred. ", "He saw him in Boston at McKenzie's pub at 3:00 where he " + " paid $2.45 for an ale. ", "Joe wanted to go to Vermont for the day to visit a cousin who " + "works at IBM, but Sally and he had to look for Fred"}; String sentence = "He was the last person to see Fred."; try { InputStream tokenStream = new FileInputStream(new File(getResourcePath()+ "en-token.bin")); InputStream modelStream = new FileInputStream(new File(getResourcePath() + "en-ner-person.bin")); TokenizerModel tokenModel = new TokenizerModel(tokenStream); Tokenizer tokenizer = new TokenizerME(tokenModel); TokenNameFinderModel entityModel = new TokenNameFinderModel(modelStream); NameFinderME nameFinder = new NameFinderME(entityModel); String tokens1[] = tokenizer.tokenize(sentence); Span nameSpans1[] = nameFinder.find(tokens1); for (int i = 0; i < nameSpans1.length; i++) { System.out.println("Span: " + nameSpans1[i].toString()); System.out.println("Entity: " + tokens1[nameSpans1[i].getStart()]); } System.out.println("---------- Multiple Sentences -----------"); for (String sentence1 : sentences) { String tokens[] = tokenizer.tokenize(sentence1); Span nameSpans[] = nameFinder.find(tokens); for (int i = 0; i < nameSpans.length; i++) { System.out.println("Span: " + nameSpans[i].toString()); System.out.println("Entity: " + tokens[nameSpans[i].getStart()]); } System.out.println(); } } catch(Exception e){ System.out.println(e); } }