gpt4 book ai didi

regex - 如何对 NLP 解析树进行逆向工程以得到原始句子?

转载 作者:行者123 更新时间:2023-12-02 15:17:38 25 4
gpt4 key购买 nike

给定一个解析树(通过 http://nlp.stanford.edu:8080/corenlp/process pretty-print 选项获得)

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

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

最佳答案

您可以使用 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 .

关于regex - 如何对 NLP 解析树进行逆向工程以得到原始句子?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39319547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com