SentimentAnalysisParser extends AbstractParser implements Initializable { @Override public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException { if (classifier == null) { LOG.warn(getClass().getSimpleName() + " is not configured properly."); return; } String inputString = IOUtils.toString(stream, "UTF-8"); String sentiment = classifier.predict(inputString); metadata.add("Sentiment", sentiment); }  @Override void initialize(Map<String, Param> params); @Override void checkInitialization(InitializableProblemHandler handler); @Override Set<MediaType> getSupportedTypes(ParseContext context); @Override void parse(InputStream stream, ContentHandler handler,
                      Metadata metadata, ParseContext context); static final String DEF_MODEL; }
@Test public void endToEndTest() throws Exception { Tika tika = getTika("tika-config-sentiment-opennlp.xml"); if (tika == null) { return; } String text = "What a wonderful thought it is that" + " some of the best days of our lives haven't happened yet."; ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes(Charset.defaultCharset())); Metadata md = new Metadata(); tika.parse(stream, md); String sentiment = md.get("Sentiment"); assertNotNull(sentiment); assertEquals("positive", sentiment); }
@Test public void testCategorical() throws Exception{ Tika tika = getTika("tika-config-sentiment-opennlp-cat.xml"); if (tika == null) { return; } String text = "Whatever, I need some cooling off time!"; ByteArrayInputStream stream = new ByteArrayInputStream(text.getBytes(Charset.defaultCharset())); Metadata md = new Metadata(); tika.parse(stream, md); String sentiment = md.get("Sentiment"); assertNotNull(sentiment); assertEquals("angry", sentiment); }