【问题标题】:extracting the text from output parse Tree从输出解析树中提取文本
【发布时间】:2012-09-20 14:18:10
【问题描述】:

我是 nlp 的新手,我正在尝试使用 stanford 解析器从文本中提取 (NP) 句子,我想检索文本中被标记的部分 (NP)

如果一个部分被标记(NP)并且它内部的较小部分也被标记(NP)我想取较小的部分。

到目前为止,我设法通过以下方法做我想做的事:

private static ArrayList<Tree> extract(Tree t) 
{
    ArrayList<Tree> wanted = new ArrayList<Tree>();
   if (t.label().value().equals("NP") )
    {
       wanted.add(t);
        for (Tree child : t.children())
        {
            ArrayList<Tree> temp = new ArrayList<Tree>();
            temp=extract(child);
            if(temp.size()>0)
            {
                int o=-1;
                o=wanted.indexOf(t);
                if(o!=-1)
                    wanted.remove(o);
            }
            wanted.addAll(temp);
        }
    }

    else
        for (Tree child : t.children())
            wanted.addAll(extract(child));
    return wanted;
}

这个方法的返回类型是树的列表,当我执行以下操作时:

     LexicalizedParser parser = LexicalizedParser.loadModel();
        x = parser.apply("Who owns club barcelona?");
     outs=extract(x);
    for(int i=0;i<outs.size();i++){System.out.println("tree #"+i+": "+outs.get(i));}

是:

tree #0: (NP (NN club) (NN barcelona))

我希望输出立即为"club barcelona",没有标签,我尝试了.labels(); 属性和.label().value(); 他们返回标签

【问题讨论】:

    标签: java nlp stanford-nlp


    【解决方案1】:

    你可以得到一个子树tr下的单词列表

    tr.yield()
    

    您可以使用 Sentence 中的便捷方法将其转换为 String 形式:

    Sentence.listToString(tr.yield())
    

    您可以像这样做一样走一棵树,但如果您要经常做这种事情,您可能需要查看tregex,它可以更轻松地通过声明性模式找到树中的特定节点,比如下面没有 NP 的 NP。一个巧妙的方法来做你正在寻找的事情是这样的:

    Tree x = lp.apply("Christopher Manning owns club barcelona?");
    TregexPattern NPpattern = TregexPattern.compile("@NP !<< @NP");
    TregexMatcher matcher = NPpattern.matcher(x);
    while (matcher.findNextMatchingNode()) {
      Tree match = matcher.getMatch();
      System.out.println(Sentence.listToString(match.yield()));
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-26
      • 1970-01-01
      • 1970-01-01
      • 2013-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-22
      • 2011-04-04
      相关资源
      最近更新 更多