Example usage for edu.stanford.nlp.neural.rnn RNNCoreAnnotations getPredictions

List of usage examples for edu.stanford.nlp.neural.rnn RNNCoreAnnotations getPredictions

Introduction

In this page you can find the example usage for edu.stanford.nlp.neural.rnn RNNCoreAnnotations getPredictions.

Prototype

public static SimpleMatrix getPredictions(Tree tree) 

Source Link

Usage

From source file:at.ac.tuwien.inso.subcat.utility.sentiment.SentimentAnalyser.java

License:Open Source License

public SentimentBlock get(String str) {
    if (pipeline == null) {
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
        pipeline = new StanfordCoreNLP(props);
    }/*from  w  ww .  j  av a  2s  .com*/

    LinkedList<SentenceSentiment> sentiments = new LinkedList<SentenceSentiment>();
    int[] classes = new int[5];

    double positiveSum = 0;
    double somewhatPositiveSum = 0;
    double neutralSum = 0;
    double somewhatNegativeSum = 0;
    double negativeSum = 0;

    double positiveWSum = 0;
    double somewhatPositiveWSum = 0;
    double neutralWSum = 0;
    double somewhatNegativeWSum = 0;
    double negativeWSum = 0;

    int words = 0;

    Annotation annotation = pipeline.process(str);
    List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);
    for (CoreMap sentence : sentences) {
        Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
        int sentenceWordCount = tree.getLeaves().size();

        // TODO: calculate it instead
        int predictedClass = RNNCoreAnnotations.getPredictedClass(tree);
        SimpleMatrix matrix = RNNCoreAnnotations.getPredictions(tree);

        classes[predictedClass]++;

        SentenceSentiment sentiment = new SentenceSentiment(matrix.get(0, 0), matrix.get(1, 0),
                matrix.get(2, 0), matrix.get(3, 0), matrix.get(4, 0), predictedClass, sentenceWordCount);

        positiveSum += sentiment.getPositive();
        somewhatPositiveSum += sentiment.getSomewhatPositive();
        neutralSum += sentiment.getNeutral();
        somewhatNegativeSum += sentiment.getSomewhatNegative();
        negativeSum += sentiment.getNegative();

        positiveWSum += sentiment.getPositive() * sentenceWordCount;
        somewhatPositiveWSum += sentiment.getSomewhatPositive() * sentenceWordCount;
        neutralWSum += sentiment.getNeutral() * sentenceWordCount;
        somewhatNegativeWSum += sentiment.getSomewhatNegative() * sentenceWordCount;
        negativeWSum += sentiment.getNegative() * sentenceWordCount;

        words += sentenceWordCount;

        sentiments.add(sentiment);
    }

    double positiveMean = 0;
    double somewhatPositiveMean = 0;
    double neutralMean = 0;
    double somewhatNegativeMean = 0;
    double negativeMean = 0;

    double positiveWMean = 0;
    double somewhatPositiveWMean = 0;
    double neutralWMean = 0;
    double somewhatNegativeWMean = 0;
    double negativeWMean = 0;

    if (sentiments.size() > 0) {
        positiveMean = positiveSum / sentiments.size();
        somewhatPositiveMean = somewhatPositiveSum / sentiments.size();
        neutralMean = neutralSum / sentiments.size();
        somewhatNegativeMean = somewhatNegativeSum / sentiments.size();
        negativeMean = negativeSum / sentiments.size();
    }

    if (words > 0) {
        positiveWMean = positiveWSum / words;
        somewhatPositiveWMean = somewhatPositiveWSum / words;
        neutralWMean = neutralWSum / words;
        somewhatNegativeWMean = somewhatNegativeWSum / words;
        negativeWMean = negativeWSum / words;
    }

    //System.out.println ("n:" + positiveMean  + "," +  somewhatPositiveMean  + "," + neutralMean + "," + somewhatNegativeMean + "," + negativeMean);
    //System.out.println ("w:" + positiveWMean  + "," +  somewhatPositiveWMean  + "," + neutralWMean + "," + somewhatNegativeWMean + "," + negativeWMean);

    SentimentBlock block = new SentimentBlock(sentiments, classes, positiveMean, somewhatPositiveMean,
            neutralMean, somewhatNegativeMean, negativeMean, positiveWMean, somewhatPositiveWMean, neutralWMean,
            somewhatNegativeWMean, negativeWMean, words);

    return block;
}

