Example usage for edu.stanford.nlp.pipeline Annotation Annotation

List of usage examples for edu.stanford.nlp.pipeline Annotation Annotation

Introduction

In this page you can find the example usage for edu.stanford.nlp.pipeline Annotation Annotation.

Prototype

public Annotation(List<CoreMap> sentences) 

Source Link

Document

Make a new Annotation from a List of tokenized sentences.

Usage

From source file:StanfordCoreNLPXMLServer.java

License:Open Source License

public String parse(String s, String date) throws java.io.IOException {
    Annotation annotation = new Annotation(s);
    annotation.set(CoreAnnotations.DocDateAnnotation.class, date);
    pipeline.annotate(annotation);/*from  w ww .  j  a  v a  2 s  .  c om*/

    StringBuilder sb = new StringBuilder();

    List<CoreMap> timexAnnsAll = annotation.get(TimeAnnotations.TimexAnnotations.class);

    sb.append(xmlPrefix);
    for (CoreMap cm : timexAnnsAll) {
        Timex t = cm.get(TimeAnnotations.TimexAnnotation.class);
        sb.append(t);
        sb.append("\n");
    }
    sb.append(xmlPostfix);

    return sb.toString();
}

From source file:SentencePair.java

License:Open Source License

private void createSentence(String text, List<POSTaggedToken> sentence) {
    Annotation d = new Annotation(text);
    nlp.annotate(d);//from  ww w . jav a 2s .  co m

    for (CoreMap ss : d.get(CoreAnnotations.SentencesAnnotation.class)) {
        for (CoreLabel token : ss.get(CoreAnnotations.TokensAnnotation.class)) {
            sentence.add(new POSTaggedToken(token.toString(), translateTag(token.tag())));
        }
    }
}

From source file:SentencePair.java

License:Open Source License

private void lemmatize(List<POSTaggedToken> sentence) {
    String text = "";

    /* Convert the sentence back to a single string */
    for (POSTaggedToken tt : sentence) {
        text += tt.token + " ";
    }/*from  w  w  w . j a v a 2s . co m*/

    Annotation d = new Annotation(text);
    nlp.annotate(d);

    for (CoreMap ss : d.get(CoreAnnotations.SentencesAnnotation.class)) {
        Iterator<CoreLabel> itToken = ss.get(CoreAnnotations.TokensAnnotation.class).iterator();
        ListIterator<POSTaggedToken> itSentence = sentence.listIterator();

        while (itToken.hasNext() && itSentence.hasNext()) {
            CoreLabel token = itToken.next();
            POSTaggedToken tt = itSentence.next();
            tt.lemma = token.lemma(); /* add a lemma to the POSTaggedToken */

            itSentence.set(tt);
        }
    }
}

From source file:DateRecognitionFunction.java

License:Apache License

public static Set<String> ner(String text) {

    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);//from ww  w . ja v  a  2 s  .  c  om

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);

    Set<String> entities = new HashSet<>();

    for (CoreMap sentence : sentences) {
        // traversing the words in the current sentence
        // a CoreLabel is a CoreMap with additional token-specific methods
        for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
            // this is the text of the token
            String word = token.get(CoreAnnotations.TextAnnotation.class);

            // this is the NER label of the token
            String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);

            if (ne.equals("DATE")) {
                entities.add(word);
            }
        }
    }

    return entities;

}

From source file:PersonRecognitionFunction.java

License:Apache License

public static Set<String> ner(String text) {

    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);//from   w w  w  . j  a va 2s  .co  m

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);

    Set<String> locations = new HashSet<>();

    for (CoreMap sentence : sentences) {
        // traversing the words in the current sentence
        // a CoreLabel is a CoreMap with additional token-specific methods
        for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
            // this is the text of the token
            String word = token.get(CoreAnnotations.TextAnnotation.class);

            // this is the NER label of the token
            String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);

            if (ne.equals("PERSON")) {
                locations.add(word);
            }
        }
    }

    return locations;

}

From source file:Treeparse.java

