分块是根据单词的性质将相似单词分组在一起的过程。在下面的示例中,我们定义了必须生成块的语法。语法表示在创建块时将遵循的诸如名词和形容词等短语的序列。块的图形输出如下所示。
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()