我正在使用Stanford CoreNLP来获取解析树,并且需要为特定的POS语音标记提取所有节点及其子节点。例如,删除作为SBAR子元素的所有S标记,为每个S标记创建单独的子树,删除此S以及S的所有子元素。 我可以在SBAR中提取S标签,但不知道如何修剪原始树以删除作为SBAR子元素的S标记。我想我需要使用过滤器和修剪,但不知道如何使用条件。 我的代码将提取所有S标记,但我想删除一个S,如果它是SBAR的子代,并且它已被添加到我的树列表中。
还有更有效的方法来做到这一点。
public class test {
public static void main(String[] args) {
String Text= new String("I think that he wants a big Car");
StanfordCoreNLP pipeline = getPipeline();
getAnnotation(pipeline, Text);
}
private static StanfordCoreNLP getPipeline(){
// creates a StanfordCoreNLP object, with POS tagging, lemmatization, parsing
Properties props = new Properties();
props.put("annotators", "tokenize, ssplit, pos, lemma, parse");
StanfordCoreNLP pipeline = new StanfordCoreNLP(props);
return pipeline;
}
private static void getAnnotation(StanfordCoreNLP pipeline, String sentence ){
Annotation document = new Annotation(sentence);
pipeline.annotate(document);
List<CoreMap> annotations = document.get(SentencesAnnotation.class);
ArrayList<Tree> list_trees = new ArrayList<Tree>();
Tree tree=null;
Tree PrunedTree ;
Filter<Tree> f = new Filter<Tree>() { public boolean accept(Tree t) { return !
t.label().value().equals("S"); } };
for(CoreMap annotation: annotations) {
// this is the parse tree of the current sentence
tree = annotation.get(TreeAnnotation.class);
for (Tree subtree : tree) {
if (subtree.label().value().equals("SBAR")) {
for (Tree sbartrees : subtree)
{
if (sbartrees.label().value().equals("S"))
list_trees.add(sbartrees);
}
}
}
}
PrunedTree=tree.prune(f);
}
}