0
点赞
收藏
分享

微信扫一扫

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


测试环境:

mac

python3.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统计词频并绘制统计图_代码示例



举报

相关推荐

0 条评论