Python + wordcloud + jieba 十分钟学会生成中文词云
生活随笔
收集整理的這篇文章主要介紹了
Python + wordcloud + jieba 十分钟学会生成中文词云
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前述
本文需要的兩個Python類庫
jieba:中文分詞分詞工具
wordcloud:Python下的詞云生成工具
寫作本篇文章用時一個小時半,閱讀需要十分鐘,讀完該文章后你將學(xué)會如何將任意中文文本生成詞云
Python詞云 wordcloud 十五分鐘入門與進階
Python中文分詞 jieba 十五分鐘入門與進階
代碼組成簡介##
如果你想用該代碼生成英文詞云,那么你需要將isCN參數(shù)設(shè)置為0,并且提供英文的停用詞表,但是我更推薦你使用Python詞云 worldcloud 十五分鐘入門與進階這篇文章中的代碼,因為它更簡潔,更有利于使用’
##Use the code by comment ##
關(guān)于該程序的使用,你可以直接讀注釋在數(shù)分鐘內(nèi)學(xué)會如何使用它
# - * - coding: utf - 8 -*- # # 作者:田豐(FontTian) # 創(chuàng)建時間:'2017/5/23' # 郵箱:fonttian@163.com # CSDN:http://blog.csdn.net/fontthrone from os import path from scipy.misc import imread import matplotlib.pyplot as plt import jieba # jieba.load_userdict("txt\userdict.txt") # 添加用戶詞庫為主詞典,原詞典變?yōu)榉侵髟~典 from wordcloud import WordCloud, ImageColorGenerator# 獲取當(dāng)前文件路徑 # __file__ 為當(dāng)前文件, 在ide中運行此行會報錯,可改為 # d = path.dirname('.') d = path.dirname(__file__)stopwords = {} isCN = 1 #默認(rèn)啟用中文分詞 back_coloring_path = "img/lz1.jpg" # 設(shè)置背景圖片路徑 text_path = 'txt/lz.txt' #設(shè)置要分析的文本路徑 font_path = 'D:\Fonts\simkai.ttf' # 為matplotlib設(shè)置中文字體路徑?jīng)] stopwords_path = 'stopwords\stopwords1893.txt' # 停用詞詞表 imgname1 = "WordCloudDefautColors.png" # 保存的圖片名字1(只按照背景圖片形狀) imgname2 = "WordCloudColorsByImg.png"# 保存的圖片名字2(顏色按照背景圖片顏色布局生成)my_words_list = ['路明非'] # 在結(jié)巴的詞庫中添加新詞back_coloring = imread(path.join(d, back_coloring_path))# 設(shè)置背景圖片# 設(shè)置詞云屬性 wc = WordCloud(font_path=font_path, # 設(shè)置字體background_color="white", # 背景顏色max_words=2000, # 詞云顯示的最大詞數(shù)mask=back_coloring, # 設(shè)置背景圖片max_font_size=100, # 字體最大值random_state=42,width=1000, height=860, margin=2,# 設(shè)置圖片默認(rèn)的大小,但是如果使用背景圖片的話,那么保存的圖片大小將會按照其大小保存,margin為詞語邊緣距離)# 添加自己的詞庫分詞 def add_word(list):for items in list:jieba.add_word(items)add_word(my_words_list)text = open(path.join(d, text_path)).read()def jiebaclearText(text):mywordlist = []seg_list = jieba.cut(text, cut_all=False)liststr="/ ".join(seg_list)f_stop = open(stopwords_path)try:f_stop_text = f_stop.read( )f_stop_text=unicode(f_stop_text,'utf-8')finally:f_stop.close( )f_stop_seg_list=f_stop_text.split('\n')for myword in liststr.split('/'):if not(myword.strip() in f_stop_seg_list) and len(myword.strip())>1:mywordlist.append(myword)return ''.join(mywordlist)if isCN:text = jiebaclearText(text)# 生成詞云, 可以用generate輸入全部文本(wordcloud對中文分詞支持不好,建議啟用中文分詞),也可以我們計算好詞頻后使用generate_from_frequencies函數(shù) wc.generate(text) # wc.generate_from_frequencies(txt_freq) # txt_freq例子為[('詞a', 100),('詞b', 90),('詞c', 80)] # 從背景圖片生成顏色值 image_colors = ImageColorGenerator(back_coloring)plt.figure() # 以下代碼顯示圖片 plt.imshow(wc) plt.axis("off") plt.show() # 繪制詞云# 保存圖片 wc.to_file(path.join(d, imgname1))image_colors = ImageColorGenerator(back_coloring)plt.imshow(wc.recolor(color_func=image_colors)) plt.axis("off") # 繪制背景圖片為顏色的圖片 plt.figure() plt.imshow(back_coloring, cmap=plt.cm.gray) plt.axis("off") plt.show() # 保存圖片 wc.to_file(path.join(d, imgname2))good luck
總結(jié)
以上是生活随笔為你收集整理的Python + wordcloud + jieba 十分钟学会生成中文词云的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python中文分词 jieba 十五分
- 下一篇: 中科院分词系统(NLPIR)JAVA简易