List of usage examples for edu.stanford.nlp.pipeline StanfordCoreNLP StanfordCoreNLP
public StanfordCoreNLP(String propsFileNamePrefix)
From source file:SemanticSimilarity.java
License:Open Source License
public static void main(String[] args) { int paramIndex; /* Initialize the Stanford NLP with the needed annotators (ssplit is mandatory) */ Properties prop = new Properties(); prop.put("annotators", "tokenize, ssplit, pos, lemma"); StanfordCoreNLP pipeline = new StanfordCoreNLP(prop); System.out.println(":: Stanford NLP pipeline initialized correctly."); /* If a properties file is passed, read and set options from it. */ if ((paramIndex = findArgument(args, "--parameters")) != -1) { parseProperties(args, paramIndex); } else {// w ww . j a va 2 s . c o m paramIndex = -2; } String inputFile = null; String outputFile = null; /* * Take options "after" the properties file, or at args[0] and args[1] if there was * no properties file. */ if (args.length > (paramIndex + 2)) { inputFile = args[paramIndex + 2]; } if (args.length > (paramIndex + 3)) { outputFile = args[paramIndex + 3]; } /* Parses a JSON tweet and gets the sentence pairs */ System.out.print(":: Parsing input tweet... "); TweetParser tp = new TweetParser(inputFile, outputFile, pipeline); tp.parse(); List<SentencePair> pairs = tp.getSentencePairs(); System.out.println("OK"); /* Load the pre-existing model from file */ SimilarityTest m = new SimilarityTest(pipeline); /* Gets the similarity scores and writes them on the JSON output file */ System.out.print(":: Computing and writing similarities on output... "); double[] similarities = m.getSimilarities(pairs); tp.writeSimilarities(similarities); System.out.println("OK"); }
From source file:StanfordCoreNLPXMLServer.java
License:Open Source License
public static void main(String args[]) throws Exception { // use port if given try {/*from w w w .j a v a 2s. co m*/ port = Integer.parseInt(args[0]); } catch (Exception e) { // silently keep port at 8080 } // initialize the Stanford Core NLP java.util.Properties props = new java.util.Properties(); props.setProperty("sutime.markTimeRanges", "true"); props.setProperty("sutime.includeRange", "true"); TimeAnnotator sutime = new TimeAnnotator("sutime", props); pipeline = new StanfordCoreNLP(props); pipeline.addAnnotator(sutime); // start the server Container container = new StanfordCoreNLPXMLServer(); Server server = new ContainerServer(container); Connection connection = new SocketConnection(server); SocketAddress address = new InetSocketAddress(port); connection.connect(address); log.info("Initialized server at port " + port + "."); }
From source file:Dependency.java
public static void init() { Properties props = new Properties(); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, parse, sentiment"); pipeline = new StanfordCoreNLP(props); }
From source file:DateRecognitionFunction.java
License:Apache License
@Override public void initialize(IFunctionHelper functionHelper) throws Exception { Properties props = new Properties(); props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref"); pipeline = new StanfordCoreNLP(props); }
From source file:CrossValidation.java
License:Open Source License
public static void main(String[] args) { if (args.length == 0) { System.out.println("Usage: CrossValidation <sample files>"); System.exit(-1);// ww w.j a v a2 s . c om } /* Suppress all output from libsvm */ svm_print_interface iface = new svm_print_interface() { @Override public void print(String string) { } }; svm.svm_set_print_string_function(iface); /* Initialize the Stanford NLP pipeline */ Properties prop = new Properties(); prop.put("annotators", "tokenize, ssplit, pos, lemma"); StanfordCoreNLP pipeline = new StanfordCoreNLP(prop); System.out.println(":: Stanford NLP pipeline initialized correctly."); String[] files = { args[0] }; SimilarityLearner sl = new SimilarityLearner(pipeline); List<TrainingSample> samples = sl.extractFeatures(files); svm_problem problem = sl.buildSVMProblem(samples); svm_parameter parameters = Constants.getSVMParameters(); double[] C_values = Constants.getCValues(); double[] P_values = Constants.getPValues(); double[] G_values = Constants.getGammaValues(); double bestCorr = Double.MIN_VALUE; double bestC = 0.0; double bestP = 0.0; double bestGamma = 0.0; double[] targets = new double[samples.size()]; double[] gs = new double[samples.size()]; /* gold standard scores provided with the samples */ int i = 0; for (Iterator<TrainingSample> it = samples.iterator(); it.hasNext();) { gs[i++] = it.next().target; } System.out.println(":: Starting cross validation."); for (int iC = 0; iC < C_values.length; iC++) { parameters.C = C_values[iC]; for (int iP = 0; iP < P_values.length; iP++) { parameters.p = P_values[iP]; for (int iG = 0; iG < G_values.length; iG++) { parameters.gamma = G_values[iG]; System.out.println( "Trying C = " + parameters.C + ", P = " + parameters.p + ", G = " + parameters.gamma); svm.svm_cross_validation(problem, parameters, Constants.getValidationFold(), targets); double corr = Correlation.getPearsonCorrelation(targets, gs); if (corr > bestCorr) { System.out.println(":: New best correlation is " + corr); bestCorr = corr; bestC = C_values[iC]; bestP = P_values[iP]; bestGamma = G_values[iG]; } } } } System.out.println(":: Cross validation finished."); System.out.println("C: " + bestC); System.out.println("P: " + bestP); System.out.println("Gamma: " + bestGamma); System.out.println("Best correlation is " + bestCorr); }
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();// w w w. j a v a2 s .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:SentimentAnalysisFunction.java
License:Apache License
@Override public void initialize(IFunctionHelper functionHelper) throws Exception { Properties props = new Properties(); props.setProperty("annotators", "tokenize, ssplit, parse, sentiment"); pipeline = new StanfordCoreNLP(props); }
From source file:SimilarityTrain.java
License:Open Source License
public static void main(String[] args) { if (args.length == 0) { System.err.println("Usage: java SimilarityTrain <train file(s)>"); System.exit(-1);// w ww . j a v a 2 s. co m } Properties prop = new Properties(); prop.put("annotators", "tokenize, ssplit, pos, lemma"); StanfordCoreNLP pipeline = new StanfordCoreNLP(prop); System.out.println(":: Stanford NLP pipeline initialized correctly."); SimilarityLearner sl = new SimilarityLearner(pipeline); List<TrainingSample> samples = sl.extractFeatures(args); sl.learnModel(samples); }
From source file:rev.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods.//from w ww .j a v a 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 void StanfordLemmatizer() { Properties props;/*from w w w . j a v a 2 s . c om*/ props = new Properties(); props.put("annotators", "tokenize, ssplit, pos, lemma"); pipeline = new StanfordCoreNLP(props); }