Skip to content

Instantly share code, notes, and snippets.

@jonnybest
Created February 27, 2014 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonnybest/9256282 to your computer and use it in GitHub Desktop.
Save jonnybest/9256282 to your computer and use it in GitHub Desktop.
public void checkAction() {
String text = "The penguin jumps once.";
edu.stanford.nlp.pipeline.Annotation annotationDocument = new edu.stanford.nlp.pipeline.Annotation(text);
edu.stanford.nlp.pipeline.StanfordCoreNLP classificationsPipeline;
// creates a StanfordCoreNLP object, with POS tagging,
// lemmatization,
// NER, parsing, and coreference resolution
Properties pipelineConfiguration = new Properties();
// alternative: wsj-bidirectional
try {
pipelineConfiguration.put(
"pos.model",
"edu/stanford/nlp/models/pos-tagger/wsj-bidirectional/wsj-0-18-bidirectional-distsim.tagger");
} catch (Exception e) {
e.printStackTrace();
}
// adding our own annotator property
pipelineConfiguration.put("customAnnotatorClass.sdclassifier",
"edu.kit.ipd.alicenlp.ivan.analyzers.StaticDynamicClassifier");
// adding our declaration finder
pipelineConfiguration.put("customAnnotatorClass.declarations",
"edu.kit.ipd.alicenlp.ivan.analyzers.DeclarationPositionFinder");
// configure pipeline
pipelineConfiguration.put(
"annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref, declarations, sdclassifier");
classificationsPipeline = new edu.stanford.nlp.pipeline.StanfordCoreNLP(pipelineConfiguration);
// run pipeline
classificationsPipeline.annotate(annotationDocument);
// get the sentences from the document
List<edu.stanford.nlp.util.CoreMap> allSentences = annotationDocument.get(edu.stanford.nlp.ling.CoreAnnotations.SentencesAnnotation.class);
// retrieve results for the first sentence
edu.stanford.nlp.util.CoreMap firstSentence = allSentences.get(0);
// retrieve the classification for the first sentence
edu.kit.ipd.alicenlp.ivan.analyzers.IvanAnalyzer.Classification myClassification = firstSentence.get(edu.kit.ipd.alicenlp.ivan.data.IvanAnnotations.SentenceClassificationAnnotation.class);
boolean compare = myClassification == edu.kit.ipd.alicenlp.ivan.analyzers.IvanAnalyzer.Classification.ActionDescription;
System.out.println("This sentence is classified as " + (compare ? "an action." : "something else."));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment