"Не удается вернуть голову null или дерева листьев" с помощью CoreNLP на Android

Я хочу использовать CoreNLP в своем Android-проекте. Но когда я создаю экземпляр 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 или дерево листьев.                                                                      в 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)                                                                      at 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. Это не самая последняя версия, но она работает с Java 7 на Android. Как я могу правильно использовать CoreNLP на Android?

Ответы

Ответ 1

Почему возникает эта проблема?

Я искал ответ. Я проверил банку. Существует класс AbstractCollinsHeadFinder.java. Из этого класса эта ошибка возникает

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

Для этой ошибки существует 2 причины.

  • Если дерево равно null, то возникает эта ошибка.
  • Если дерево является листом, то возникает эта ошибка.

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

Ссылка на ресурс:

создает объект StanfordCoreNLP


Простая, полная примерная программа:

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

Ссылка на ресурс: