情绪评分斯坦福核心NLP

时间:2014-03-16 07:14:26

标签: stanford-nlp sentiment-analysis

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

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

3 个答案:

答案 0 :(得分:5)

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

代码如下所示:

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

答案 1 :(得分:3)

给定句子,一旦可以对注释后的预测类进行平均。 可以访问预测的类,将带注释的树类传递给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;
    }
}

答案 2 :(得分:0)

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

  1. 使用SentimentCoreAnnotations.SentimentClass.class获取情绪 类型。
  2. 使用RNNCoreAnnotations.getPredictedClass获取情绪 评分
  3. 情绪分析器

    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