1.字頻統(tǒng)計(jì):collections.Counter
(1)指定文本
from collections import Counter
cnt = Counter()
for word in ['red', 'blue', 'red', 'green', 'blue', 'blue']:
????cnt[word] += 1
print(cnt.most_common())
(2)隨機(jī)生成文本
import random
import collections
import string
#大小寫復(fù)合字母
str1=string.ascii_letters
#小寫:str1=string.ascii_lowercase
#大寫:str1=string.ascii_uppercase
strlist=[random.choice(str1) for i in range(100)]
strcount=collections.Counter(strlist)
#輸出詞頻排前10的字母
for key,value in strcount.most_common(10):
????print(key,value)
(3)讀取文本文件
2.詞云圖:WordCloud庫
參考博文:https://www.cnblogs.com/qq21270/p/7695275.html
(1)詞云圖:
(2)設(shè)置其它參數(shù)
【注】參數(shù)說明
①字體:font_path
msyh.ttf 微軟雅黑
msyhbd.ttf 微軟雅黑 粗體
simsun.ttc 宋體
simhei.ttf 黑體
②主體色調(diào):colormap
autumn 從紅色平滑變化到橙色,然后到黃色。
bone 具有較高的藍(lán)色成分的灰度色圖盒粮。該色圖用于對(duì)灰度圖添加電子的視圖食拜。
cool 包含青綠色和品紅色的陰影色共屈。從青綠色平滑變化到品紅色眯杏。
copper 從黑色平滑過渡到亮銅色匾乓。
flag 包含紅录煤、白鳄厌、綠和黑色。
gray 返回線性灰度色圖妈踊。
hot 從黑平滑過度到紅了嚎、橙色和黃色的背景色,然后到白色廊营。
hsv 從紅歪泳,變化到黃、綠露筒、青綠呐伞、品紅,返回到紅慎式。
jet 從藍(lán)到紅伶氢,中間經(jīng)過青綠趟径、黃和橙色。它是hsv色圖的一個(gè)變異癣防。
line 產(chǎn)生由坐標(biāo)軸的ColorOrder屬性產(chǎn)生的顏色以及灰的背景色的色圖蜗巧。
pink 柔和的桃紅色,它提供了灰度圖的深褐色調(diào)著色蕾盯。
prism 重復(fù)這六種顏色:紅惧蛹、橙、黃刑枝、綠香嗓、藍(lán)和紫色。
spring 包含品紅和黃的陰影顏色装畅。
summer 包含綠和黃的陰影顏色靠娱。
white 全白的單色色圖。
winter 包含藍(lán)和綠的陰影色掠兄。