gpt4 book ai didi

java - 斯坦福 NLP 在运行代码时给出异常

转载 作者:行者123 更新时间:2023-12-01 08:51:19 25 4
gpt4 key购买 nike

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

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)

现在我不明白为什么会发生这种情况?我应该怎么做才能成功运行此代码?

最佳答案

您没有在管道中运行情感注释器或解析器。这是一个命令行调用,显示运行管道并获取情绪。通过设置管道的属性以匹配此调用指定的属性,您可以轻松地使其适应 Java 代码。

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

您需要将 parsesentiment 注释器添加到管道中,并且需要确保 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));
}
}
}

关于java - 斯坦福 NLP 在运行代码时给出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42385519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com