Java Stanford NLP在运行代码时出现异常

Java Stanford NLP在运行代码时出现异常,java,stanford-nlp,Java,Stanford Nlp,我正在尝试使用文件输入并输出到另一个文件来运行此代码: 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.sent

我正在尝试使用文件输入并输出到另一个文件来运行此代码:

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...");


  }

}
当我使用Eclipse Neon运行代码时,出现以下错误:

[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)
现在我不明白为什么会这样?如何才能成功运行此代码?

树对象似乎为空。然而,为什么会发生这种情况需要对你正在做的事情有更深入的了解


只是一个猜测,但似乎您指定的类可能不在句子中。。。例如,您所称的句子。get,在要获取的句子中没有感伤注释树。类,因此该方法返回null

您没有在管道中运行情感注释器或解析器。下面是一个命令行调用,显示了运行管道和获取情绪。通过将管道的属性设置为与此调用指定的属性相匹配,可以轻松地将其改编为Java代码

java -Xmx8g edu.stanford.nlp.pipeline.StanfordCoreNLP -annotators tokenize,ssplit,pos,lemma,parse,sentiment -parse.binaryTrees -file example-sentence.txt -outputFormat text
您需要将parse和情绪注释器添加到管道中,并且需要确保parse注释器生成的二叉树的parse.binaryTrees属性设置为true

下面是一些显示访问情绪的示例代码:

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

请看这是命令行。我已经说过我正在使用Eclipse Neon。如果你知道如何使用Eclipse,请建议我。我知道了。你能告诉我如何编辑我的文件代码吗。按照你的建议,我只能从文件中读到一行。因为我需要读一个完整的文件。请看我的输入示例。你在吗?你能帮忙吗?谢谢你的帮助
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));
    }
  }
}