如何从给定模式的句子中获取所有块。
示例
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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)