用nltk分块

如何从给定模式的句子中获得所有块。 为例

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

我觉得你的问题是如何取得n一个句子的最可能的解析。 我对吗? 如果是,请参阅2.0文档中的nbest_parse(sent, n=None)函数。


@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
链接地址: http://www.djcxy.com/p/91715.html

上一篇: Chunking with nltk

下一篇: std::vector preallocation (size n, capacity n + 2)