@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")); } }
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); }
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); } }
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); }  }
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); }
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; }