关于StanfordCoreNLP在情绪分析中的使用

时间:2015-12-13 23:24:28

标签: nlp stanford-nlp

这是关于StanfordCoreNLP用于情绪分析的问题。我不确定,基于我的探索" SentimentCoreAnnotations.AnnotatedTree.class"已更改为" SentimentCoreAnnotations.SentimentAnnotatedTree.class"。因为我得到" SentimentCoreAnnotations.AnnotatedTree无法解析为类型"。但是当我改为" SentimentCoreAnnotations.SentimentAnnotatedTree.class"我得到NULL。有人可以澄清一下吗?谢谢! 我正在使用以下互联网代码。我发现大多数实现都是类似的。在我尝试过的几乎所有实现中都遇到了同样的问题。

package crawler;
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;
import java.util.List;
import java.util.Properties;
import edu.stanford.*;


public class NLP {  
    public static void main(String[] args) {
        findSentiment("life is good.");
    }

    public static void findSentiment(String line) {

        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) {
            System.out.println("line:"+line);
            int longest = 0;
            Annotation annotation = pipeline.process(line);
            for (CoreMap sentence : annotation
                    .get(CoreAnnotations.SentencesAnnotation.class)) {
                Tree tree = sentence
                        .get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
                int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
                String partText = sentence.toString();
                if (partText.length() > longest) {
                    mainSentiment = sentiment;
                    longest = partText.length();
                }

            }
        }
        if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) {
            System.out.println("Neutral " + line);
        }
        else{
        }
        /*
         * TweetWithSentiment tweetWithSentiment = new TweetWithSentiment(line,
         * toCss(mainSentiment)); return tweetWithSentiment;
         */

    }
}   

1 个答案:

答案 0 :(得分:0)

  1. 从这里下载Stanford CoreNLP 3.6.0:http://stanfordnlp.github.io/CoreNLP/

  2. 访问情绪的一些示例代码:

    import edu.stanford.nlp.hcoref.CorefCoreAnnotations;
    import edu.stanford.nlp.hcoref.data.CorefChain;
    import edu.stanford.nlp.hcoref.data.Mention;
    import edu.stanford.nlp.ling.CoreAnnotations;
    import edu.stanford.nlp.pipeline.Annotation;
    import edu.stanford.nlp.sentiment.SentimentCoreAnnotations.*;
    import edu.stanford.nlp.pipeline.StanfordCoreNLP;
    import edu.stanford.nlp.util.CoreMap;
    
    import java.util.Properties;
    
    public class SentimentExample {
    
        public static void main(String[] args) throws Exception {
    
            Annotation document = new Annotation("The movie was great!");
            Properties props = new Properties();
            props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
            pipeline.annotate(document);
            for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
                System.out.println("---");
                System.out.println(sentence);
                System.out.println(sentence.get(SentimentAnnotatedTree.class));
                System.out.println(sentence.get(SentimentClass.class));
    
            }
        }
    }