Java ';Can';t返回空树或叶树的头部';Android上的CoreNLP

Java ';Can';t返回空树或叶树的头部';Android上的CoreNLP,java,android,sentiment-analysis,stanford-nlp,Java,Android,Sentiment Analysis,Stanford Nlp,我想在我的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.pipe

我想在我的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
  • 斯坦福-corenlp-3.4.1.jar
  • 斯坦福-corenlp-3.4.1-models.jar
在我使用java 1.8.092的桌面java环境中,此代码运行正常,但在Android上运行代码时(编译无误后),我在实例化NLP类时遇到错误:

原因:java.lang.IllegalArgumentException:无法返回 空或叶树。 在 edu.stanford.nlp.trees.AbstractCollinsHeadFinder.determineHead(AbstractCollinsHeadFinder.java:158) 在 edu.stanford.nlp.trees.AbstractCollinsHeadFinder.determineHead(AbstractCollinsHeadFinder.java:138) 在 ParserAnnotator.java:132) 在 annotatorimplements.parse(annotatorimplements.java:132) 在 StanfordCoreNLP$10.create(StanfordCoreNLP.java:719) 位于edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:85) 在 StanfordCoreNLP.construct(StanfordCoreNLP.java:292) 在 StanfordCoreNLP.(StanfordCoreNLP.java:129) 在 StanfordCoreNLP.java:125)

我正在使用CoreNLP 3.4.1。这不是最新的版本,但它可以在Android上与Java7一起使用。如何在Android上正确使用CoreNLP?

为什么会出现此问题? 我在寻找答案。我已经检查过罐子了。有一个类
AbstractCollinsHeadFinder.java
。这个错误来自这个类

edu.stanford.nlp.trees.AbstractCollinsHeadFinder.determineHead(AbstractCollinsHeadFinder.java:158) 在 edu.stanford.nlp.trees.AbstractCollinsHeadFinder.determineHead(AbstractCollinsHeadFinder.java:138)

此错误有两个根本原因

  • 如果树为空,则会发生此错误
  • 如果树是叶子,则会发生此错误

    @Override
    public Tree determineHead(Tree t, Tree parent) {
      if (nonTerminalInfo == null) {
        throw new IllegalStateException("Classes derived from AbstractCollinsHeadFinder must create and fill HashMap nonTerminalInfo.");
      }
      // The error mainly generate for the following condition
      if (t == null || t.isLeaf()) {
        throw new IllegalArgumentException("Can't return head of null or leaf Tree."); 
      }
      if (DEBUG) {
        log.info("determineHead for " + t.value());
      }
    
      Tree[] kids = t.children();
      -------------
      -------------
      return theHead;
    }
    
  • 资源链接:

  • 检查参数: 我也检查了你的代码。在setProperty(…)中,有一些参数可能缺少一些参数。因此,您可以按照以下代码创建一个对象

    // creates a StanfordCoreNLP object, with POS tagging, lemmatization, NER, parsing, and coreference resolution 
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    
    资源链接:


    一个简单、完整的示例程序:
    import java.io.*;
    导入java.util.*;
    导入edu.stanford.nlp.io.*;
    导入edu.stanford.nlp.ling.*;
    导入edu.stanford.nlp.pipeline.*;
    导入edu.stanford.nlp.trees.*;
    导入edu.stanford.nlp.trees.treeCorenotations.*;
    导入edu.stanford.nlp.util.*;
    公共类StanfordCorenlpe示例{
    公共静态void main(字符串[]args)引发IOException{
    PrintWriter xmlOut=新的PrintWriter(“xmlOutput.xml”);
    Properties props=新属性();
    props.setProperty(“注释器”,
    “标记化、ssplit、pos、引理、ner、解析”);
    StanfordCoreNLP管道=新的StanfordCoreNLP(道具);
    注释=新注释(
    “这是一个简短的句子,这是另一个。”);
    管道注释(注释);
    管道.xmlPrint(注释,xmlOut);
    //注释是地图,您可以获取并使用
    //分别进行各种分析。例如
    //获取文本中第一个句子的分析树。
    列出句子=注释
    .get(CoreAnnotations.SentencesAnotation.class);
    if(句子!=null&&句子.size()>0){
    CoreMap语句=语句。获取(0);
    Tree-Tree=句子.get(TreeAnnotation.class);
    PrintWriter out=新的PrintWriter(System.out);
    println(“解析的第一句是:”);
    树。打印(输出);
    }
    }
    }
    
    资源链接:

  • 我刚刚在hello world android应用程序中下载并使用了您的代码。它似乎工作得很好。我无法重现您的错误您在java 8上运行吗?用户2212461,是的,您希望它在java 8或java 7上运行吗?您正在测试什么Android设备(API版本)?如上所述,您是否介意提供更多关于Android部分的信息,我们可以重现您的错误
    import java.io.*;
    import java.util.*;
    import edu.stanford.nlp.io.*;
    import edu.stanford.nlp.ling.*;
    import edu.stanford.nlp.pipeline.*;
    import edu.stanford.nlp.trees.*;
    import edu.stanford.nlp.trees.TreeCoreAnnotations.*;
    import edu.stanford.nlp.util.*;
    
    public class StanfordCoreNlpExample {
        public static void main(String[] args) throws IOException {
            PrintWriter xmlOut = new PrintWriter("xmlOutput.xml");
            Properties props = new Properties();
            props.setProperty("annotators",
                    "tokenize, ssplit, pos, lemma, ner, parse");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
            Annotation annotation = new Annotation(
                    "This is a short sentence. And this is another.");
            pipeline.annotate(annotation);
            pipeline.xmlPrint(annotation, xmlOut);
            // An Annotation is a Map and you can get and use the
            // various analyses individually. For instance, this
            // gets the parse tree of the 1st sentence in the text.
            List<CoreMap> sentences = annotation
                    .get(CoreAnnotations.SentencesAnnotation.class);
            if (sentences != null && sentences.size() > 0) {
                CoreMap sentence = sentences.get(0);
                Tree tree = sentence.get(TreeAnnotation.class);
                PrintWriter out = new PrintWriter(System.out);
                out.println("The first sentence parsed is:");
                tree.pennPrint(out);
            }
        }
    }