词频统计小案例
import jieba
file = open(r'C:UsersAdministratorDesktopday01琵琶行','r',encoding='utf8')
data = file.read()
data_jieba = jieba.lcut(data)
count_dict = {}
for word in data_jieba:
if len(word) == 1:
continue
if word in count_dict:
count_dict[word] += 1
else:
count_dict[word] = 1
def func(i):
return i[1]
data_list=list(count_dict.items())
data_list.sort(key=func)
data_list.reverse()
for i in data_list[0:4]:
print(f'{i[0]:^8}{i[1]:^5}')
词云小案例
import wordcloud
from imageio import imread
mask = imread(r'C:UsersAdministratorDesktopday01无标题.png')
file_write = open(r'C:UsersAdministratorDesktopday01琵琶行','r',encoding='utf8')
data = file_write.read()
word = wordcloud.WordCloud(font_path=r'C:WindowsFontssimfang',mask=mask,width=500,height=500,background_color="white")
word.generate(data)
word.to_file('myimage.png')