“无法返回空头或叶树”与CoreNLP在Android上
我想在我的Android项目中使用CoreNLP。但是当我创建一个像这样的CoreNLP实例时:
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;
}
}
该项目链接到以下.jar文件:
- ejml-0.23.jar
- 斯坦福-科伦普-3.4.1.jar
- 斯坦福-科伦普-3.4.1-模型.jar
在我使用java 1.8.0_92的桌面java环境中,此代码可以正常运行,但是当在Android上运行代码时(编译无错误后),当NLP类实例化时,我收到错误:
由以下原因引起:java.lang.IllegalArgumentException:无法返回 null 或 leaf Tree 的 head。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)
我使用的是CoreNLP 3.4.1。它不是最新版本,但它适用于Android上的Java 7。如何在Android上正确使用CoreNLP?