Using the Stanford pipeline to perform sentiment analysis

In this section, we will illustrate how the Stanford API can be used to perform sentiment analysis. We will use the StanfordCoreNLP pipeline to perform this analysis on different texts.

We will use three different texts, as defined in the following code. The review string is a movie review from Rotten Tomatoes (http://www.rottentomatoes.com/m/forrest_gump/) about the movie Forrest Gump:

String review = "An overly sentimental film with a somewhat " 
    + "problematic message, but its sweetness and charm " 
    + "are occasionally enough to approximate true depth " 
    + "and grace. "; 
 
String sam = "Sam was an odd sort of fellow. Not prone " 
    + "to angry and not prone to merriment. Overall, " 
    + "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 in the following code. This also requires the use of the tokenize, ssplit, and parse annotators. The parse annotator provides more structural information about the text, which will be discussed in more detail in Chapter 10, Using Parsers 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 possible sentiments:

String[] sentimentText = {"Very Negative", "Negative",  
    "Neutral", "Positive", "Very Positive"};

The Annotation class's get method returns an object that implements the CoreMap interface. In this case, these objects represent the results of splitting the input text into sentences, 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 to 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); 
    System.out.println(sentimentText[score]); 
} 

When the code is executed using the review string, we get the following output:

Positive  

The text sam consists of three sentences. The output for each is as follows, showing the sentiment for each sentence:

Neutral
Negative
Neutral  

The text mary consists of two sentences. The output for each is as follows:

Positive
Neutral  
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset