gpt4 book ai didi

python - 与 nltk 分块

转载 作者:太空狗 更新时间:2023-10-30 02:21:41 28 4
gpt4 key购买 nike

如何从给定模式的句子中获取所有 block 。例子

NP:{<NN><NN>}

标记的句子:

[("money", "NN"), ("market", "NN") ("fund", "NN")]

如果我解析我得到

(S (NP money/NN market/NN) fund/NN)

我也想有另一种选择是

(S money/NN (NP market/NN fund/NN))

最佳答案

@mbatchkarov 对 nbest_parse 文档的看法是正确的。为了代码示例,请参阅:

import nltk
# Define the cfg grammar.
grammar = nltk.parse_cfg("""
S -> NP
S -> NN NP
S -> NP NN
NP -> NN NN
NN -> 'market'
NN -> 'money'
NN -> 'fund'
""")

# Make your string into a list of tokens.
sentence = "money market fund".split(" ")

# Load the grammar into the ChartParser.
cp = nltk.ChartParser(grammar)

# Generate and print the nbest_parse from the grammar given the sentence tokens.
for tree in cp.nbest_parse(sentence):
print tree

关于python - 与 nltk 分块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14692489/

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