使用Stanford Parser(CoreNLP)查找短语头

我将使用Stanford Corenlp 2013找到短语标题。 我看到了这个post 。

但是,答案对我来说并不清楚,我无法添加任何评论来继续该线程。 所以,我很抱歉重复。

我现在所拥有的是一个句子的解析树(使用Stanford Corenlp)(我也尝试过由Stanford Corenlp创建的CONLL格式)。 而我所需要的只是名词短语的头部。

我不知道如何使用依赖关系和解析树来提取名词短语的头部。 我所知道的是,如果我有nsubj (x, y) ,y就是主题的头部。 如果我有dobj(x,y) ,则y是直接对象的头部。 f我有iobj(x,y) ,y是间接对象的头部。

但是,我不确定这种方式是否是找到所有短语头的正确方法。 如果是,我应该添加哪些规则来获取所有名词短语?

也许,值得一提的是,我需要在java代码中使用名词短语的头部。

由于我不能评论Chaitanya给出的答案,所以在这里补充更多答案。

斯坦福CoreNLP套件实现了Collins头部探测器启发式和语义头部探测器启发式的forms

  1. CollinsHeadFinder
  2. ModCollinsHeadFinder
  3. SemanticHeadFinder

您只需要实例化三个中的一个并执行以下操作。

 Tree tree = sentence.get(TreeCoreAnnotations.TreeAnnotation.class); headFinder.determineHead(tree).pennPrint(out); 

您可以遍历树的节点并在需要的地方确定头部单词。

PS:我的答案基于截至20140104年发布的StanfordCoreNLP套件。

这是一个简单的dfs,可以让你为一个句子中的所有名词短语提取头部单词

 public static void dfs(Tree node, Tree parent, HeadFinder headFinder) { if (node == null || node.isLeaf()) { return; } //if node is a NP - Get the terminal nodes to get the words in the NP if(node.value().equals("NP") ) { System.out.println(" Noun Phrase is "); List leaves = node.getLeaves(); for(Tree leaf : leaves) { System.out.print(leaf.toString()+" "); } System.out.println(); System.out.println(" Head string is "); System.out.println(node.headTerminal(headFinder, parent)); } for(Tree child : node.children()) { dfs(child, node, headFinder); } } 

您可以提取感兴趣的短语,使其成为类树的对象。然后,您可以从实现接口HeadFinder的任何类中使用determineHead(Tree t)方法。