public static void main(String[] args) {
    // TODO code application logic here

    Properties props = new Properties();
    props.setProperty("annotators", "tokenize, ssplit, pos, lemma,parse");
    StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
    System.out.println("Enter the text:");
    Scanner sc = new Scanner(System.in);
    text = sc.nextLine();/*from  w  ww.ja v  a  2s.co  m*/

    //while(text!="exit")
    //{
    Annotation document = new Annotation(text);
    pipeline.annotate(document);

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

    for (CoreMap sentence : sentences) {

        token_length = sentence.get(TokensAnnotation.class).size();
        arr1 = new String[POSTagger.token_length];
        arr2 = new String[POSTagger.token_length];
        int i = 0, j = 0;

        // System.out.println("Size"+token_length);
        for (CoreLabel token : sentence.get(TokensAnnotation.class)) {

            String word = token.get(TextAnnotation.class);

            String pos = token.get(PartOfSpeechAnnotation.class);
            //    String ner = token.get(NamedEntityTagAnnotation.class);

        }
        Tree tree = sentence.get(TreeAnnotation.class);
        // System.out.println(tree);
        List<Tree> x = GetNounPhrases(tree);
        System.out.println(x);

        // Print words and Pos Tags
        /*for (Tree leaf : leaves) { 
            Tree parent = leaf.parent(tree);
            System.out.print(leaf.label().value() + "-" + parent.label().value() + " ");
        }*/
    }
    //System.out.println("Enter the text:");
    //text=sc.nextLine();  

}

From source file:rev.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./* ww w .  j  ava  2 s  .  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:unCompressedIndex.java

public static String lemmatize(String documentText) {
    List<String> lemmas = new LinkedList<String>();
    // Create an empty Annotation just with the given text
    Annotation document = new Annotation(documentText);
    // run all Annotators on this text
    pipeline.annotate(document);/*  www  .j a v  a  2  s .com*/
    // Iterate over all of the sentences found
    String temp = "";
    List<CoreMap> sentences = document.get(SentencesAnnotation.class);
    for (CoreMap sentence : sentences) {
        // Iterate over all tokens in a sentence
        for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
            temp = token.get(LemmaAnnotation.class);
            lemmas.add(temp);

        }
    }
    return lemmas.get(0).toString();
    //System.out.println("lemmas:"+lemmas.get(0));
}

From source file:LocationRecognitionFunction.java

License:Apache License

public static Set<String> ner(String text) {

    Annotation document = new Annotation(text);

    // run all Annotators on this text
    pipeline.annotate(document);/*from   w w  w  .  j a  v  a 2 s .co  m*/

    // these are all the sentences in this document
    // a CoreMap is essentially a Map that uses class objects as keys and has values with custom types
    List<CoreMap> sentences = document.get(CoreAnnotations.SentencesAnnotation.class);

    Set<String> locations = new HashSet<>();

    for (CoreMap sentence : sentences) {
        // traversing the words in the current sentence
        // a CoreLabel is a CoreMap with additional token-specific methods
        for (CoreLabel token : sentence.get(CoreAnnotations.TokensAnnotation.class)) {
            // this is the text of the token
            String word = token.get(CoreAnnotations.TextAnnotation.class);

            // this is the NER label of the token
            String ne = token.get(CoreAnnotations.NamedEntityTagAnnotation.class);

            if (ne.equals("LOCATION")) {
                locations.add(word);
            }
        }
    }

    return locations;

}

From source file:agk.chatbot.nlp.StanfordAnnotator.java

License:Open Source License

@Override
public NLPText processString(String content) {
    NLPText txt = new NLPText(content);
    Annotation doc = new Annotation(content);
    pipeline.annotate(doc);/*  w  ww.  j  ava2s .c  o  m*/

    for (CoreMap sentence : doc.get(SentencesAnnotation.class)) {
        String text = sentence.get(TextAnnotation.class);
        NLPSentence s = new NLPSentence(text);

        for (CoreLabel token : sentence.get(TokensAnnotation.class)) {
            String word = token.get(TextAnnotation.class);
            String pos = token.get(PartOfSpeechAnnotation.class);
            String lemma = token.get(LemmaAnnotation.class);
            String ner = token.get(NamedEntityTagAnnotation.class);

            NLPToken t = new NLPToken(word);
            t.setNerTag(ner);
            t.setPostag(pos);
            t.setStem(lemma);
            t.setMainName(!ner.equals("O"));
            t.setStopWord(NLPText.isStopWord(word));
            s.addToken(t);
        }
        txt.addSentence(s);
    }

    return txt;
}