How to get phrase tags in Stanford CoreNLP?

假如想象 提交于 2019-12-31 01:50:10

问题


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

(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.


回答1:


//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.



来源:https://stackoverflow.com/questions/14373557/how-to-get-phrase-tags-in-stanford-corenlp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!