博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python做词云图
阅读量:2045 次
发布时间:2019-04-28

本文共 2243 字,大约阅读时间需要 7 分钟。

python做词云图

#导入需要模块import jiebaimport numpy as np import matplotlib.pyplot as plt from PIL import Image from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator text_road=str(input('请输入文章的路径:'))picture_road=str(input('请输入图片的路径:')) #加载需要分析的文章text = open(text_road,'r',encoding='utf-8').read() #对文章进行分词wordlist_after_jieba = jieba.cut(text, cut_all=False)wl_space_split = " ".join(wordlist_after_jieba) #读取照片通过numpy.array函数将照片等结构数据转化为np-arraymask=np.array(Image.open(picture_road)) #选择屏蔽词,不显示在词云里面stopwords = set(STOPWORDS)#可以加多个屏蔽词stopwords.add("
") #创建词云对象wc = WordCloud( background_color="white", font_path='/Library/Fonts/Arial Unicode.ttf', max_words=1000, # 最多显示词数 mask=mask, stopwords=stopwords, max_font_size=100 # 字体最大值 ) #生成词云wc.generate(text) #从背景图建立颜色方案image_colors =ImageColorGenerator(mask) #将词云颜色设置为背景图方案wc.recolor(color_func=image_colors) #显示词云plt.imshow(wc,interpolation='bilinear') #关闭坐标轴plt.axis("off") #显示图像plt.show() #保存词云wc.to_file('词云图.png')
from wordcloud import WordCloud, STOPWORDSfrom imageio import imreadfrom sklearn.feature_extraction.text import CountVectorizerimport jiebaimport csv# 获取文章内容with open("caifu.txt") as f:    contents = f.read()print("contents变量的类型:", type(contents))# 使用jieba分词,获取词的列表contents_cut = jieba.cut(contents)print("contents_cut变量的类型:", type(contents_cut))contents_list = " ".join(contents_cut)print("contents_list变量的类型:", type(contents_list))# 制作词云图,collocations避免词云图中词的重复,mask定义词云图的形状,图片要有背景色wc = WordCloud(stopwords=STOPWORDS.add("一个"), collocations=False,                background_color="white",                font_path=r"C:\Windows\Fonts\simhei.ttf",               width=400, height=300, random_state=42,                mask=imread('axis.png',pilmode="RGB"))wc.generate(contents_list)wc.to_file("ciyun.png")# 使用CountVectorizer统计词频cv = CountVectorizer()contents_count = cv.fit_transform([contents_list])# 词有哪些list1 = cv.get_feature_names()# 词的频率list2 = contents_count.toarray().tolist()[0] # 将词与频率一一对应contents_dict = dict(zip(list1, list2))# 输出csv文件,newline="",解决输出的csv隔行问题with open("caifu_output.csv", 'w', newline="") as f:    writer = csv.writer(f)    for key, value in contents_dict.items():        writer.writerow([key, value])

你的时间用在哪里决定你成为一个什么样的人。

转载地址:http://tnaof.baihongyu.com/

你可能感兴趣的文章
Leetcode C++《热题 Hot 100-54》438. 找到字符串中所有字母异位词
查看>>
Leetcode C++《热题 Hot 100-55》494. 目标和
查看>>
Leetcode C++《热题 Hot 100-56》300. 最长上升子序列
查看>>
Leetcode C++《热题 Hot 100-57》139. 单词拆分
查看>>
Leetcode C++《热题 Hot 100-58》560. 和为K的子数组
查看>>
Leetcode C++《热题 Hot 100-59》416. 分割等和子集
查看>>
Leetcode C++《热题 Hot 100-60》146. LRU缓存机制
查看>>
Leetcode C++《热题 Hot 100-61》200. 岛屿数量
查看>>
Leetcode C++《热题 Hot 100-62》621. 任务调度器
查看>>
Leetcode C++《热题 Hot 100-63》394. 字符串解码
查看>>
Leetcode C++《热题 Hot 100-64》142. 环形链表 II
查看>>
Leetcode C++《热题 Hot 100-65》207. 课程表
查看>>
Leetcode C++《热题 Hot 100-66》309. 最佳买卖股票时机含冷冻期
查看>>
Leetcode C++《热题 Hot 100-67》279. 完全平方数
查看>>
Leetcode C++《热题 Hot 100-68》337. 打家劫舍 III
查看>>
Leetcode C++《热题 Hot 100-69》647. 回文子串
查看>>
Leetcode C++ 《第22场双周赛-1》 5348.两个数组间的距离值
查看>>
[Kick Start 2020] Round A 1.Allocation
查看>>
[Kick Start 2020] Round A 2.Plates
查看>>
Leetcode C++ 《第181场周赛-1》 5364. 按既定顺序创建目标数组
查看>>