I'm trying to apply Sentiment Analysis for a dataset containing tweets that I already collected, for some experiments I run for my thesis. I have followed various tutorials on the Internet and my code is the following so far:
public class SentimentAnalyzer {
public static StanfordCoreNLP pipeline;
public SentimentAnalyzer() {
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
pipeline = new StanfordCoreNLP(props);
}
public static int getSentiment(String tweet) {
int mainSentiment = 0;
if (tweet != null && tweet.length() > 0) {
int longest = 0;
Annotation annotation = pipeline.process(tweet);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
String partText = sentence.toString();
if (partText.length() > longest) {
mainSentiment = sentiment;
longest = partText.length();
}
}
}
return mainSentiment;
}
}
The problem is that when I run this piece of code, Java returns an Illegal Argument Exception:
Adding annotator tokenize
Adding annotator ssplit
Adding annotator parse
Loading parser from serialized file
edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... done [1,4 sec].
Adding annotator sentiment
Exception in thread "main" java.lang.IllegalArgumentException: No annotator named sentiment
at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:78)
at edu.stanfoJava Result: 1
but every single tutorial lists "sentiment" as a valid annotator! The program specifically hangs when calling the StanfordCoreNLP constructor (inside the SentimentAnalyzer constructor) and though I have tried every configuration that I can think of (making the pipeline not-static, creating the pipeline eveytime in getSentiment() method, using a .properties file instead), the problem stays. I'm using StanfordCoreNLP 3.3.1, along with the models .jar (it's the 3.5.2 version, though) and ejml 0.23 -I have used 3.5.2 version, but with no success.
EDIT: I replaced 3.3.2 with 3.5.2 and now the error changed:
Loading parser from text file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz Exception in thread "main" java.lang.RuntimeException: edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz: expecting BEGIN block; got ��
The rest is unreadable and cannot copy that either. It seems that it's a problem with the models library, at least the directory that the error points, belongs to models. I also updated with the latest library from their GitHub repository, but the error is the same.
Turns out that StanfordCoreNLP v. 1.3.4 was left out from a previous implementation since August. When I removed it, the code started working fine.