Java Reference
In-Depth Information
+ "an odd fellow.";
String mary = "Mary thought that custard pie was the "
+ "best pie in the world. However, she loathed "
+ "chocolate pie.";
To perform this analysis, we need to use a sentiment
annotator
as shown here. This
also requires the use of the
tokenize
,
ssplit
and
parse
annotators. The
parse
an-
notator provides more structural information about the text, which will be discussed in
more detail in
Chapter 7
,
Using a Parser to Extract Relationships
:
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, parse,
sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
The text is used to create an
Annotation
instance, which is then used as the argument
to the
annotate
method that performs the actual work, as shown here:
Annotation annotation = new Annotation(review);
pipeline.annotate(annotation);
The following array holds the strings for the different sentiments possible:
String[] sentimentText = {"Very Negative", "Negative",
"Neutral", "Positive", "Very Positive"};
The
Annotation
class'
get
method returns an object that implements the
CoreMap
interface. In this case, these objects represent the results of splitting the input text into sen-
tences, as shown in the following code. For each sentence, an instance of a
Tree
object is
obtained that represents a tree structure containing a parse of the text for the sentiment.
The
getPredictedClass
method returns an index into the
sentimentText
array
reflecting the sentiment of the test:
for (CoreMap sentence : annotation.get(
CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence.get(
SentimentCoreAnnotations.AnnotatedTree.class);
int score = RNNCoreAnnotations.getPredictedClass(tree);