Я пытаюсь запустить этот код, используя входной файл и из положить в другой файл: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)
Теперь я не понимая, ш Это происходит? Что мне следует сделать, чтобы успешно запустить этот код?
Смотрите это командная строка. Я уже сказал, что использую Eclipse Neon. Я знаю, как это сделать с Eclipse, пожалуйста, предложите мне. –
Я понял. Можете ли вы показать мне, как редактировать мой код для файла. Поскольку после вашего предложения я мог читать только одну строку из файла. Где мне нужно читать полный файл. Вы видите мой пример ввода. –
Вы там? вы можете помочь? –