【问题标题】:Sentiments Scores Stanford Core NLP情绪得分斯坦福核心 NLP
【发布时间】:2014-03-16 07:14:25
【问题描述】:

我们如何使用斯坦福核心 NLP 获得完整句子的情感得分?

它将完整的句子分为正面和负面情绪,但是我们可以从斯坦福 NLP 工具中获得总情绪分数吗?

【问题讨论】:

  • docs 看起来很全面,而且它有教程,你为什么不试试,让我们知道你遇到了什么问题?

标签: java stanford-nlp sentiment-analysis


【解决方案1】:

我所做的是根据句子长度平均每个句子的分数。其背后的逻辑是,较长的句子应该比较短的句子更有分量。

代码如下所示:

String line = "Great item! HDMI and decent wifi required as with all streaming devices.\n" +
            "The flow on the homepage is very good and responsive. Watching a series is a doddle, flow is great, no action required.\n" +
            "The remote and controller app both work a treat.\n" +
            "I really like this device.\n" +
            "I'd like to see an Amazon-written mirroring app available for non-Amazon products but no-one likes talking to each other in this field!";

    Long textLength = 0L;
    int sumOfValues = 0;

    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)) {
            Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
            int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
            String partText = sentence.toString();
            if (partText.length() > longest) {
                textLength += partText.length();
                sumOfValues = sumOfValues + sentiment * partText.length();

                System.out.println(sentiment + " " + partText);
            }
        }
    }

    System.out.println("Overall: " + (double)sumOfValues/textLength);

下载整个项目here

