如何根据模式从句子中获取所有的块。
例如:
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))
如何根据模式从句子中获取所有的块。
例如:
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))
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
nbest_parse(sent, n=None)
函数。