安装 NLTK 库

// Python 2.x
pip install nltk

// Python 3.x
pip3 install nltk

下载 NLTK 自带文本库

import nltk
nltk.download()

!382FBE66-B477-4F8E-B112-016522C6C9CC.png(http://storage.blog.ikyxxs.com/ec072ca767d94104a32bad8a7463e14e.png)

创建 2-gram 模型

from nltk import FreqDist
from nltk import ngrams
from nltk.book import text6

bigrams = ngrams(text6, 2)
bigramsDist = FreqDist(bigrams)
print(bigramsDist.most_common(10))

参考

《Python网络数据采集》