Reputation: 553
I'm trying to use the CoreNLP and would like to know if its possible to split the sentence using just the phrase level without going into detail with to the word level.
Basically, I want to analyze a sentence then obtain the phrase tags of it, then obtain the splits into variables.
For example, for a sentence, if it contains the noun phrase(X) and verb phrase (Y), I want to analyze using CoreNLP the X and Y, then obtain X and Y separately into variables.
Any idea on how to do it?
Upvotes: 0
Views: 925
Reputation: 385
the following code gets all NP and VP in the given text:
props.setProperty("annotators", "tokenize, ssplit, pos, lemma, ner, parse");
props.setProperty("ner.model","edu/stanford/nlp/models/ner/english.muc.7class.distsim.crf.ser.gz");
List<mention> NPS = new ArrayList<mention>();
HeadFinder headFinder = null;
if (doc != null && doc.length() > 0) {
edu.stanford.nlp.pipeline.Annotation annotation = pipeline.process(doc);
for (CoreMap sentence : annotation.get(CoreAnnotations.SentencesAnnotation.class)) {
Tree tree = sentence.get(TreeAnnotation.class);
headFinder = new SemanticHeadFinder();
for (Tree subtree : tree) {
if (subtree.label().value().equals("NP")) {
//this is a NP, you can assign it to a variable here
}
if (subtree.label().value().equals("VP")) {
//this is a VP, you can assign it to a variable here
}
}
}
}
Upvotes: -1
Reputation: 8739
There is some sample code in my answer to this question which demonstrates how to access the constituency parse
I provided a sample class called RootFinderExample.java.
How to get the root node in Stanford Parse-Tree?
Here is where the Tree is accessed:
Tree tree = sentence.get(TreeAnnotation.class);
Here is some documentation on the Tree class:
http://nlp.stanford.edu/nlp/javadoc/javanlp/edu/stanford/nlp/trees/Tree.html
Upvotes: 1