Search code examples
javastanford-nlp

Stanford NLP giving exception while running code


I am trying to run this code using file input and out put to another file:

import java.util.*;

import edu.stanford.nlp.pipeline.*;
import edu.stanford.nlp.io.*;
import edu.stanford.nlp.ling.*;
import edu.stanford.nlp.neural.rnn.*;
import edu.stanford.nlp.sentiment.*;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations.SentimentAnnotatedTree;
import edu.stanford.nlp.trees.*;
import edu.stanford.nlp.util.*;

import java.io.BufferedReader;
//import java.io.BufferedWriter;
import java.io.FileReader;
//import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class TestCoreNLP {

  public static void main(String[] args) throws IOException {
    PrintWriter out =  new PrintWriter("/home/aims/Desktop/outputNLP1");

    Properties props=new Properties();
    props.setProperty("annotators","tokenize, ssplit, pos,lemma");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    Annotation annotation;  
    String readString = "";
        //PrintWriter pw = null;
    BufferedReader br  = new BufferedReader ( new FileReader ( "/home/aims/Desktop/testNLP" )  ) ;
        //pw = new PrintWriter ( new BufferedWriter ( new FileWriter ( "/home/aims/Desktop/outputNLP", true )  )  ) ;      
    //String x = "";
        while  (( readString = br.readLine ())  != null)   {
            // pw.println ( readString ) ; 
             //String xx=readString;x=xx;//System.out.println("OKKKKK"); 
        annotation = new Annotation(readString);
 //System.out.print(readString);
    pipeline.annotate(annotation);    //System.out.println("LamoohAKA");
    pipeline.prettyPrint(annotation, out);
    out.println();
    out.println("The top level annotation");
    out.println(annotation.toShorterString());
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);

    if (sentences != null && !sentences.isEmpty()) {
        for (int i = 0; i < sentences.size (); i++) {
            CoreMap sentence = sentences.get(i);
            Tree tree = sentence.get(SentimentAnnotatedTree.class);//Tree tree = sentence.get(SentimentAnnotatedTree.class);
            int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
            String sentimentName = sentence.get(SentimentCoreAnnotations.SentimentClass.class);

            out.println();
            out.println("The sentence is:");
            out.println(sentence.toShorterString());
            out.println();
            out.println("Sentiment of \n> \""+sentence.get(CoreAnnotations.TextAnnotation.class)+"\"\nis: " + sentiment+" (i.e., "+sentimentName+")");
            out.println();
          }
    }

    IOUtils.closeIgnoringExceptions(out);
        }
        br.close (  ) ;
   // pw.close (  ) ;
    System.out.println("Done...");


  }

}

The input tp this code is:

I am glad you are here.
I will see you tomorrow.
I hate you.
Remember me!
I like ice-cream to utmost level of likeness.

When I ran the code using Eclipse Neon, I got the following error:

[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator tokenize
[main] INFO edu.stanford.nlp.pipeline.TokenizerAnnotator - No tokenizer type provided. Defaulting to PTBTokenizer.
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator ssplit
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator pos
[main] INFO edu.stanford.nlp.tagger.maxent.MaxentTagger - Loading POS tagger from edu/stanford/nlp/models/pos-tagger/english-left3words/english-left3words-distsim.tagger ... done [2.4 sec].
[main] INFO edu.stanford.nlp.pipeline.StanfordCoreNLP - Adding annotator lemma
Exception in thread "main" java.lang.NullPointerException
    at edu.stanford.nlp.neural.rnn.RNNCoreAnnotations.getPredictedClass(RNNCoreAnnotations.java:83)
    at TestCoreNLP.main(TestCoreNLP.java:48)

Now I am not understanding why it is happening? What should I do to run this code successfully?


Solution

  • You aren't running the sentiment annotator or parser in your pipeline. Here is a commandline call that shows running a pipeline and getting sentiment. You can easily adapt it to Java code by setting the properties for your pipeline to match the ones specified by this call.

    java -Xmx8g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,parse,sentiment -parse.binaryTrees -file example-sentence.txt -outputFormat text
    

    You need to add the parse and sentiment annotators to your pipeline and you need to make sure that the parse annotator produces binary trees with the parse.binaryTrees property being set to true.

    Here is some sample code that shows accessing sentiment:

    import edu.stanford.nlp.ling.*;
    import edu.stanford.nlp.pipeline.*;
    import edu.stanford.nlp.sentiment.*;
    import edu.stanford.nlp.util.*;
    import java.util.Properties;
    
    public class SentimentExample {
    
      public static void main(String[] args) {
        Annotation document = new Annotation("I liked the first movie.  I hated the second movie.");
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize,ssplit,pos,lemma,ner,parse,sentiment");
        props.setProperty("parse.binaryTrees","true");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
        pipeline.annotate(document);
        for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
          System.out.println("---");
          System.out.println(sentence.get(CoreAnnotations.TextAnnotation.class));
          System.out.println(sentence.get(SentimentCoreAnnotations.SentimentClass.class));
        }
      }
    }