gpt4 book ai didi

parsing - 如何使用 nltk 或 spacy 从带括号的解析字符串中获取解析 NLP 树对象?

转载 作者:行者123 更新时间:2023-12-02 00:36:20 26 4
gpt4 key购买 nike

我有一句话“你可以说他们经常洗个澡,这增加了他们的兴奋和生活乐趣。”我无法像以下示例:

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

我想复制这个问题的解决方案 https://stackoverflow.com/a/39320379但我有一个字符串句子而不是 NLP 树。

顺便说一句,我正在使用 python 3

最佳答案

使用 Tree.fromstring() 方法:

>>> from nltk import Tree
>>> parse = Tree.fromstring('(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))))))))))))) (. .)))')

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

>>> parse.pretty_print()
ROOT
|
S
______________________________________________________|_____________________________________________________________
| VP |
| ____|___ |
| | VP |
| | ___|____ |
| | | SBAR |
| | | ____|_______ |
| | | | S |
| | | | _______|____________ |
| | | | | | VP |
| | | | | | ____|______________ |
| | | | | | | NP |
| | | | | | | __________|__________ |
| | | | | | | | | SBAR |
| | | | | | | | | ____|____ |
| | | | | | | | | | S |
| | | | | | | | | | | |
| | | | | | | | | | VP |
| | | | | | | | | | ____|____ |
| | | | | | | | | | | PP |
| | | | | | | | | | | ____|_____________________ |
| | | | | | | | | | | | NP |
| | | | | | | | | | | | ________________|________ |
NP | | | NP ADVP | NP | WHNP | | NP | NP |
| | | | | | | ___|____ | | | | ____|_______ | ____|____ |
PRP MD VB IN PRP RB VB DT NN , WDT VBZ TO PRP$ NN CC FW FW FW .
| | | | | | | | | | | | | | | | | | | |
You could say that they regularly catch a shower , which adds to their exhilaration and joie de vivre .

关于parsing - 如何使用 nltk 或 spacy 从带括号的解析字符串中获取解析 NLP 树对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371180/

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