在Android上使用CoreNLP'无法返回null或leaf Tree的头部'

时间:2016-11-15 06:27:06

标签: 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.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环境下使用java 1.8.0_92,这段代码运行正常,但是在Android上运行代码时(编译后没有错误),我在实例化NLP类时收到错误:

  

造成:java.lang.IllegalArgumentException:无法返回头部   null或leaf Tree。                                                                        在   edu.stanford.nlp.trees.AbstractCollinsHeadFinder.determineHead(AbstractCollinsHeadFinder.java:158)                                                                        在   edu.stanford.nlp.trees.AbstractCollinsHeadFinder.determineHead(AbstractCollinsHeadFinder.java:138)                                                                        在   edu.stanford.nlp.pipeline.ParserAnnotator。(ParserAnnotator.java:132)                                                                        在   edu.stanford.nlp.pipeline.AnnotatorImplementations.parse(AnnotatorImplementations.java:132)                                                                        在   edu.stanford.nlp.pipeline.StanfordCoreNLP $ 10.create(StanfordCoreNLP.java:719)                                                                        在edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:85)                                                                        在   edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:292)                                                                        在   edu.stanford.nlp.pipeline.StanfordCoreNLP。(StanfordCoreNLP.java:129)                                                                        在   edu.stanford.nlp.pipeline.StanfordCoreNLP。(StanfordCoreNLP.java:125)

我正在使用CoreNLP 3.4.1。它不是最新版本,但它适用于Android上的Java 7。如何在Android上正确使用CoreNLP?

1 个答案:

答案 0 :(得分:7)

为什么会出现此问题?

我一直在寻找答案。我检查了罐子。有一个班级AbstractCollinsHeadFinder.java。从这个类中,出现了这个错误

  

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

此错误有两个根本原因。

  1. 如果tree为null,则会发生此错误。
  2. 如果树是叶子,则会发生此错误。

    @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;
    }
    
  3. 资源链接:

    1. https://github.com/stanfordnlp/CoreNLP/blob/master/src/edu/stanford/nlp/trees/AbstractCollinsHeadFinder.java#L163
    2. 检查参数:

      我还检查了你的代码。在你的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);
      

      资源链接:

      creates a StanfordCoreNLP object

      一个简单,完整的示例程序:

      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);
              }
          }
      }
      

      资源链接:

      1. The Stanford CoreNLP Natural Language Processing Toolkit