From source file:bi.meteorite.sentiment.NLPStep.java

License:Apache License

private String processString(String document) {
    // shut off the annoying intialization messages
    Properties props = new Properties();
    //specify the annotators that we want to use to annotate the text.  We need a tokenized sentence with POS tags to extract sentiment.
    //this forms our pipeline
    props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    Annotation annotation = pipeline.process(document);
    List<Sentence> sentences = new ArrayList<Sentence>();
    /*//from w  w  w.j  a  va 2 s  . c  o m
     * We're going to iterate over all of the sentences and extract the sentiment.  We'll adopt a majority rule policy
     */
    for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
        //for each sentence, we get the sentiment that CoreNLP thinks this sentence indicates.
        Tree sentimentTree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
        int sentimentClassIdx = RNNCoreAnnotations.getPredictedClass(sentimentTree);
        SentimentClass sentimentClass = SentimentClass.getSpecific(sentimentClassIdx);

        /*
         * Each possible sentiment has an associated probability, so let's pull the entire
         * set of probabilities across all sentiment classes.
         */
        double[] probs = new double[SentimentClass.values().length];
        {
            SimpleMatrix mat = RNNCoreAnnotations.getPredictions(sentimentTree);
            for (int i = 0; i < SentimentClass.values().length; ++i) {
                probs[i] = mat.get(i);
            }
        }
        /*
         * Add the sentence and the associated probabilities to our list.
         */
        String sentenceStr = AnnotationUtils.sentenceToString(sentence).replace("\n", "");
        sentences.add(new Sentence(probs, sentenceStr, sentimentClass));
    }
    SentimentClass sentimentClass = null;
    if (meta.getAnalysisType().equals("Wilson Score")) {
        sentimentClass = SentimentRollup.WILSON_SCORE.apply(sentences);
    } else if (meta.getAnalysisType().equals("Simple Vote Rollup")) {
        sentimentClass = SentimentRollup.SIMPLE_VOTE.apply(sentences);
    } else if (meta.getAnalysisType().equals("Longest Sentence Wins")) {
        sentimentClass = SentimentRollup.LONGEST_SENTENCE_WINS.apply(sentences);
    } else if (meta.getAnalysisType().equals("Last Sentence Wins")) {
        sentimentClass = SentimentRollup.LAST_SENTENCE_WINS.apply(sentences);
    } else if (meta.getAnalysisType().equals("Average Probabilities Rollup")) {
        sentimentClass = SentimentRollup.AVERAGE_PROBABILITIES.apply(sentences);
    }

    if (sentimentClass != null) {
        return sentimentClass.toString();
    } else
        return null;
}

From source file:de.tudarmstadt.ukp.dkpro.core.stanfordnlp.StanfordSentimentAnalyzer.java

License:Open Source License

@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
    for (Sentence sentenceDKPro : JCasUtil.select(jCas, Sentence.class)) {
        String sentenceText = sentenceDKPro.getCoveredText();

        Annotation annotation = pipeline.process(sentenceText);

        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
            Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
            SimpleMatrix sentimentCoefficients = RNNCoreAnnotations.getPredictions(tree);

            double veryNegative = sentimentCoefficients.get(0);
            double negative = sentimentCoefficients.get(1);
            double neutral = sentimentCoefficients.get(2);
            double positive = sentimentCoefficients.get(3);
            double veryPositive = sentimentCoefficients.get(4);

            StanfordSentimentAnnotation sentimentAnnotation = new StanfordSentimentAnnotation(jCas);
            sentimentAnnotation.setBegin(sentenceDKPro.getBegin());
            sentimentAnnotation.setEnd(sentenceDKPro.getEnd());
            sentimentAnnotation.setVeryNegative(veryNegative);
            sentimentAnnotation.setNegative(negative);
            sentimentAnnotation.setNeutral(neutral);
            sentimentAnnotation.setPositive(positive);
            sentimentAnnotation.setVeryPositive(veryPositive);
            sentimentAnnotation.addToIndexes();
        }//from  w w w  .  ja va2 s  .  com
    }
}

