'Can't return head of null or leaf Tree' with CoreNLP on Android
Asked Answered
D

1

15

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?

Detritus answered 15/11, 2016 at 6:27 Comment(5)
I've just downloaded CoreNLP 3.4.1 and used your code in a hello world android app. It seemed to work fine. I can't reproduce your errorsDifficile
are you on java 8?Detritus
user2212461, yeahDifficile
And you would like it to run on Java 8 or 7? And what Android device are you testing (API version)?Faye
As mentioned above, would you mind provide more info on the Android part, which we can reproduce your errorsWilmott
G
8

Why this problem occurs?

I was looking for the answer. I have checked the jar. There is a class AbstractCollinsHeadFinder.java. From this class, this error comes

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

There are 2 root causes for this error.

  1. If tree is null, then this error occurs.
  2. If tree is leaf, then this error occurs.

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

Resource Link:

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

Check for parameters:

I have also checked your code. In your setProperty(...), there are some parameters. Maybe there are some parameter missing. So, you can create a object by following the code.

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

Resource Link:

creates a StanfordCoreNLP object


A simple, complete example program:

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

Resource Link:

  1. The Stanford CoreNLP Natural Language Processing Toolkit
Gabi answered 21/11, 2016 at 17:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.