Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SentimentCoreAnnotations returns null #39

Closed
warpech opened this issue Jan 3, 2016 · 4 comments
Closed

SentimentCoreAnnotations returns null #39

warpech opened this issue Jan 3, 2016 · 4 comments

Comments

@warpech
Copy link

warpech commented Jan 3, 2016

Hi! I have managed to run the examples on the website, but I am missing an example for sentiment analysis. I have found an example here: http://stackoverflow.com/questions/31832387/using-stanford-nlp-lib-in-c-sharp-while-trying-to-get-sentiment-positive-nega, but it doesn't work for me.

After adapting it to a C# class, I ended up with code that compiles:

using System;

namespace PureCS {
    class Program {
        static void Main() {
            var sentimentHelper = new SentimentHelper();
            var sentiment = sentimentHelper.Analyze("This is freaking awesome!");
        }
    }
}
using edu.stanford.nlp.ling;
using edu.stanford.nlp.neural.rnn;
using edu.stanford.nlp.pipeline;
using edu.stanford.nlp.sentiment;
using edu.stanford.nlp.trees;
using edu.stanford.nlp.util;
using java.io;
using java.text;
using java.util;
using System;
using System.IO;

namespace PureCS {
    public class StanfordNLPModelNotFoundExpection : Exception {
    }

    public class SentimentHelper {
        private string jarRoot;
        public string LastError;

        public SentimentHelper() {
            //JarRoot = @"..\..\..\..\paket-files\nlp.stanford.edu\stanford-corenlp-full-2015-12-09\models";
            jarRoot = "C:\\www\\warpech\\Crystal\\stanford-corenlp-full-2015-12-09\\stanford-corenlp-3.6.0-models";

            if (Directory.Exists(jarRoot) == false) {
                LastError = typeof(StanfordNLPModelNotFoundExpection).Name;
            }
        }

        public int Analyze(string text) {
            var result = "";

            // Annotation pipeline configuration
            var props = new java.util.Properties();

            props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
            //props.setProperty("sutime.binders", "0");
            //props.setProperty("ner.useSUTime", "false");
            props.setProperty("ner.useSUTime", "0");

            // We should change current directory, so D:\Core NLP Files\stanford-corenlp-full-2015-04-20\stanford-corenlp-full-2015-04-20 could find all the model files automatically
            var curDir = Environment.CurrentDirectory;
            Directory.SetCurrentDirectory(jarRoot);
            var pipeline = new StanfordCoreNLP(props);
            Directory.SetCurrentDirectory(curDir);

            // Annotation
            var annotation = new edu.stanford.nlp.pipeline.Annotation(text);
            pipeline.annotate(annotation);

            // Result - Pretty Print
            using (var stream = new ByteArrayOutputStream()) {
                pipeline.prettyPrint(annotation, new PrintWriter(stream));

                //Analyze the statement as positive or negative
                int mainSentiment = 0;
                int longest = 0;
                String[] sentimentText = { "Very Negative", "Negative", "Neutral", "Positive", "Very Positive" };

                NumberFormat NF = new DecimalFormat("0.0000");

                var sentences = annotation.get(new CoreAnnotations.SentencesAnnotation().getClass()) as ArrayList;

                foreach (CoreMap sentence in sentences) {
                    Tree tree = sentence.get(new SentimentCoreAnnotations.SentimentAnnotatedTree().getClass()) as Tree;

                    int sentiment = RNNCoreAnnotations.getPredictedClass(tree);

                    String partText = sentence.ToString();
                    result += "Sentence: '" + partText + "' is rather " + sentimentText[sentiment] + ". ";

                    if (partText.Length > longest) {
                        mainSentiment = sentiment;
                        longest = partText.Length;
                    }
                }

                if (mainSentiment == 2 || mainSentiment > 4 || mainSentiment < 0) {
                    result += "Overall it was sort of neutral review";
                }
                else if (mainSentiment > 2) {
                    result += "Overall we are happy";
                }
                else {
                    result += "Bottom line. We are displeased";
                }

                stream.close();
                return mainSentiment;
            }

        }
    }
}

The problem that I have is that this line returns null:

Tree tree = sentence.get(new SentimentCoreAnnotations.SentimentAnnotatedTree().getClass()) as Tree;
@warpech
Copy link
Author

warpech commented Jan 3, 2016

Screenshot: image

@warpech
Copy link
Author

warpech commented Jan 3, 2016

Leave me alone and I will figure it out :) The problem was that I didn't load the sentiment annotator.

Instead of:

props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");

Should be:

props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");

@warpech warpech closed this as completed Jan 3, 2016
@sergey-tihon
Copy link
Owner

Cool, this demo may be also interesting to you https://github.com/evelinag/SentimentAnalysisDemo

@warpech
Copy link
Author

warpech commented Jan 3, 2016

Thanks! It's really helpful

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants