简述
分块是根据单词的性质将相似的单词组合在一起的过程。在下面的示例中,我们定义了必须生成块的语法。语法建议短语的顺序,如名词和形容词等,在创建块时将遵循这些顺序。块的图形输出如下所示。
import nltk
sentence = [("The", "DT"), ("small", "JJ"), ("red", "JJ"),("flower", "NN"),
("flew", "VBD"), ("through", "IN"), ("the", "DT"), ("window", "NN")]
grammar = "NP: {?*}"
cp = nltk.RegexpParser(grammar)
result = cp.parse(sentence)
print(result)
result.draw()
当我们运行上述程序时,我们得到以下输出 -
改变语法,我们得到不同的输出,如下所示。
import nltk
sentence = [("The", "DT"), ("small", "JJ"), ("red", "JJ"),("flower", "NN"),
("flew", "VBD"), ("through", "IN"), ("the", "DT"), ("window", "NN")]
grammar = "NP: {?*}"
chunkprofile = nltk.RegexpParser(grammar)
result = chunkprofile.parse(sentence)
print(result)
result.draw()
当我们运行上述程序时,我们得到以下输出 -