[英]Stanford NLP sentiment score returns -1 always in c#
I am trying to implement Stanford NLP sentiment analysis code in c# using code borrowed from this site and on the main Stanford website. 我正在尝试使用从本网站和主要斯坦福网站上借来的代码在C#中实现斯坦福NLP情感分析代码。 The following code works but Score is always -1.
以下代码有效,但总分始终为-1。 Score should be between 0 to 4. Any help?
分数应介于0到4之间。有帮助吗?
// We should change current directory, so StanfordCoreNLP could find all the model files automatically
var curDir = Environment.CurrentDirectory;
Directory.SetCurrentDirectory(jarRoot);
var pipeline = new StanfordCoreNLP(props);
Directory.SetCurrentDirectory(curDir);
foreach(String text in texts) {
// create an empty Annotation just with the given text
Annotation document = new Annotation(text);
// run all Annotators on this text
pipeline.annotate(document);
// these are all the sentences in this document
// a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
var sentences = document.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList;
String[] sentimentText = { "Very Negative","Negative", "Neutral", "Positive", "Very Positive"};
foreach(CoreMap sentence in sentences) {
Tree tree = (Tree)sentence.get(typeof(edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation));
int score = RNNCoreAnnotations.getPredictedClass(tree);
Console.WriteLine(sentimentText[score]); // prints sentiment for each sentence in the doc
}
Found the solution: 找到了解决方案:
change: 更改:
Tree tree = (Tree)sentence.get(typeof(edu.stanford.nlp.trees.TreeCoreAnnotations.TreeAnnotation));
to: 至:
Tree tree = (Tree)sentence.get(typeof(edu.stanford.nlp.sentiment.SentimentCoreAnnotations.SentimentAnnotatedTree));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.