1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | InputStream modelIn = new FileInputStream("en-ner-person.bin"); try { TokenNameFinder model = new TokenNameFinderModel(modelIn); } catch (IOException e) { e.printStackTrace(); } finally { if (modelIn != null) { try { modelIn.close(); } catch (IOException e) { } } } NameFinderME nameFinder = new NameFinderME(model); for (String document[][] : documents) { for (String[] sentence : document) { Span nameSpans[] = find(sentence); // do something with the names } nameFinder.clearAdaptiveData() } InputStream in = getClass() .getClassLoader() .getResourceAsStream( "opennlp/tools/namefind/AnnotatedSentences.txt"); String encoding = "ISO-8859-1"; ObjectStream<NameSample> sampleStream = new NameSampleDataStream( new PlainTextByLineStream(new InputStreamReader(in, encoding))); TokenNameFinderModel nameFinderModel = NameFinderME.train("en", "default", sampleStream, Collections .<String, Object> emptyMap(), 70, 1); TokenNameFinder nameFinder = new NameFinderME(nameFinderModel); // now test if it can detect the sample sentences String sentence[] = { "Alisa", "appreciated", "the", "hint", "and", "enjoyed", "a", "delicious", "traditional", "meal." }; Span names[] = nameFinder.find(sentence); |