2017-02-22 15 views
0

Я пытаюсь запустить этот код, используя входной файл и из положить в другой файл:Standford 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.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..."); 


    } 

} 

вход Т.П. этого кода является:

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. 

Когда я побежал код, используя 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) 

Теперь я не понимая, ш Это происходит? Что мне следует сделать, чтобы успешно запустить этот код?

ответ

1

Вы не используете аннотатор настроений или парсер в своем конвейере. Вот вызов командной строки, который показывает запуск конвейера и получение настроя. Вы можете легко адаптировать его к Java-коду, установив свойства для вашего конвейера в соответствии с параметрами, указанными в этом вызове.

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

Вы должны добавить parse и sentiment аннотаторы к вашему трубопроводу, и вам необходимо убедиться, что parse аннотатор производит бинарные деревья с собственностью parse.binaryTrees быть установлен верно.

Ниже приведен пример кода, который показывает доступ настроения:

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

Смотрите это командная строка. Я уже сказал, что использую Eclipse Neon. Я знаю, как это сделать с Eclipse, пожалуйста, предложите мне. –

+0

Я понял. Можете ли вы показать мне, как редактировать мой код для файла. Поскольку после вашего предложения я мог читать только одну строку из файла. Где мне нужно читать полный файл. Вы видите мой пример ввода. –

+0

Вы там? вы можете помочь? –

0

Кажется, что объект Tree имеет значение null. Однако, почему это происходит, требуется более глубокое знание того, что вы делаете.

Просто предположим, но кажется, что указанный вами класс не может быть в предложении ... Так, например, то, что вы называете sentence.get, в предложении нет SentimentAnnotatedTree.class, и поэтому метод возвращает ноль.

 Смежные вопросы

  • Нет связанных вопросов^_^