如果我想获取每个单词对应的短语标签,如何获取?
例如 :
在这句话中,
我的狗也喜欢吃香肠。
我可以在斯坦福 NLP 中得到一个解析树,例如
(ROOT (S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ likes) (NP (JJ eating) (NN sausage))) (. .)))
在上面的情况下,我想获得与每个单词对应的短语标签,例如
(My - NP), (dog - NP), (also - ADVP), (likes - VP), ...
有没有什么方法可以简单地提取短语标签?
请帮我。
//I guess this is how you get your parse tree.
Tree tree = sentAnno.get(TreeAnnotation.class);
//The children of a Tree annotation is an array of trees.
Tree[] children = parent.children()
//Check the label of any sub tree to see whether it is what you want (a phrase)
for (Tree child: children){
if (child.value().equals("NP")){// set your rule of defining Phrase here
List<Tree> leaves = child.getLeaves(); //leaves correspond to the tokens
for (Tree leaf : leaves){
List<Word> words = leaf.yieldWords();
for (Word word: words)
System.out.print(String.format("(%s - NP),",word.word()));
}
}
}
该代码尚未经过充分测试,但我认为它大致可以满足您的需要。更重要的是,我没有写任何有关递归访问子树的内容,但我相信您应该能够做到这一点。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)