天天看点

Python:使用nltk统计词频并绘制统计图

测试环境:

mac

python

3.6.5

安装

pip install nltk      

代码示例

# -*- coding: utf-8 -*-

from nltk import FreqDist
from matplotlib import rcParams

# matplotlib 设置中文字体
rcParams["font.family"] = "STHeiti"
rcParams["font.size"] = 8


words = ["你好", "你好", "我好", "我还有"]

freq = FreqDist(words)
print(freq.most_common(1))  # [('你好', 2)]
print(freq.freq("你好"))     # 频率 0.5
print(freq["你好"])          # 次数 2

freq.tabulate()              # 频率分布表

freq.plot()                  # 频率分布图

      
Python:使用nltk统计词频并绘制统计图