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

时间:2013-10-17 16:05:13

标签: java nlp stanford-nlp phrase

我将使用Stanford Corenlp 2013找到短语标题。我看到了this thread

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

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

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

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

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

2 个答案:

答案 0 :(得分:8)

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

Stanford CoreNLP套件以

的形式实现了Collins头部探测器启发式和语义头部探测器启发式
  1. CollinsHeadFinder
  2. ModCollinsHeadFinder
  3. SemanticHeadFinder
  4. 您需要的只是实例化三者之一并执行以下操作。

    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<Tree> 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);
        }
    
     }
    

答案 1 :(得分:4)

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

相关问题