Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, ner, parse, dcoref");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
String test = "Stanford University is located in California. It is a great university.";
Annotation doc = new Annotation(test);
// run all Annotators on this text
pipeline.annotate(doc);
List<CoreMap> sentences = doc.get(SentencesAnnotation.class);
for(CoreMap sentence: sentences) {
// traversing the words in the current sentence
// a CoreLabel is a CoreMap with additional token-specific methods
System.out.println("Sentence:" + sentence.toString());
for (CoreLabel token: sentence.get(TokensAnnotation.class))
{
String word = token.get(TextAnnotation.class);
String lemma = token.get(CoreAnnotations.LemmaAnnotation.class);
System.out.println("word:" + word);
System.out.println("lemma:" + lemma);
}
// this is the parse tree of the current sentence
Tree tree = sentence.get(TreeAnnotation.class);
// this is the Stanford dependency graph of the current sentence
SemanticGraph dependencies = sentence.get(CollapsedCCProcessedDependenciesAnnotation.class);
}
A minimal example for creating a StanfordNLP Parser
最新推荐文章于 2022-07-22 19:14:00 发布