Last active
August 29, 2015 14:15
-
-
Save xiyuanHou/bf38d79c66eb1a5fab3f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
working with | |
javax.json.jar | |
joda-time.jar | |
jollyday.jar | |
stanford-corenlp-3.5.1-models.jar | |
stanford-corenlp-3.5.1.jar | |
ejml-0.23.jar | |
in build path | |
*/ | |
import java.io.*; | |
import java.util.*; | |
import edu.stanford.nlp.dcoref.*; | |
import edu.stanford.nlp.io.*; | |
import edu.stanford.nlp.ling.*; | |
import edu.stanford.nlp.neural.rnn.*; | |
import edu.stanford.nlp.pipeline.*; | |
import edu.stanford.nlp.semgraph.*; | |
import edu.stanford.nlp.sentiment.*; | |
import edu.stanford.nlp.trees.*; | |
import edu.stanford.nlp.util.*; | |
public class StanfordCoreNlpSentimentDemo { | |
// the text to process. | |
private static final String line = "bad. not so good. ok. pretty neat. amazing"; | |
public static void main(String[] args) { | |
// set up optional output files | |
PrintWriter out = new PrintWriter(System.out); | |
// Create a CoreNLP pipeline with custom properties | |
Properties props = new Properties(); | |
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment"); | |
StanfordCoreNLP pipeline = new StanfordCoreNLP(props); | |
// Initialize an Annotation with some text to be annotated. The text is | |
// the string LINE | |
Annotation annotation = new Annotation(line); | |
// run all the selected Annotators on this text | |
pipeline.annotate(annotation); | |
// print the results | |
pipeline.prettyPrint(annotation, out); | |
// Access the Annotation in code | |
// The toString() method on an Annotation just prints the text of the | |
// Annotation | |
// But you can see what is in it with other methods like | |
// toShorterString() | |
out.println(); | |
out.println("The top level annotation"); | |
out.println(annotation.toShorterString()); | |
// An Annotation is a Map and you can get and use the various analyses | |
// individually. | |
// For instance, this gets the parse tree of the sentences in the | |
// text. | |
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.AnnotatedTree.class); | |
int sentiment = RNNCoreAnnotations.getPredictedClass(tree); | |
String sentimentName = sentence.get(SentimentCoreAnnotations.ClassName.class); | |
out.println(); | |
out.println("The sentence is:"); | |
out.println(sentence.toShorterString()); | |
out.println(); | |
out.println("Sentiment of \n> \""+sentence.get(CoreAnnotations.TextAnnotation.class)+"\"\nis: " + sentiment+" (i.e., "+sentimentName+")"); | |
out.println(); | |
} | |
} | |
IOUtils.closeIgnoringExceptions(out); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
working with | |
javax.json.jar | |
joda-time.jar | |
jollyday.jar | |
stanford-corenlp-3.5.1-models.jar | |
stanford-corenlp-3.5.1.jar | |
ejml-0.23.jar | |
in build path | |
*/ | |
import java.io.*; | |
import java.util.*; | |
import edu.stanford.nlp.dcoref.*; | |
import edu.stanford.nlp.io.*; | |
import edu.stanford.nlp.ling.*; | |
import edu.stanford.nlp.neural.rnn.*; | |
import edu.stanford.nlp.pipeline.*; | |
import edu.stanford.nlp.semgraph.*; | |
import edu.stanford.nlp.sentiment.*; | |
import edu.stanford.nlp.trees.*; | |
import edu.stanford.nlp.util.*; | |
import processing.core.*; | |
public class StanfordCoreNLPSentimentProcessingDemo extends PApplet { | |
public void setup() { | |
size(200, 200); | |
background(0); | |
String line = "bad. not so good. ok. pretty neat. amazing"; | |
sentiment(line); | |
} | |
public void sentiment(String l) { | |
// set up optional output files | |
PrintWriter out = new PrintWriter(System.out); | |
// Create a CoreNLP pipeline with custom properties | |
Properties props = new Properties(); | |
props.setProperty("annotators", "tokenize, ssplit, parse, sentiment"); | |
StanfordCoreNLP pipeline = new StanfordCoreNLP(props); | |
// Initialize an Annotation with some text to be annotated. | |
// The text is the String line | |
Annotation annotation = new Annotation(l); | |
// run all the selected Annotators on this text | |
pipeline.annotate(annotation); | |
// print the results | |
pipeline.prettyPrint(annotation, out); | |
// Access the Annotation in code | |
// The toString() method on an Annotation just prints the text of the | |
// Annotation | |
// But you can see what is in it with other methods like | |
// toShorterString() | |
out.println(); | |
out.println("The top level annotation"); | |
out.println(annotation.toShorterString()); | |
// An Annotation is a Map and you can get and use the various analyses | |
// individually. | |
// For instance, this gets the parse tree of the sentences in the | |
// text. | |
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.AnnotatedTree.class); | |
int sentiment = RNNCoreAnnotations.getPredictedClass(tree); | |
String sentimentName = sentence.get(SentimentCoreAnnotations.ClassName.class); | |
out.println(); | |
out.println("The sentence is:"); | |
out.println(sentence.toShorterString()); | |
out.println(); | |
out.println("Sentiment of \n> \""+sentence.get(CoreAnnotations.TextAnnotation.class)+"\"\nis: " + sentiment+" (i.e., "+sentimentName+")"); | |
out.println(); | |
} | |
} | |
IOUtils.closeIgnoringExceptions(out); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment