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

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

Introduction

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

Prototype

public static int getPredictedClass(Label label) 

Source Link

Document

Return as an int the predicted class.

Usage

From source file:Dependency.java

public static int findSentiment(String review) {

    int mainSentiment = 0;
    if (review != null && review.length() > 0) {
        int longest = 0;
        Annotation annotation = pipeline.process(review);
        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();
            }//  w  ww . j  a  va  2s  .  c o m

        }
    }
    return mainSentiment;
}

From source file:SentimentAnalysisFunction.java

License:Apache License

public static int findSentiment(String tweet) {

    int mainSentiment = 0;
    if (tweet != null && tweet.length() > 0) {
        int longest = 0;
        Annotation annotation = pipeline.process(tweet);
        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();
            }/* w ww  . j  a  va  2 s.com*/
        }
    }
    return mainSentiment;
}

From source file:rev.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w w  w  . ja v  a  2s .c o  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException, ClassNotFoundException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        String a = request.getParameter("userMsg");

        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE HTML>\n" + "<head>\n"
                + "<link href=\"css/style.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\"/>\n"
                + "<link href=\"css/slider.css\" rel=\"stylesheet\" type=\"text/css\" media=\"all\"/>\n"
                + "<script type=\"text/javascript\" src=\"js/jquery-1.9.0.min.js\"></script>\n"
                + "<script type=\"text/javascript\" src=\"js/move-top.js\"></script>\n"
                + "<script type=\"text/javascript\" src=\"js/easing.js\"></script>\n"
                + "<script type=\"text/javascript\" src=\"js/jquery.nivo.slider.js\"></script>\n"
                + "<script type=\"text/javascript\">\n" + "    $(window).load(function() {\n"
                + "        $('#slider').nivoSlider();\n" + "    });\n" + "    <%! String n;\n" + "    %>\n"
                + "    <%  \n" + "          \n" + "        n=(String)session.getAttribute(\"uname\"); \n"
                + "        %>\n" + "    </script>\n" + "</head>\n" + "<body>\n" + "   <div class=\"header\">\n"
                + "       <div class=\"headertop_desc\">\n" + "         <div class=\"wrap\">\n"
                + "            <div class=\"nav_list\">\n" + "               \n" + "            </div>\n"
                + "            <div class=\"account_desc\">\n" + "                  <ul>\n"
                + "                     <li><a href=\"available.jsp\">Available movies</a></li>\n"
                + "                     <li><a href=\"takereview.jsp\">Review Movies</a></li>\n"
                + "                     <li><a href=\"rated.jsp\">Movies Rated</a></li>\n"
                + "                                                        <li><a href=\"abc.jsp\">Recommend Me</a></li>\n"
                + "                                                        \n"
                + "                                                        <li><a href=\"contact.html\">Contact</a></li>\n"
                + "                                                        <li><a href=\"logout\">Logout</a></li>\n"
                + "                                                </ul>\n" + "               </div>\n"
                + "            <div class=\"clear\"></div>\n" + "         </div>\n" + "        </div>\n"
                + "             <div class=\"wrap\">\n" + "            <div class=\"header_top\">\n"
                + "               <div class=\"logo\">\n"
                + "                  <a href=\"index.html\"><img src=\"images/logo1.jpg\" alt=\"\" /></a>\n"
                + "               </div>\n" + "               <div class=\"header_top_right\">\n"
                + "                <div class=\"search_box\">\n" + "                          \n"
                + "                       </div>\n" + "                    <div class=\"clear\"></div>\n"
                + "               </div>\n" + "                   \n"
                + "          <div class=\"clear\"></div>\n" + "        </div>\n" + "            \n" + "   \n"
                + "\n" + "\n" + "");

        String line = "this book is too good to sleep";
        Properties props = new Properties();
        props.setProperty("annotators", "tokenize, ssplit, parse, sentiment, lemma");
        StanfordCoreNLP pipeline = new StanfordCoreNLP(props);

        Annotation annotation = new Annotation(a);

        pipeline.annotate(annotation);

        annotation.toShorterString();

        List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class);

        if (sentences != null && !sentences.isEmpty()) {
            for (int i = 0; i < sentences.size(); i++) {
                CoreMap sentence = sentences.get(i);
                Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class);
                int sentiment = RNNCoreAnnotations.getPredictedClass(tree);
                String sentimentName = sentence.get(SentimentCoreAnnotations.SentimentClass.class);
                //Class.forName("com.mysql.jdbc.Driver");

                /*String connectionURL = "jdbc:mysql://localhost:3306/review";
                       Connection conn;
                        Statement stmt;
                        ResultSet rs;
                      conn = DriverManager.getConnection (connectionURL,"root","");
                       stmt = conn.createStatement();
                      // rs = stmt.executeQuery("");
                      out.println();
                         
                        
                */

                out.println("The sentence is:");
                sentence.get(CoreAnnotations.TextAnnotation.class);

                //out.println("Sentiment of \n> \""++"\"\nis: " + sentiment+" (i.e., "+sentimentName+")");
                out.println(sentimentName + " " + sentiment);
                if (sentimentName.equalsIgnoreCase("Negative")) {
                    final String negative = "negative";
                    final String positive = "positive";

                    final String nuetral = "nuetral";
                    final String verypositive = "very positive";
                    final String verynegative = " very negative";
                    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

                    //out.println("NOT");
                    dataset.addValue(0, positive, positive);
                    dataset.addValue(sentiment, negative, negative);
                    dataset.addValue(0, nuetral, nuetral);
                    dataset.addValue(0, verynegative, verynegative);
                    dataset.addValue(0, verypositive, verypositive);
                    JFreeChart barChart = ChartFactory.createBarChart("Movie Reviews", "Ratings", "Sentiments",
                            dataset, PlotOrientation.VERTICAL, true, true, false);

                    int width = 640; /* Width of the image */
                    int height = 480; /* Height of the image */
                    File BarChart = new File("/home/rishabh/NetBeansProjects/minor/web/images/k.jpeg");
                    ChartUtilities.saveChartAsJPEG(BarChart, barChart, width, height);
                    out.println("<img src=\"images/BarChart.jpeg\">");

                } else if (sentimentName.equalsIgnoreCase("Positive")) {
                    final String negative = "negative";
                    final String positive = "positive";

                    final String nuetral = "nuetral";
                    final String verypositive = "very positive";
                    final String verynegative = " very negative";
                    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

                    // out.println("Good");
                    dataset.addValue(sentiment, positive, positive);
                    dataset.addValue(0, negative, negative);
                    dataset.addValue(0, nuetral, nuetral);
                    dataset.addValue(0, verynegative, verynegative);
                    dataset.addValue(0, verypositive, verypositive);
                    JFreeChart barChart = ChartFactory.createBarChart("Movie Reviews", "Ratings", "Sentiments",
                            dataset, PlotOrientation.VERTICAL, true, true, false);

                    int width = 640; /* Width of the image */
                    int height = 480; /* Height of the image */
                    File BarChart = new File("/home/rishabh/NetBeansProjects/minor/web/images/k.jpeg");
                    ChartUtilities.saveChartAsJPEG(BarChart, barChart, width, height);
                    out.println("<img src=\"images/BarChart1.jpeg\">");

                } else if (sentimentName.equalsIgnoreCase("Neutral")) {
                    final String negative = "negative";
                    final String positive = "positive";

                    final String nuetral = "nuetral";
                    final String verypositive = "very positive";
                    final String verynegative = " very negative";
                    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

                    //out.println("Good");
                    dataset.addValue(0, positive, positive);
                    dataset.addValue(0, negative, negative);
                    dataset.addValue(sentiment, nuetral, nuetral);
                    dataset.addValue(0, verynegative, verynegative);
                    dataset.addValue(0, verypositive, verypositive);
                    JFreeChart barChart = ChartFactory.createBarChart("Movie Reviews", "Ratings", "Sentiments",
                            dataset, PlotOrientation.VERTICAL, true, true, false);

                    int width = 640; /* Width of the image */
                    int height = 480; /* Height of the image */
                    File BarChart = new File("/home/rishabh/NetBeansProjects/minor/web/images/k.jpeg");
                    ChartUtilities.saveChartAsJPEG(BarChart, barChart, width, height);

                    out.println("<img src=\"images/BarChart2.jpeg\">");
                } else if (sentimentName.equalsIgnoreCase("Very Positive")) {
                    final String negative = "negative";
                    final String positive = "positive";

                    final String nuetral = "nuetral";
                    final String verypositive = "very positive";
                    final String verynegative = " very negative";
                    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

                    //out.println("Good");
                    dataset.addValue(0, positive, positive);
                    dataset.addValue(0, negative, negative);
                    dataset.addValue(0, nuetral, nuetral);
                    dataset.addValue(0, verynegative, verynegative);
                    dataset.addValue(sentiment, verypositive, verypositive);
                    JFreeChart barChart = ChartFactory.createBarChart("Movie Reviews", "Ratings", "Sentiments",
                            dataset, PlotOrientation.VERTICAL, true, true, false);

                    int width = 640; /* Width of the image */
                    int height = 480; /* Height of the image */
                    File BarChart = new File("/home/rishabh/NetBeansProjects/minor/web/images/k.jpeg");
                    ChartUtilities.saveChartAsJPEG(BarChart, barChart, width, height);
                    out.println("<img src=\"images/BarChart4.jpeg\">");

                } else if (sentimentName.equalsIgnoreCase("Very Negative")) {
                    final String negative = "negative";
                    final String positive = "positive";

                    final String nuetral = "nuetral";
                    final String verypositive = "very positive";
                    final String verynegative = " very negative";
                    final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

                    //out.println("Good");
                    dataset.addValue(0, positive, positive);
                    dataset.addValue(0, negative, negative);
                    dataset.addValue(0, nuetral, nuetral);
                    dataset.addValue(sentiment, verynegative, verynegative);
                    dataset.addValue(0, verypositive, verypositive);
                    JFreeChart barChart = ChartFactory.createBarChart("Movie Reviews", "Ratings", "Sentiments",
                            dataset, PlotOrientation.VERTICAL, true, true, false);

                    int width = 640; /* Width of the image */
                    int height = 480; /* Height of the image */
                    File BarChart = new File("/home/rishabh/NetBeansProjects/minor/web/images/k.jpeg");
                    ChartUtilities.saveChartAsJPEG(BarChart, barChart, width, height);

                    out.println("<img src=\"images/BarChart3.jpeg\">");
                }

            }
        }

        out.println("<div class=\"footer\">\n" + "        <div class=\"wrap\">\n"
                + "        <div class=\"section group\">\n" + "            <div class=\"col span\">\n"
                + "                  <h4>Information</h4>\n" + "                  <ul>\n"
                + "                  <li><a href=\"#\">About Us</a></li>\n" + "                  \n"
                + "                  <li><a href=\"contact.html\">Contact Us</a></li>\n"
                + "                  </ul>\n" + "               </div>\n"
                + "            <div class=\"col span\">\n" + "               <h4>Know us better</h4>\n"
                + "                  <ul>\n" + "                  <li><a href=\"#\">About Us</a></li>\n"
                + "            \n" + "                  <li><a href=\"contact.html\">Site Map</a></li>\n"
                + "                  <li><a href=\"#\">Search Terms</a></li>\n" + "                  </ul>\n"
                + "            </div>\n" + "            \n" + "            <div class=\"col span\">\n"
                + "               <h4>Contact</h4>\n" + "                  <ul>\n"
                + "                     <li><span>9971825755</span></li>\n"
                + "                     <li><span>8130527232</span></li>\n" + "                  </ul>\n"
                + "                  <div class=\"social-icons\">\n"
                + "                     <h4>Follow Us</h4>\n" + "                          <ul>\n"
                + "                           <li><a href=\"#\" target=\"_blank\"><img src=\"images/facebook.png\" alt=\"\" /></a></li>\n"
                + "                           <li><a href=\"#\" target=\"_blank\"><img src=\"images/twitter.png\" alt=\"\" /></a></li>\n"
                + "                           <li><a href=\"#\" target=\"_blank\"><img src=\"images/skype.png\" alt=\"\" /> </a></li>\n"
                + "                           <li><a href=\"#\" target=\"_blank\"> <img src=\"images/linkedin.png\" alt=\"\" /></a></li>\n"
                + "                           <div class=\"clear\"></div>\n" + "                       </ul>\n"
                + "                      </div>\n" + "            </div>\n" + "         </div>\n"
                + "          <div class=\"copy_right\">\n"
                + "            <p>Company Name  All rights Reseverd </p>\n" + "         </div>\n"
                + "        </div>\n" + "    </div>\n" + "    <script type=\"text/javascript\">\n"
                + "      $(document).ready(function() {\n"
                + "         $().UItoTop({ easingType: 'easeOutQuart' });\n" + "\n" + "      });\n"
                + "   </script>\n" + "    <a href=\"#\" id=\"toTop\"><span id=\"toTopHover\"> </span></a>\n"
                + "</body>\n" + "</html>\n" + "\n" + "");

    }
}

