使用 stanford core nlp 时出错
Error while using stanford core nlp
我在使用 stanford nlp 时遇到问题。我在尝试使用 Stanford Core NLP 时遇到各种错误 tools.I 想知道通过的句子的情绪。但是当 运行 来自 eclipse 的代码和需要的 jar 添加到类路径时,我无法让 nlp 工具工作,
这是我要执行的代码。
import java.util.Properties;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.SentimentAnnotator;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.TypesafeMap.Key;
public class sentiment_demo {
public static void sentiment_analysis(String line)
{
//Uses Stanford NLP sentimnet analysis
//found in latest model released from stanford
// ver 3.3.1
//applies sentiment analysis to text
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
int mainSentiment = 0;
if (line != null && line.length() > 0) {
int longest = 0;
Annotation annotation = pipeline.process(line);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
System.out.println(sentence);
for (Tree token: sentence.get(SentimentCoreAnnotations.AnnotatedTree.class))
{
//System.out.println(token);
}
Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
System.out.println(sentiment);
String partText = sentence.toString();
//System.out.println(partText);
if (partText.length() > longest) {
mainSentiment = sentiment;
longest = partText.length();
}
}
}
if(mainSentiment==2)
{
System.out.println("Average");
}
else if(mainSentiment>2)
{
System.out.println("Positive");
}
else if(mainSentiment<2)
{
System.out.println("Negative ");
}
if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) {
//return null;
}
}
public static void main(String[] args)
{
sentiment_analysis("Cristiano Ronaldo, is a Portuguese professional footballer who plays for Spanish club Real Madrid and the Portugal national team. He is a forward and serves as captain for Portugal.Often ranked as the best player in the world and rated by some in the sport as the greatest of all time");
}
}
这是我设置的库
我正在使用 eclipse mars。起初它显示 ejml library.but 错误然后我导入了 ejml jar 文件以便解决错误但它现在引起了这个错误...
Adding annotator tokenize
Adding annotator ssplit
Adding annotator parse
Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... done [1.6 sec].
Adding annotator sentiment
Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException: java.lang.ClassNotFoundException: edu.stanford.nlp.neural.SimpleTensor
at edu.stanford.nlp.sentiment.SentimentModel.loadSerialized(SentimentModel.java:470)
at edu.stanford.nlp.pipeline.SentimentAnnotator.<init>(SentimentAnnotator.java:45)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.create(StanfordCoreNLP.java:845)
at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:81)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:260)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:127)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:123)
at sentiment_demo.sentiment_analysis(sentiment_demo.java:28)
at sentiment_demo.main(sentiment_demo.java:70)
Caused by: java.lang.ClassNotFoundException: edu.stanford.nlp.neural.SimpleTensor
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.TreeMap.buildFromSorted(Unknown Source)
at java.util.TreeMap.buildFromSorted(Unknown Source)
at java.util.TreeMap.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.TreeMap.buildFromSorted(Unknown Source)
at java.util.TreeMap.buildFromSorted(Unknown Source)
at java.util.TreeMap.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at edu.stanford.nlp.io.IOUtils.readObjectFromURLOrClasspathOrFileSystem(IOUtils.java:298)
at edu.stanford.nlp.sentiment.SentimentModel.loadSerialized(SentimentModel.java:466)
我卡在 it.If 有人对此有任何想法然后请提出建议,这将是很棒的 help.I 在 Whosebug 上搜索了其他类似的问题但仍然没有找到解决这个问题的方法.
您似乎在使用旧版本的 CoreNLP (3.3.0) 和新模型。尝试下载 3.6.0 代码 + 模型。
实际上问题出在不同版本 used.some 的 jar 文件,例如 xom、ejml,其中 missing.I 通过从 stanford core NLP 站点下载完整的 jar 文件来修复它,版本为 3.6.0 .其中一个导入语句更改为
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
SentimentCoreAnnotations.AnnotatedTree.class需要改成
SentimentCoreAnnotations.SentimentAnnotatedTree.class
此更改解决了我的所有错误。
我在使用 stanford nlp 时遇到问题。我在尝试使用 Stanford Core NLP 时遇到各种错误 tools.I 想知道通过的句子的情绪。但是当 运行 来自 eclipse 的代码和需要的 jar 添加到类路径时,我无法让 nlp 工具工作,
这是我要执行的代码。
import java.util.Properties;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.ling.CoreAnnotations.TokensAnnotation;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.SentimentAnnotator;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;
import edu.stanford.nlp.util.TypesafeMap.Key;
public class sentiment_demo {
public static void sentiment_analysis(String line)
{
//Uses Stanford NLP sentimnet analysis
//found in latest model released from stanford
// ver 3.3.1
//applies sentiment analysis to text
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
int mainSentiment = 0;
if (line != null && line.length() > 0) {
int longest = 0;
Annotation annotation = pipeline.process(line);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
System.out.println(sentence);
for (Tree token: sentence.get(SentimentCoreAnnotations.AnnotatedTree.class))
{
//System.out.println(token);
}
Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
System.out.println(sentiment);
String partText = sentence.toString();
//System.out.println(partText);
if (partText.length() > longest) {
mainSentiment = sentiment;
longest = partText.length();
}
}
}
if(mainSentiment==2)
{
System.out.println("Average");
}
else if(mainSentiment>2)
{
System.out.println("Positive");
}
else if(mainSentiment<2)
{
System.out.println("Negative ");
}
if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) {
//return null;
}
}
public static void main(String[] args)
{
sentiment_analysis("Cristiano Ronaldo, is a Portuguese professional footballer who plays for Spanish club Real Madrid and the Portugal national team. He is a forward and serves as captain for Portugal.Often ranked as the best player in the world and rated by some in the sport as the greatest of all time");
}
}
这是我设置的库
我正在使用 eclipse mars。起初它显示 ejml library.but 错误然后我导入了 ejml jar 文件以便解决错误但它现在引起了这个错误...
Adding annotator tokenize
Adding annotator ssplit
Adding annotator parse
Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... done [1.6 sec].
Adding annotator sentiment
Exception in thread "main" edu.stanford.nlp.io.RuntimeIOException: java.lang.ClassNotFoundException: edu.stanford.nlp.neural.SimpleTensor
at edu.stanford.nlp.sentiment.SentimentModel.loadSerialized(SentimentModel.java:470)
at edu.stanford.nlp.pipeline.SentimentAnnotator.<init>(SentimentAnnotator.java:45)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.create(StanfordCoreNLP.java:845)
at edu.stanford.nlp.pipeline.AnnotatorPool.get(AnnotatorPool.java:81)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.construct(StanfordCoreNLP.java:260)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:127)
at edu.stanford.nlp.pipeline.StanfordCoreNLP.<init>(StanfordCoreNLP.java:123)
at sentiment_demo.sentiment_analysis(sentiment_demo.java:28)
at sentiment_demo.main(sentiment_demo.java:70)
Caused by: java.lang.ClassNotFoundException: edu.stanford.nlp.neural.SimpleTensor
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at java.io.ObjectInputStream.resolveClass(Unknown Source)
at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
at java.io.ObjectInputStream.readClassDesc(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.TreeMap.buildFromSorted(Unknown Source)
at java.util.TreeMap.buildFromSorted(Unknown Source)
at java.util.TreeMap.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at java.util.TreeMap.buildFromSorted(Unknown Source)
at java.util.TreeMap.buildFromSorted(Unknown Source)
at java.util.TreeMap.readObject(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
at java.io.ObjectInputStream.readSerialData(Unknown Source)
at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
at java.io.ObjectInputStream.readObject0(Unknown Source)
at java.io.ObjectInputStream.readObject(Unknown Source)
at edu.stanford.nlp.io.IOUtils.readObjectFromURLOrClasspathOrFileSystem(IOUtils.java:298)
at edu.stanford.nlp.sentiment.SentimentModel.loadSerialized(SentimentModel.java:466)
我卡在 it.If 有人对此有任何想法然后请提出建议,这将是很棒的 help.I 在 Whosebug 上搜索了其他类似的问题但仍然没有找到解决这个问题的方法.
您似乎在使用旧版本的 CoreNLP (3.3.0) 和新模型。尝试下载 3.6.0 代码 + 模型。
实际上问题出在不同版本 used.some 的 jar 文件,例如 xom、ejml,其中 missing.I 通过从 stanford core NLP 站点下载完整的 jar 文件来修复它,版本为 3.6.0 .其中一个导入语句更改为
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
SentimentCoreAnnotations.AnnotatedTree.class需要改成 SentimentCoreAnnotations.SentimentAnnotatedTree.class
此更改解决了我的所有错误。