List of usage examples for edu.stanford.nlp.pipeline StanfordCoreNLP xmlPrint
public void xmlPrint(Annotation annotation, OutputStream os) throws IOException
From source file:samples.TwitterSentiment.java
public static void main(String[] args) { Date final_date = Date.from(Instant.now()); Date initial_date = addDays(final_date, -10); Date tempdate = (Date) initial_date.clone(); String t4jquery = "trump"; while (tempdate.before(final_date)) { PrintWriter xmlOut = null; try {//from ww w.ja v a 2s .c o m System.out.println("------------------------------------------------"); tempdate = addDays(tempdate, 1); System.out.println(tempdate); SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy"); System.out.println(dateFormat.format(tempdate)); String filename = System.getProperty("user.dir") + "\\xmls\\" + dateFormat.format(tempdate) + ".xml"; System.err.println(filename); String[] twittermessages = get_twits(tempdate, t4jquery); String message = ""; for (String twittermessage : twittermessages) { message += twittermessage + "."; } // http://stackoverflow.com/questions/22434081/sentiments-scores-stanford-core-nlp xmlOut = new PrintWriter(filename); Properties props = new Properties(); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, sentiment"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation annotation = new Annotation(message); pipeline.annotate(annotation); pipeline.xmlPrint(annotation, xmlOut); List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class); if (sentences != null && sentences.size() > 0) { int longest = 0; // Annotation annotation = pipeline.process(line); Long textLength = 0L; int sumOfValues = 0; for (CoreMap sentence : sentences) { Tree tree = sentence.get(SentimentCoreAnnotations.SentimentAnnotatedTree.class); int sentiment = RNNCoreAnnotations.getPredictedClass(tree); String partText = sentence.toString(); if (partText.length() > longest) { textLength += partText.length(); sumOfValues = sumOfValues + sentiment * partText.length(); System.out.println(sentiment + " " + partText); } } System.out.println("Overall: " + (double) sumOfValues / textLength); // CoreMap sentence = sentences.get(0); // Tree tree = sentence.get(TreeAnnotation.class); // PrintWriter out = new PrintWriter(System.out); // out.println("The first sentence parsed is:"); // tree.pennPrint(out); } //------------------------------------------------------- /* PrintWriter xmlOut = new PrintWriter("xmlOutput.xml"); Properties props = new Properties(); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse, sentiment"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation annotation = new Annotation( "It is a wonderful day. the cat runs"); pipeline.annotate(annotation); pipeline.xmlPrint(annotation, xmlOut); // An Annotation is a Map and you can get and use the // various analyses individually. For instance, this // gets the parse tree of the 1st sentence in the text. List<CoreMap> sentences = annotation.get( CoreAnnotations.SentencesAnnotation.class); if (sentences != null && sentences.size() > 0) { CoreMap sentence = sentences.get(0); Tree tree = sentence.get(TreeAnnotation.class); PrintWriter out = new PrintWriter(System.out); out.println("The first sentence parsed is:"); tree.pennPrint(out); }*/ } catch (FileNotFoundException ex) { Logger.getLogger(TwitterSentiment.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(TwitterSentiment.class.getName()).log(Level.SEVERE, null, ex); } finally { xmlOut.close(); } } }
From source file:service.AnnotationProcessor.java
License:Apache License
public static void annotate(final String textInput, final String outputFileName) throws IOException { PrintWriter xmlOut = new PrintWriter(outputFileName); Properties props = new Properties(); // props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, // parse, cleanxml"); props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse"); StanfordCoreNLP pipeline = new StanfordCoreNLP(props); Annotation annotation = new Annotation(textInput); pipeline.annotate(annotation);//w w w.j a v a2 s . c om pipeline.xmlPrint(annotation, xmlOut); // An Annotation is a Map and you can get and use the // various analyses individually. For instance, this // gets the parse tree of the 1st sentence in the text. List<CoreMap> sentences = annotation.get(CoreAnnotations.SentencesAnnotation.class); if (sentences != null && sentences.size() > 0) { CoreMap sentence = sentences.get(0); Tree tree = sentence.get(TreeAnnotation.class); PrintWriter out = new PrintWriter(System.out); out.println("The first sentence parsed is:"); if (tree != null) { tree.pennPrint(out); } } }