简体   繁体   English

如何在斯坦福 CoreNLP 中获取短语标签?

[英]How to get phrase tags in Stanford CoreNLP?

If I want to get phrase tags corresponding each word, how to I get this?如果我想获得与每个单词对应的短语标签,我该如何获得?

For example :例如 :

In this sentence,在这句话中,

My dog also likes eating sausage.我的狗也喜欢吃香肠。

I can get a parse tree in Stanford NLP such as我可以在斯坦福 NLP 中得到一个解析树,例如

(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?有没有什么方法可以简单地提取短语标签?

Please help me.请帮我。

//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.更重要的是我没有写任何关于递归访问子树的内容,但我相信你应该能够做到这一点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM