日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

实例使用pyhanlp创建中文词云

發布時間:2025/3/21 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 实例使用pyhanlp创建中文词云 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

使用pyhanlp創建詞云

去年我曾經寫過一篇文章Python + wordcloud + jieba 十分鐘學會用任意中文文本生成詞云(你可能覺得這和wordcloud中官方文檔中的中文詞云的例子代碼很像,不要誤會,那個也是我寫的)

現在我們可以仿照之前的格式在寫一份pyhanlp版本的。

對于wordcloud而言,因為原生支持的英文是自帶空格的,所以我們這里需要的是進行分詞和去停處理,然后將文本變為我們需要的list格式,輸入wordcloud。同時因為文檔中可能有新的詞匯我們之前并沒有發現,所以有必要的話。我們還需要進行添加自定義詞典的代碼。其代碼時非常簡單的。

首先對于添加自定義詞典我們只需要,引用CustomDictionary類即可,代碼如下。

CustomDictionary = JClass("com.hankcs.hanlp.dictionary.CustomDictionary")for word in userdict_list:CustomDictionary.add(word)

不過為了更好的發現新詞我們還是采用一款發現新詞比較好的分詞器,比如默認的維特比,或者CRF。考慮到之前實驗中CRF在默認的命名實體識別條件下,表現并不好,或許維特比才是更好的選擇。

mywordlist = [] HanLP.Config.ShowTermNature = FalseCRFnewSegment = HanLP.newSegment("viterbi")

去停功能同樣十分簡單,我們還是利用之前在分詞中提到的方法,不過這次我們把代碼更pythoic一些

CoreStopWordDictionary = JClass("com.hankcs.hanlp.dictionary.stopword.CoreStopWordDictionary")text_list = CRFnewSegment.seg(text)CoreStopWordDictionary.apply(text_list)fianlText = [i.word for i in text_list]

而是否采用之前文章中的停詞功能則可以自主選擇,而處理為wordcloud所需要的格式,我們則直接采用之前的代碼即可。最終核心部分代碼如下:

CustomDictionary = JClass("com.hankcs.hanlp.dictionary.CustomDictionary")for word in userdict_list:CustomDictionary.add(word)mywordlist = [] HanLP.Config.ShowTermNature = FalseCRFnewSegment = HanLP.newSegment("viterbi")fianlText = []if isUseStopwordsOfHanLP == True:CoreStopWordDictionary = JClass("com.hankcs.hanlp.dictionary.stopword.CoreStopWordDictionary")text_list = CRFnewSegment.seg(text)CoreStopWordDictionary.apply(text_list)fianlText = [i.word for i in text_list]else:fianlText = list(CRFnewSegment.segment(text))liststr = "/ ".join(fianlText)with open(stopwords_path, encoding='utf-8') as f_stop:f_stop_text = f_stop.read()f_stop_seg_list = f_stop_text.splitlines()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)

最終實例

現在我們得到了最終實例,已經可以運行了,關于該部分,你還可以參考我fork的wordcloud項目,還有相比于此處新的變化。那里提供了一個jieba和pyhanlp合并的版本。

# - * - coding: utf - 8 -*- """ create wordcloud with chinese =======================Wordcloud is a very good tools, but if you want to create Chinese wordcloud only wordcloud is not enough. The file shows how to use wordcloud with Chinese. First, you need a Chinese word segmentation library pyhanlp, pyhanlp is One of the most powerful natural language processing libraries in Chinese today, and it's extremely easy to use.You can use 'PIP install pyhanlp'. To install it. Its level of identity of named entity,word segmentation was better than jieba, and has more ways to do it """from os import path from scipy.misc import imread import matplotlib.pyplot as plt from wordcloud import WordCloud, ImageColorGenerator from pyhanlp import * %matplotlib inline# d = path.dirname(__file__) d = "/home/fonttian/Github/word_cloud/examples"stopwords_path = d + '/wc_cn/stopwords_cn_en.txt' # Chinese fonts must be set font_path = d + '/fonts/SourceHanSerif/SourceHanSerifK-Light.otf'# the path to save worldcloud imgname1 = d + '/wc_cn/LuXun.jpg' imgname2 = d + '/wc_cn/LuXun_colored.jpg' # read the mask / color image taken from back_coloring = imread(path.join(d, d + '/wc_cn/LuXun_color.jpg'))# Read the whole text. text = open(path.join(d, d + '/wc_cn/CalltoArms.txt')).read()# userdict_list = ['孔乙己']# The function for processing text with HaanLP def pyhanlp_processing_txt(text,isUseStopwordsOfHanLP = True):CustomDictionary = JClass("com.hankcs.hanlp.dictionary.CustomDictionary")for word in userdict_list:CustomDictionary.add(word)mywordlist = [] HanLP.Config.ShowTermNature = FalseCRFnewSegment = HanLP.newSegment("viterbi")fianlText = []if isUseStopwordsOfHanLP == True:CoreStopWordDictionary = JClass("com.hankcs.hanlp.dictionary.stopword.CoreStopWordDictionary")text_list = CRFnewSegment.seg(text)CoreStopWordDictionary.apply(text_list)fianlText = [i.word for i in text_list]else:fianlText = list(CRFnewSegment.segment(text))liststr = "/ ".join(fianlText)with open(stopwords_path, encoding='utf-8') as f_stop:f_stop_text = f_stop.read()f_stop_seg_list = f_stop_text.splitlines()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)wc = WordCloud(font_path=font_path, background_color="white", max_words=2000, mask=back_coloring,max_font_size=100, random_state=42, width=1000, height=860, margin=2,)pyhanlp_processing_txt = pyhanlp_processing_txt(text,isUseStopwordsOfHanLP = True)wc.generate(pyhanlp_processing_txt)# create coloring from image image_colors_default = ImageColorGenerator(back_coloring)plt.figure() # recolor wordcloud and show plt.imshow(wc, interpolation="bilinear") plt.axis("off") plt.show()# save wordcloud wc.to_file(path.join(d, imgname1))# create coloring from image image_colors_byImg = ImageColorGenerator(back_coloring)# show # we could also give color_func=image_colors directly in the constructor plt.imshow(wc.recolor(color_func=image_colors_byImg), interpolation="bilinear") plt.axis("off") plt.figure() plt.imshow(back_coloring, interpolation="bilinear") plt.axis("off") plt.show()# save wordcloud wc.to_file(path.join(d, imgname2))

總結

以上是生活随笔為你收集整理的实例使用pyhanlp创建中文词云的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 日韩欧美在线观看一区二区三区 | 中文字幕在线观看免费 | 中文字字幕在线中文乱码电影 | 99re色 | 精品视频一二区 | 欧州一区二区 | 一本色道久久综合亚洲精品图片 | aaaaa毛片 | 起碰在线 | 色桃网 | 伊人影院av | 免费看黄色一级片 | 亚洲激情久久久 | 黄色日韩网站 | 久久人人干 | 又大又粗欧美黑人aaaaa片 | 国产偷人视频 | 国产精品久久久久久免费免熟 | 国产乱淫av麻豆国产 | 色啪综合 | 欧美激情一级精品国产 | 国产一区视频在线观看免费 | 色老妹| 成人在线视频免费看 | v99av| 经典三级在线视频 | 性生交大片免费看狂欲 | 一个人看的www片免费高清中文 | 十八岁世界在线观看高清免费韩剧 | www国产精品视频 | 一级片视频在线观看 | 荷兰女人裸体性做爰 | 高潮一区二区三区乱码 | 一区二区三区在线播放视频 | 久久一区av | 亚洲av无码一区二区乱子仑 | 911色| 精品人妻在线视频 | 国产三级一区二区 | 成人一级片在线观看 | 精品一区电影国产 | 依人在线| 丁香花电影在线观看免费高清 | 啪啪免费网站 | 国产精品伦理一区 | 一本高清dvd在线播放 | 欧美成人激情在线 | 51久久久 | 成人毛片一区二区三区 | 欧美福利视频 | 视频在线免费观看 | 少妇xxxx69| 日韩高清一区二区 | 国产精品视频成人 | 女人下面无遮挡 | 国产黄色在线观看 | 午夜黄色一级片 | 欧洲av无码放荡人妇网站 | 亚洲欧美综合精品久久成人 | 日本少妇久久久 | 成人亚洲黄色 | 久久av一区二区三区亚洲 | 国产在线一区二区 | www.五月婷| 国产福利视频网站 | 四虎影视最新网址 | 福利社区一区二区 | 亚洲视频在线观看一区二区三区 | 中文字幕av网站 | 成人毛片100免费观看 | 国产精品毛片一区二区三区 | 亚洲成av人片一区二区梦乃 | 中文字幕精品一区二区三区视频 | 少妇人妻综合久久中文字幕 | mm1313亚洲国产精品无码试看 | 欧美日韩一区电影 | 精品福利在线观看 | 一区二区在线影院 | 久久精品无码人妻 | 日日草夜夜草 | 日韩一级完整毛片 | chinese麻豆新拍video | 迈开腿让我尝尝你的小草莓 | 成人欧美激情 | 三级做爰第一次 | 日韩中文字幕国产 | 激情宗合网 | 欧美又粗又大aaa片 老熟妇仑乱视频一区二区 亚洲妇女体内精汇编 | 男女曰逼视频 | 亚洲乱子伦 | 欧美日韩国产a | 日韩精品在线一区二区 | 午夜久久精品 | 99精品一区二区三区无码吞精 | 欧美成人69 | 好紧好爽再浪一点视频 | 综合色导航| 欧美乱轮视频 | 在线免费成人 |