使用nltk进行分块处理

4 投票
2 回答
3739 浏览
提问于 2025-04-17 14:43

我该如何根据一个模式从句子中获取所有的片段呢?

举个例子:

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

2 个回答

6

@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
1

我觉得你的问题是关于如何获取一个句子最可能的 n 种解析方式。对吗?如果是的话,可以看看 nbest_parse(sent, n=None) 这个函数,详细信息可以在 2.0 文档 中找到。

撰写回答