【问题标题】:How to reverse engineer an NLP parse tree to arrive at the original sentence?如何对 NLP 解析树进行逆向工程以得出原始句子?
【发布时间】:2016-09-04 17:20:30
【问题描述】:

给定一个解析树(通过http://nlp.stanford.edu:8080/corenlp/process 漂亮的打印选项获得)

(ROOT (S (NP (PRP You)) (VP (MD could) (VP (VB say) (SBAR (IN that) (S (NP (PRP they)) (ADVP (RB regularly)) (VP (VB catch) (NP (NP (DT a) (NN shower)) (, ,) (SBAR (WHNP (WDT which)) (S (VP (VBZ adds) (PP (TO to) (NP (NP (PRP$ their) (NN exhilaration)) (CC and) (NP (FW joie) (FW de) (FW vivre))))))))))))) (. .)))

我怎样才能得出原句?

You could say that they regularly catch a shower, which adds to their exhilaration and joie de vivre.

我正在考虑使用一些正则表达式魔法,但我想知道斯坦福 NLP 是否有内置功能来完成这项任务?

【问题讨论】:

    标签: regex nlp nltk stanford-nlp


    【解决方案1】:

    您可以使用Tree.fromstring() 将字符串转换为Tree。现在,您可以使用Tree.leaves() 方法从树中获取所有令牌。

    代码:

    from nltk import Tree
    
    parse_str = "(ROOT (S (NP (PRP You)) (VP (MD could) (VP (VB say) (SBAR (IN that) (S (NP (PRP they)) (ADVP (RB regularly)) (VP (VB catch) (NP (NP (DT a) (NN shower)) (, ,) (SBAR (WHNP (WDT which)) (S (VP (VBZ adds) (PP (TO to) (NP (NP (PRP$ their) (NN exhilaration)) (CC and) (NP (FW joie) (FW de) (FW vivre))))))))))))) (. .)))"
    
    t = Tree.fromstring(parse_str)
    
    #print t.leaves()
    print ' '.join(t.leaves())
    

    输出:

    You could say that they regularly catch a shower , which adds to their exhilaration and joie de vivre .
    

    【讨论】:

    • Thx @RAVI 看起来输出被标记化了?我们也可以得到一个非标记化的版本吗?
    • 由于解析输出不存储任何未标记的句子信息,因此无法获得该输出。
    • 但是根据你的要求,你可以根据toekn的字符串写一些规则来加空格或者不加空格。例如, ”,” ”。”等
    【解决方案2】:

    通常正则表达式不足以解析树状结构。如果您想手动解析,最简单的方法可能是编写一个小型递归下降解析器,这将涉及编写探索每个节点的递归函数,其中每个叶节点的函数将附加到结果的文本中。

    幸运的是,我认为您不需要进行任何解析,因为斯坦福 NLP 有几种方法可以将树转换为 List 的叶子,还有几种方法可以将 List 的叶子转换为 Sentence .请参阅theselinks。特别是Treeyield 方法可以派上用场。

    我会尝试Sentence.listToOriginalTextString(tree.yield())。如果这不起作用,您可以尝试Sentence.listToOriginalTextString(taggedLabeledYield())。类型签名有点尴尬,所以我不是 100% 有信心。

    【讨论】:

      猜你喜欢
      • 2014-02-22
      • 2011-05-09
      • 2016-04-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-30
      • 2015-04-23
      • 2012-01-16
      • 1970-01-01
      相关资源
      最近更新 更多