类似生成各种形状的云图,我也会啦!
其实也就是针对新手吧,我估计老手分分钟,哈哈哈,我怕我忘了,随手记录下~
四个步骤吧:
- 读文件
- 分词
- 生成词云图
- 显示词云图
1. 读文件
使用了下 codecs 库,读取内容更方便。
import codecs
def get_file_content(filePath):
with codecs.open(filePath, 'r', 'utf-8') as f:
txt = f.read()
return txt
2. 分词
jieba
是一个分词库,可以将一段文本分割成词语。cut 是将文本精确切分开,不存在冗余词语。比如颜酱是一个厉害的厨师,会变成['颜酱', '厉害', '厨师']
。 Counter
是一个计数器,可以统计词语出现的次数,most_common 是取出最常用的词语。
import jieba
from collections import Counter
def get_words(txt):
# 先分词,得到词语数组
seg_list = jieba.cut(txt)
# 开始计数
c = Counter()
for x in seg_list:
if len(x)>1 and x != '\r\n':
c[x] += 1
word_list = []
print('常用词频度统计结果')
# 统计前99个词
for (k,v) in c.most_common(99):
word_list.append(str(k))
# 将词语生成文本文件
file = open("./dist/out_words.txt", 'w').close()
with open("./dist/out_words.txt",'a+',encoding='utf-8') as writeFile:
for (k,v) in c.most_common(99): # 统计前99个词
writeFile.write(str(k))
writeFile.write(str(v))
writeFile.write('\n')
print(word_list)
# ['发展','平安']
return word_list