【讨论】:

    【解决方案2】:

    给定句子,once 可以平均标注后的预测类别。 可以通过将带注释的树类传递给RNNCoreAnnotations.getPredictedClass 来访问预测的类:

    public class SentimentAnalysis {
        private final String document;
    
        public SentimentAnalysis(String document) {
            this.document = document;
        }
    
        public SentimentAnalysis(Collection<String> sentences) {
            this.document = Joiner.on(". ").join(sentences);
        }
    
        public List<Integer> annotateAndScore(){
            List<Integer> scores = Lists.newArrayList();
            Properties props = new Properties();
            props.put("annotators", "tokenize, ssplit, pos, parse, sentiment");
            StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
            Annotation document = new Annotation(this.document);
            pipeline.annotate(document);
            for (CoreMap sentence : document.get(CoreAnnotations.SentencesAnnotation.class)) {
                Tree annotatedTree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
                int score = RNNCoreAnnotations.getPredictedClass(annotatedTree);
                scores.add(score);
            }
            return scores;
        }
    }
    

    【讨论】:

      【解决方案3】:

      我正在从我的blog 获取情绪分析代码: 代码很简单:

      1. 使用 SentimentCoreAnnotations.SentimentClass.class 获取情绪 类型。
      2. 使用 RNNCoreAnnotations.getPredictedClass 获取情绪 得分

      情绪分析器

      package com.interviewBubble.sentimentanalysis;
      
      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.trees.Tree;
      
      import edu.stanford.nlp.util.CoreMap;
      
      public class SentimentAnalyzer {
      
       StanfordCoreNLP pipeline;
      
       public void initialize() {
      
        Properties properties = new Properties();
      
        properties.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
      
        pipeline = new StanfordCoreNLP(properties);
      
       }
      
       public SentimentResult getSentimentResult(String text) {
      
        SentimentClassification classification = new SentimentClassification();
      
        SentimentResult sentimentResult = new SentimentResult();
      
        if (text != null && text.length() > 0) {
      
         Annotation annotation = pipeline.process(text);
      
         for(CoreMap sentence: annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
      
             // System.out.println(sentence);
      
         Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
      
         //System.out.println(tree);
      
         SimpleMatrix simpleMatrix = RNNCoreAnnotations.getPredictions(tree);
      
         //System.out.println(simpleMatrix);
      
         classification.setVeryNegative((int)Math.round(simpleMatrix.get(0)*100d));
      
         classification.setNegative((int)Math.round(simpleMatrix.get(1)*100d));
      
         classification.setNeutral((int)Math.round(simpleMatrix.get(2)*100d));
      
         classification.setPositive((int)Math.round(simpleMatrix.get(3)*100d));
      
         classification.setVeryPositive((int)Math.round(simpleMatrix.get(4)*100d));
      
         String setimentType = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
      
         sentimentResult.setSentimentType(setimentType);
      
         sentimentResult.setSentimentClass(classification);
      
         sentimentResult.setSentimentScore(RNNCoreAnnotations.getPredictedClass(tree));
      
         }
      
        }
      
        return sentimentResult;
      
       }
      
      }
      

      情绪分析:

      package com.interviewBubble.sentimentanalysis;
      
      public class SentimentAnalysis {
      
       public static void main(String[] args) {
      
           // I have taken iPhone X (Silver) User Review from Amazon
      
           String text = "Just love the X. Feel so Premium and a Head turner too. Face ID working fine but still miss "
      
             + "the fingerprint scanner very much. I jump from 5S to X so it’s a huge skip. I’m very very happy"
      
             + " with it. Specially battery backup is great after using with 4g cellular network and no heating "
      
             + "issue at all, though I’m not a mobile gamer, Oftentimes I play Boom Beach and I watch YouTube "
      
             + "videos and I surf a lot. It makes a deep hole in pocket at the Hefty price tag. So it’s all "
      
             + "upto your Consideration.\n";
      
           SentimentAnalyzer sentimentAnalyzer = new SentimentAnalyzer();
      
           sentimentAnalyzer.initialize();
      
           SentimentResult sentimentResult = sentimentAnalyzer.getSentimentResult(text);
      
      
      
           System.out.println("Sentiments Classification:");
      
        System.out.println("Very positive: " + sentimentResult.getSentimentClass().getVeryPositive()+"%");
      
        System.out.println("Positive: " + sentimentResult.getSentimentClass().getPositive()+"%");
      
        System.out.println("Neutral: " + sentimentResult.getSentimentClass().getNeutral()+"%");
      
        System.out.println("Negative: " + sentimentResult.getSentimentClass().getNegative()+"%");
      
        System.out.println("Very negative: " + sentimentResult.getSentimentClass().getVeryNegative()+"%");
      
           System.out.println("\nSentiments result:");
      
           System.out.println("Sentiment Score: " + sentimentResult.getSentimentScore());
      
        System.out.println("Sentiment Type: " + sentimentResult.getSentimentType());
      
       }
      
      }
      

      输出:

      0    [main] INFO  edu.stanford.nlp.pipeline.StanfordCoreNLP  - Adding annotator tokenize
      
      8    [main] INFO  edu.stanford.nlp.pipeline.TokenizerAnnotator  - No tokenizer type provided. Defaulting to PTBTokenizer.
      
      12   [main] INFO  edu.stanford.nlp.pipeline.StanfordCoreNLP  - Adding annotator ssplit
      
      17   [main] INFO  edu.stanford.nlp.pipeline.StanfordCoreNLP  - Adding annotator parse
      
      512  [main] INFO  edu.stanford.nlp.parser.common.ParserGrammar  - Loading parser from serialized file edu/stanford/nlp/models/lexparser/englishPCFG.ser.gz ... done [0.5 sec].
      
      517  [main] INFO  edu.stanford.nlp.pipeline.StanfordCoreNLP  - Adding annotator sentiment
      
      Sentiments Classification:
      
      Very positive: 4%
      
      Positive: 23%
      
      Neutral: 24%
      
      Negative: 39%
      
      Very negative: 8%
      
      Sentiments result:
      
      Sentiment Score: 1
      
      Sentiment Type: Negative
      

      查找整个项目here

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-06-20
        • 1970-01-01
        • 2012-08-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-04-30
        相关资源
        最近更新 更多