#!/usr/bin/python3 # -*- coding: utf-8 -*- # coding=utf-8 # 导入wordcloud模块 from wordcloud import WordCloud # 中文分词库 pip install jieba import jieba # 新建一个词云对象,中文需要加载字体(微软雅黑) wc_obj = WordCloud(font_path=r'C:Program FilesMicrosoft OfficerootvfsFontsprivateMSYH.TTC', width=1366, height=1000, background_color='white') # 打开本地文本文件 filepath = r"C:UsersomaidbDesktop小说.txt" with open(filepath, 'rt', encoding='utf8') as f: text = f.read() # jieba中文分词 text_spt = jieba.cut(text, cut_all=False) # jieba返回的使一个生成器,需要用join拼接 text_cn = '/'.join(text_spt) # 执行分词 wc_obj.generate(text_cn) # 输出词云 wc_obj.to_file(r"C:UsersomaidbDesktopciyun.png")
词云展示
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)