I want to use CoreNLP in my Android project. But when I create a CoreNLP instance like this:
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 NLP {
private StanfordCoreNLP pipeline;
Properties props;
public NLP() {
props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, pos, parse, sentiment");
pipeline = new StanfordCoreNLP(props);//-->ERROR, SEE BELOW
}
public int findSentiment(String line) {
int mainSentiment = 0;
if (line != null && line.length() > 0) {
int longest = 0;
Annotation annotation = pipeline.process(line);
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 project links to the following .jar files:
- ejml-0.23.jar
- stanford-corenlp-3.4.1.jar
- stanford-corenlp-3.4.1-models.jar
On my desktop java environment with java 1.8.0_92, this code runs correctly, but when running the code on an Android (after compiling without error), I am getting the error when the NLP class is instantiated:
Caused by: java.lang.IllegalArgumentException: Can't return head of null or leaf Tree. at edu.stanford.nlp.trees.AbstractCollinsHeadFinder.determineHead(AbstractCollinsHeadFinder.java:158) at edu.stanford.nlp.trees.AbstractCollinsHeadFinder.determineHead(AbstractCollinsHeadFinder.java:138) at edu.stanford.nlp.pipeline.ParserAnnotator.(ParserAnnotator.java:132) at edu.stanford.nlp.pipeline.AnnotatorImplementations.parse(AnnotatorImplementations.java:132) at edu.stanford.nlp.pipeline.StanfordCoreNLP$10.create(StanfordCoreNLP.java:719) at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:85) at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:292) at edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:129) at edu.stanford.nlp.pipeline.StanfordCoreNLP.(StanfordCoreNLP.java:125)
I am using CoreNLP 3.4.1. It's not the most recent version, but it works with Java 7 on Android. How can I use CoreNLP correctly on Android?