Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stanford CoreNLP sentiment

I'm trying to implement the coreNLP sentiment analyzer in eclipse. Getting the error:

Unable to resolve "edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz"

As either class path, filename or URL. I installed all of the NLP files using maven so I am not sure why it is looking for something else. Here is the code I am getting the error on.

import java.util.Properties;


import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;

public class StanfordSentiment {


StanfordCoreNLP pipeline; 



public StanfordSentiment(){
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");

    pipeline = new StanfordCoreNLP(props);


}

public float calculateSentiment (String text) {


        float mainSentiment = 0;

        int longest = 0;
        Annotation annotation = pipeline.process(text);
        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
            Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
            int sentiment = RNNCoreAnnotations.getPredictedClass(tree) - 2;
            String partText = sentence.toString();
            if (partText.length() > longest) {
                mainSentiment = sentiment;
                longest = partText.length();
            }

        }

       return mainSentiment;



}
}
like image 770
English Grad Avatar asked Apr 14 '14 15:04

English Grad


People also ask

What is Stanford CoreNLP?

Stanford CoreNLP [backup download page] An integrated suite of natural language processing tools for English, Spanish, and (mainland) Chinese in Java, including tokenization, part-of-speech tagging, named entity recognition, parsing, and coreference.

How does Stanford CoreNLP work?

CoreNLP enables users to derive linguistic annotations for text, including token and sentence boundaries, parts of speech, named entities, numeric and time values, dependency and constituency parses, coreference, sentiment, quote attributions, and relations.

How do you perform sentiment analysis in Java?

In Java code, the Stanford CoreNLP sentiment classifier is used as follows. To start, you build up a text processing pipeline by adding the annotators required to perform sentiment analysis, such as tokenize , ssplit , parse , and sentiment .

What is sentiment analysis?

Sentiment analysis, also referred to as opinion mining, is an approach to natural language processing (NLP) that identifies the emotional tone behind a body of text. This is a popular way for organizations to determine and categorize opinions about a product, service, or idea.


1 Answers

public class SentimentAnalysis {

    public static void main(String[] args) throws IOException {
        String text = "I am very happy";
        Properties props = new Properties();
        props.setProperty("annotators",
                "tokenize, ssplit, pos, lemma, parse, sentiment");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        Annotation annotation = pipeline.process(text);
        List<CoreMap> sentences = annotation
                .get(CoreAnnotations.SentencesAnnotation.class);
        for (CoreMap sentence : sentences) {
            String sentiment = sentence
                    .get(SentimentCoreAnnotations.ClassName.class);
            System.out.println(sentiment + "\t" + sentence);
        }
    }
}

Hope it will help..:)

like image 55
aug13 Avatar answered Oct 12 '22 01:10

aug13