From source file:BuildBinarizedDataset.java

public static void setPredictedLabels(Tree tree) {
    if (tree.isLeaf()) {
        return;/*from w  ww  .j a  va2s  .co m*/
    }

    for (Tree child : tree.children()) {
        setPredictedLabels(child);
    }

    tree.label().setValue(Integer.toString(RNNCoreAnnotations.getPredictedClass(tree)));
}

From source file:analyzer.SentimentAnalyzerBean.java

@Override
public Sentiment findSentiment(final String text) {
    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, parse, sentiment");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    int mainSentiment = 0;
    if (text != null && text.length() > 0) {
        int longest = 0;
        Annotation annotation = pipeline.process(text);
        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();
            }/*from   w w w . j  ava2  s. c  o  m*/

        }
    }

    return Sentiment.getFromValue(mainSentiment);
}

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);
    }//w w  w.j ava 2 s . c  o  m

    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:beproject.PolarityAnalyzer.java

License:Open Source License

int getSentiment(String text) {
    text = text.replaceAll("\n", "");
    int mainSentiment = 0;
    if (text != null && text.length() > 0) {
        int longest = 0;
        Annotation annotation = pipeline.process(text);
        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) {
                mainSentiment = sentiment;
                longest = partText.length();
            }//from  w ww. ja  v a  2s .c om
        }
    }

    if (mainSentiment > 4 || mainSentiment < 0) {
        System.out.println(text + ": " + mainSentiment);
    }
    return mainSentiment;
}

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   www  . ja va 2  s  .co 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:byusentiment.Tweet.java

public void calculateSentiment() {
    String text = this.dbrow.get("text").toString();
    Annotation document = new Annotation(text);
    Byusentiment.pipeline.annotate(document);
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);
    int avgSentimentScore = 0;
    int its = 0;/*from  w  ww  .j  a va2s.c om*/
    for (CoreMap sentence : sentences) {
        its++;
        Tree t = sentence.get(edu.stanford.nlp.sentiment.SentimentCoreAnnotations.AnnotatedTree.class);
        int sentimentScore = RNNCoreAnnotations.getPredictedClass(t);
        avgSentimentScore += sentimentScore;
    }
    avgSentimentScore = avgSentimentScore / its;

    this.dbrow.append("sentiment", avgSentimentScore);

    System.out.println(text);

}

From source file:ch.zhaw.parallelComputing.model.sentiment.TweetMapper.java

License:Open Source License

private Integer 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) {
        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) {
                mainSentiment = sentiment;
                longest = partText.length();
            }/*  ww  w. j a v  a2 s .  c  o  m*/
        }
    }
    return mainSentiment;
}