I downloaded stanford core nlp packages and tried to test it on my machine.
Using command: java -cp \"*\" -mx1g edu.stanford.nlp.sentiment.SentimentPipeline -f
You need to add the "sentiment" annotator to the list of annotators:
-annotators tokenize,ssplit,pos,lemma,parse,sentiment
This will add a "sentiment" property to each sentence node in your XML.
Per the example here you need to run the Sentiment Analysis.
java -cp "*" -mx5g edu.stanford.nlp.sentiment.SentimentPipeline -file input.txt
Apparently this is a memory expensive operation, it may not complete with only 1 gigabyte. Then you can use the "Evaluation Tool"
java -cp "*" edu.stanford.nlp.sentiment.Evaluate edu/stanford/nlp/models/sentiment/sentiment.ser.gz input.txt
This is working fine for me -
Maven Dependencies :
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.5.2</version>
<classifier>models</classifier>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-corenlp</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>edu.stanford.nlp</groupId>
<artifactId>stanford-parser</artifactId>
<version>3.5.2</version>
</dependency>
Java Code :
public static void main(String[] args) throws IOException {
String text = "This World is an amazing place";
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.SentimentClass.class);
System.out.println(sentiment + "\t" + sentence);
}
}
Results :
Very positive This World is an amazing place
You can do the following in your code:
String text = "I am feeling very sad and frustrated.";
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.SentimentClass.class);
System.out.println(sentiment + "\t" + sentence);
}
It will print the sentiment of the sentence and the sentence itself, e.g. "I am feeling very sad and frustrated.":
Negative I am feeling very sad and frustrated.