斯坦福情绪分析得分 java
Stanford sentiment analysis score java
我使用 Stanford core NLP 库进行情绪分析。下面的代码 return class 一个例子但是我怎样才能得到分数呢?例如 -0.3 表示负等
private int getScore(String line) {
boolean isrun = false;
StanfordCoreNLP pipeline = null;
if(!isrun){
Properties props = getProperties();
pipeline = new StanfordCoreNLP(props);
isrun = true;
}
Annotation annotation;
int sentiment = -1;
if (line != null && line.length() > 0) {
annotation = pipeline.process(line);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
sentiment = RNNCoreAnnotations.getPredictedClass(tree);
}
}
return sentiment;
}
编辑
在线demo 当鼠标在图表的根目录时,我们可以看到该示例为负 72%。如何获得这个号码?
0.Download 斯坦福 NLP 核心库并导入外部库 stanford-corenlp-3.5.2-models.jar、stanford-corenlp-3.5.2.jar、stanford-corenlp-3.5。 2-sources.jar 和 ejml-0.23.jar 到这个包中。
1.Build 这个 class Eclipse 中的 NLP
import java.util.Properties;
import org.ejml.simple.SimpleMatrix;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations.SentimentAnnotatedTree;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;
public class NLP {
static StanfordCoreNLP pipeline;
public static void init() {
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
pipeline = new StanfordCoreNLP(props);
}
public static int findSentiment(String tweet) {
int mainSentiment = 0;
if (tweet != null && tweet.length() > 0) {
int longest = 0;
Annotation annotation = pipeline.process(tweet);
for (CoreMap sentence : annotation
.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence
.get(SentimentAnnotatedTree.class);
int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
SimpleMatrix sentiment_new = RNNCoreAnnotations.getPredictions(tree);
String partText = sentence.toString();
if (partText.length() > longest) {
mainSentiment = sentiment;
longest = partText.length();
}
}
}
return mainSentiment;
}
}
2.Build 一个新的 class 来用 NLP
解析你的句子
import java.util.ArrayList;
public class What2Think {
public static void main(String[] args) {
ArrayList<String> tweets = new ArrayList<String>();
tweets.add("In this country, \"democracy\" means pro-government. #irony");
NLP.init();
for(String tweet : tweets) {
System.out.println(tweet + " : " + NLP.findSentiment(tweet));
}
}
}
运行它
我也有类似的需求。您可以从 SimpleMatrix
获取此信息
SimpleMatrix sm = RNNCoreAnnotations.getPredictions(tree);
如果你打印变量 sm 输出有这样的东西
Type = dense , numRows = 5 , numCols = 1
0.111
0.758
0.087
0.035
0.009
这给出了估计的概率。在在线演示中,您可以在 %.
中看到这些值
你可以找到我的实现 here。
希望对您有所帮助!!!
我使用 Stanford core NLP 库进行情绪分析。下面的代码 return class 一个例子但是我怎样才能得到分数呢?例如 -0.3 表示负等
private int getScore(String line) {
boolean isrun = false;
StanfordCoreNLP pipeline = null;
if(!isrun){
Properties props = getProperties();
pipeline = new StanfordCoreNLP(props);
isrun = true;
}
Annotation annotation;
int sentiment = -1;
if (line != null && line.length() > 0) {
annotation = pipeline.process(line);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
sentiment = RNNCoreAnnotations.getPredictedClass(tree);
}
}
return sentiment;
}
编辑
在线demo 当鼠标在图表的根目录时,我们可以看到该示例为负 72%。如何获得这个号码?
0.Download 斯坦福 NLP 核心库并导入外部库 stanford-corenlp-3.5.2-models.jar、stanford-corenlp-3.5.2.jar、stanford-corenlp-3.5。 2-sources.jar 和 ejml-0.23.jar 到这个包中。
1.Build 这个 class Eclipse 中的 NLP
import java.util.Properties;
import org.ejml.simple.SimpleMatrix;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.neural.rnn.RNNCoreAnnotations;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations;
import edu.stanford.nlp.sentiment.SentimentCoreAnnotations.SentimentAnnotatedTree;
import edu.stanford.nlp.trees.Tree;
import edu.stanford.nlp.util.CoreMap;
public class NLP {
static StanfordCoreNLP pipeline;
public static void init() {
Properties props = new Properties();
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
pipeline = new StanfordCoreNLP(props);
}
public static int findSentiment(String tweet) {
int mainSentiment = 0;
if (tweet != null && tweet.length() > 0) {
int longest = 0;
Annotation annotation = pipeline.process(tweet);
for (CoreMap sentence : annotation
.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence
.get(SentimentAnnotatedTree.class);
int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
SimpleMatrix sentiment_new = RNNCoreAnnotations.getPredictions(tree);
String partText = sentence.toString();
if (partText.length() > longest) {
mainSentiment = sentiment;
longest = partText.length();
}
}
}
return mainSentiment;
}
}
2.Build 一个新的 class 来用 NLP
解析你的句子import java.util.ArrayList;
public class What2Think {
public static void main(String[] args) {
ArrayList<String> tweets = new ArrayList<String>();
tweets.add("In this country, \"democracy\" means pro-government. #irony");
NLP.init();
for(String tweet : tweets) {
System.out.println(tweet + " : " + NLP.findSentiment(tweet));
}
}
}
运行它
我也有类似的需求。您可以从 SimpleMatrix
获取此信息 SimpleMatrix sm = RNNCoreAnnotations.getPredictions(tree);
如果你打印变量 sm 输出有这样的东西
Type = dense , numRows = 5 , numCols = 1
0.111
0.758
0.087
0.035
0.009
这给出了估计的概率。在在线演示中,您可以在 %.
中看到这些值你可以找到我的实现 here。
希望对您有所帮助!!!