NamedEntityParser extends AbstractParser { public void parse(InputStream inputStream, ContentHandler contentHandler, Metadata metadata, ParseContext parseContext) throws IOException, SAXException, TikaException { if (!initialized) { initialize(parseContext); } if (!available) { return; } Reader reader = MediaType.TEXT_PLAIN.toString() .equals(metadata.get(Metadata.CONTENT_TYPE)) ? new InputStreamReader(inputStream, StandardCharsets.UTF_8) : secondaryParser.parse(inputStream); String text = IOUtils.toString(reader); IOUtils.closeQuietly(reader); for (NERecogniser ner : nerChain) { Map<String, Set<String>> names = ner.recognise(text); if (names != null) { for (Map.Entry<String, Set<String>> entry : names.entrySet()) { if (entry.getValue() != null) { String mdKey = MD_KEY_PREFIX + entry.getKey(); for (String name : entry.getValue()) { metadata.add(mdKey, name); } } } } } XHTMLContentHandler xhtml = new XHTMLContentHandler(contentHandler, metadata); extractOutput(text.trim(), xhtml); }  Set<MediaType> getSupportedTypes(ParseContext parseContext); void parse(InputStream inputStream, ContentHandler contentHandler,
                      Metadata metadata, ParseContext parseContext); static final Logger LOG; static final Set<MediaType> MEDIA_TYPES; static final String MD_KEY_PREFIX; static final String DEFAULT_NER_IMPL; static final String SYS_PROP_NER_IMPL; public Tika secondaryParser; }
@Test public void testParse() throws Exception { try (InputStream is = getClass().getResourceAsStream(CONFIG_FILE)) { TikaConfig config = new TikaConfig(is); Tika tika = new Tika(config); String text = "I am student at University of Southern California (USC)," + " located in Los Angeles . USC's football team is called by name Trojans." + " Mr. John McKay was a head coach of the team from 1960 - 1975"; Metadata md = new Metadata(); tika.parse(new ByteArrayInputStream(text.getBytes(Charset.defaultCharset())), md); HashSet<String> set = new HashSet<String>(); set.addAll(Arrays.asList(md.getValues("X-Parsed-By"))); assumeTrue(set.contains(NamedEntityParser.class.getName())); set.clear(); set.addAll(Arrays.asList(md.getValues("NER_PERSON"))); assumeTrue(set.contains("John McKay")); set.clear(); set.addAll(Arrays.asList(md.getValues("NER_LOCATION"))); assumeTrue(set.contains("Los Angeles")); set.clear(); set.addAll(Arrays.asList(md.getValues("NER_ORGANIZATION"))); assumeTrue(set.contains("University of Southern California")); set.clear(); set.addAll(Arrays.asList(md.getValues("NER_DATE"))); assumeTrue(set.contains("1960 - 1975")); } }
@Test public void testNerChain() throws Exception { String classNames = OpenNLPNERecogniser.class.getName() + "," + RegexNERecogniser.class.getName(); System.setProperty(NamedEntityParser.SYS_PROP_NER_IMPL, classNames); try (InputStream is = getClass().getResourceAsStream(CONFIG_FILE)) { TikaConfig config = new TikaConfig(is); Tika tika = new Tika(config); String text = "University of Southern California (USC), is located in Los Angeles ." + " Campus is busy from monday to saturday"; Metadata md = new Metadata(); tika.parse(new ByteArrayInputStream(text.getBytes(Charset.defaultCharset())), md); HashSet<String> keys = new HashSet<>(Arrays.asList(md.names())); assumeTrue(keys.contains("NER_WEEK_DAY")); assumeTrue(keys.contains("NER_LOCATION")); } }