如何在Stanford CoreNLP中获取短语标签? [英] How to get phrase tags in Stanford CoreNLP?

查看:345
本文介绍了如何在Stanford CoreNLP中获取短语标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想获得与每个单词相对应的词组标签,该如何获得?

If I want to get phrase tags corresponding each word, how to I get this?

例如:

在这句话中,

我的狗也喜欢吃香肠.

My dog also likes eating sausage.

我可以在Stanford NLP中获得一个解析树,例如

I can get a parse tree in Stanford NLP such as

(ROOT (S (NP (PRP$ My) (NN dog)) (ADVP (RB also)) (VP (VBZ likes) (NP (JJ eating) (NN sausage))) (. .)))

在上述情况下,我想获取与每个单词相对应的词组标签

In the above situtation, I want to get phrase tags corresponding each word like

(My - NP), (dog - NP), (also - ADVP), (likes - VP), ...

有什么方法可以对短语标签进行这种简单提取?

Is there any method for this simple extraction for phrase tags?

请帮助我.

推荐答案

//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()));
          }
   }
}

该代码尚未经过全面测试,但我认为它可以满足您的需求.而且,我没有写任何关于递归访问子树的内容,但我相信您应该能够做到这一点.

The code is not fully tested but I think it roughly do what you need. And what's more is I didn't write anything about recursively visit the subtrees but I believe you should be able to do that.

这篇关于如何在Stanford CoreNLP中获取短语标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