与nltk分块

vig*_*gte 4 python nlp chunking nltk

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

NP:{<NN><NN>}
Run Code Online (Sandbox Code Playgroud)

句子标记:

[("money", "NN"), ("market", "NN") ("fund", "NN")]
Run Code Online (Sandbox Code Playgroud)

如果我解析我获得

(S (NP money/NN market/NN) fund/NN)
Run Code Online (Sandbox Code Playgroud)

我想也有另一种选择

(S money/NN (NP market/NN fund/NN))
Run Code Online (Sandbox Code Playgroud)

alv*_*vas 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
Run Code Online (Sandbox Code Playgroud)