国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開發(fā)設(shè)計(jì) > 正文

使用python的wordcloud包實(shí)現(xiàn)中文標(biāo)簽云制作

2019-11-14 11:24:30
字體:
供稿:網(wǎng)友

一、前言

數(shù)據(jù)分析報(bào)告之中文標(biāo)簽云

在我的前一篇博客(python爬蟲爬取拉勾網(wǎng)職業(yè)信息)中,獲得了一份格式化的職業(yè)信息數(shù)據(jù),不清楚的可以看看我的上一篇博客。

python制作標(biāo)簽云的包有幾個(gè),我這里選用了Wordcloud,直接pip install 即可。

二、實(shí)戰(zhàn)

1、統(tǒng)計(jì)詞頻數(shù)

前面我們用爬蟲已經(jīng)抓取了一份格式化的數(shù)據(jù),要制作標(biāo)簽云,要先把各個(gè)公司職位優(yōu)勢(shì)的詞頻數(shù)統(tǒng)計(jì)出來,代碼如下:

def wordCount(filename):    '''    簡(jiǎn)單計(jì)算詞頻的函數(shù)    :param filename: 文件名    :return: 詞頻    '''    wordCount = {}    file = open(filename,'r')    while True:        line = file.readline()        if line:            wordlist = line.split(',[')            if len(wordlist) == 1: continue            wordlist = wordlist[1].split('],')[0].split(',')            for word in wordlist:                word = word.replace(' ', '').replace("'", '')                if word in wordCount.keys():                    wordCount[word] = wordCount[word]+1                else:                    wordCount[word] = 1        else:            break    return [(k, wordCount[k]) for k in wordCount.keys()]注意,上述代碼只用于特定格式下,即爬蟲抓取回來的職位信息中公司優(yōu)勢(shì)的詞頻統(tǒng)計(jì)

2、生成標(biāo)簽云

def generateCloud(filename,imagename,cloudname,fontname):    '''    生成標(biāo)簽云的函數(shù)    '''    coloring = imread(imagename)             # 讀取背景圖片    wc = WordCloud(background_color="white", # 背景顏色max_words=2000,# 詞云顯示的最大詞數(shù)                   mask=coloring,            # 設(shè)置背景圖片                   stopwords=STOPWORDS,      # 停止詞                   font_path=fontname,       # 兼容中文字體                   max_font_size=150)        # 字體最大值    #計(jì)算好詞頻后使用generate_from_frequencies函數(shù)生成詞云    #txtFreq例子為[('詞a', 100),('詞b', 90),('詞c', 80)]    txtFreq = wordCount(filename)    wc.generate_from_frequencies(txtFreq)    # 生成圖片    plt.imshow(wc)    plt.axis("off")    # 繪制詞云    plt.figure()    # 保存詞云    wc.to_file(cloudname)這里需要注意的是,fontname這個(gè)參數(shù),在Ubuntu系統(tǒng)下,生成中文標(biāo)簽云默認(rèn)字體可能會(huì)不兼容,所以要重新指定兼容的字體,直接在window系統(tǒng)下復(fù)制微軟雅黑(msyh,tff)字體過來即可。

3、運(yùn)行

if __name__ == '__main__':    d = path.dirname(__file__)              # 獲取當(dāng)前文件路徑    fontname = path.join(d, 'msyh.ttf')     # 中文字體路徑    filename = path.join(d, '廣州.txt')      # txt文件路徑    imagename = path.join(d, "circle.jpg")  # 背景圖片路徑    cloudname = path.join(d, "cloud.png")   # 標(biāo)簽云路徑    generateCloud(filename, imagename, cloudname, fontname)三、結(jié)果

1、選用的背景圖片--circle.jpg

2、生成的標(biāo)簽云--cloud.png

3、到這里就大工告成了,具體代碼可以在我的github上找到:https://github.com/lpty


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 汪清县| 西和县| 磐石市| 南乐县| 加查县| 万年县| 清丰县| 甘孜县| 辽中县| 光山县| 高要市| 五寨县| 大城县| 延寿县| 乌拉特中旗| 乌鲁木齐县| 龙游县| 林甸县| 新竹市| 中牟县| 靖安县| 巴彦淖尔市| 彝良县| 双牌县| 微博| 常山县| 渑池县| 四平市| 南宁市| 南安市| 当雄县| 廉江市| 金堂县| 抚州市| 承德市| 得荣县| 元谋县| 庆城县| 临海市| 建湖县| 铜梁县|