From source file:de.tudarmstadt.ukp.dkpro.core.stanfordsentiment.StanfordSentimentAnnotator.java

License:Open Source License

@Override
public void process(JCas jCas) throws AnalysisEngineProcessException {
    Collection<Sentence> dkproSentences = JCasUtil.select(jCas, Sentence.class);

    if (dkproSentences.isEmpty()) {
        throw new AnalysisEngineProcessException(new IllegalStateException("No sentences annotated"));
    }/*  w ww  .j  a  va 2  s . c om*/

    for (Sentence sentenceDKPro : dkproSentences) {
        String sentenceText = sentenceDKPro.getCoveredText();

        Annotation annotation = pipeline.process(sentenceText);

        for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
            Tree tree = sentence.get(SentimentCoreAnnotations.AnnotatedTree.class);
            SimpleMatrix sentimentCoefficients = RNNCoreAnnotations.getPredictions(tree);

            double veryNegative = sentimentCoefficients.get(0);
            double negative = sentimentCoefficients.get(1);
            double neutral = sentimentCoefficients.get(2);
            double positive = sentimentCoefficients.get(3);
            double veryPositive = sentimentCoefficients.get(4);

            StanfordSentimentAnnotation sentimentAnnotation = new StanfordSentimentAnnotation(jCas);
            sentimentAnnotation.setBegin(sentenceDKPro.getBegin());
            sentimentAnnotation.setEnd(sentenceDKPro.getEnd());
            sentimentAnnotation.setVeryNegative(veryNegative);
            sentimentAnnotation.setNegative(negative);
            sentimentAnnotation.setNeutral(neutral);
            sentimentAnnotation.setPositive(positive);
            sentimentAnnotation.setVeryPositive(veryPositive);
            sentimentAnnotation.addToIndexes();
        }
    }
}

From source file:nlp.NLP.java

public static int findSentiment(String review) {

    int mainSentiment = 0;
    // checking if the length of the review is > 0 or not. Therefore
    // checking if any review is present or not.
    if (review != null && review.length() > 0) {
        int longest = 0;
        try {/*ww  w.  j ava2  s . c  o m*/
            // Process the reviews with the annotators in the
            // StanfordCoreNLP object.
            Annotation annotation = pipeline.process(review);

            // a CoreMap is essentially a Map that uses class objects as
            // keys and has values with custom types
            for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
                // Parses the sentence in the tree structure
                Tree tree = sentence.get(SentimentAnnotatedTree.class);

                // Recursively traverse through the tree. Performs Recursive
                // Neural Network.
                // It gets the sentiment score of that sentence.
                int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
                SimpleMatrix sentiment_new = RNNCoreAnnotations.getPredictions(tree);

                // converting String to text
                String partText = sentence.toString();

                // Checking the size of the text.
                if (partText.length() > longest) {
                    mainSentiment = sentiment;
                    // Saving the length of the text which will be used for
                    // sentences larger than the sentence.
                    longest = partText.length();
                }
            }
        } catch (Exception e) {
            // System.out.println("Exception is " + e.printStackTrace());
            e.printStackTrace();
        }
    }
    // returning the sentiment score.
    return mainSentiment;
}

From source file:opennlp.tools.parse_thicket.opinion_processor.DefaultSentimentProcessor.java

License:Apache License

/**
 * Outputs the scores from the tree. Counts the tree nodes the same as
 * setIndexLabels./*from   w  w  w  .  j a  v  a  2  s.  c o m*/
 */
static int outputTreeScores(PrintStream out, Tree tree, int index) {
    if (tree.isLeaf()) {
        return index;
    }

    out.print("  " + index + ":");
    SimpleMatrix vector = RNNCoreAnnotations.getPredictions(tree);
    for (int i = 0; i < vector.getNumElements(); ++i) {
        out.print("  " + NF.format(vector.get(i)));
    }
    out.println();
    index++;
    for (Tree child : tree.children()) {
        index = outputTreeScores(out, child, index);
    }
    return index;
}