WordCloud词云库实战(二)

简介: WordCloud词云库实战(二)

写在前面

昨天我们讲了英文词云绘制,今天我们来试试中文词云,首先我们需要一本道德经



读取文件


#-*- coding:utf-8 -*-
with open('C:\\Users\\Administrator\\Desktop\\daode.txt',errors='ignore') as read_file:#读取文本
    data=read_file.read()
    print(data)


读取出来咋用啊,还是逐行读取为字符串吧


data = ''
with open('C:\\Users\\Administrator\\Desktop\\daode.txt',errors='ignore') as f:#逐行读取文本为str
    for line in f.readlines():
        line = line.strip()
        data += line
        print(data)


去一下标点符号


from string import punctuation
str = data
add_punc=',。、【】“”:;()《》‘’{}?!⑦()、%^>℃:.”“^-——=擅长于的&#@¥' # 去除字符串内的符号
all_punc = punctuation + add_punc
temp = []
for c in str:
    if c not in all_punc :
        temp.append(c)
newText = ''.join(temp)
print(newText)


去除数字


from string import digits
s = newText
remove_digits = str.maketrans('', '', digits)#去除字符串内的数字
res = s.translate(remove_digits)
print(res)


结巴(jieba)分词


import jieba
mytext = " ".join(jieba.cut(res))
print(mytext)


可视化


import wordcloud
c = wordcloud.WordCloud(background_color='white')#1.配置对象参数,背景色换为白色
wenzi = "He is busy every day. He has many thing to do. He has no time to go home for lunch. He gets home at 7:00 p.m. At home he does the housework. He cooks nice dishes for mother and me."
c.generate(mytext)  #2.加载词云文本
c.to_file("pywordcloud.png")#3.输出词云文件

懵逼了吧,宝儿,这是因为matplotlib默认字体是不包含中文的,所以我们要给他的参数定义一个字体

import wordcloud
c = wordcloud.WordCloud(font_path="msyh.ttc",background_color='white')#1.配置对象参数,背景色换为白色
wenzi = "He is busy every day. He has many thing to do. He has no time to go home for lunch. He gets home at 7:00 p.m. At home he does the housework. He cooks nice dishes for mother and me."
c.generate(mytext)  #2.加载词云文本
c.to_file("pywordcloud.png")#3.输出词云文件


目录
相关文章
Python绘图神器Matplotlib、Echarts、Pyecharts 和 Plotly ——可绘制各种图
Python绘图神器Matplotlib、Echarts、Pyecharts 和 Plotly ——可绘制各种图
Python绘图神器Matplotlib、Echarts、Pyecharts 和 Plotly ——可绘制各种图
|
6月前
|
算法 数据可视化 JavaScript
Python如何使用Pyecharts+TextRank生成词云图?
Python如何使用Pyecharts+TextRank生成词云图?
65 0
|
7月前
|
Python
【Python】【Matplotlib】词云图
关于从网页获取文本
38 0
|
11月前
|
自然语言处理
绘图系列|R-wordcloud2包绘制词云
绘图系列|R-wordcloud2包绘制词云
|
数据可视化 Python
WordCloud词云库快速入门(一)
wordcloud是优秀的词云展示第三方库,以词语为基本单位,通过图形可视化的方式,更加直观和艺术的展示文本。
279 0
|
人工智能 自然语言处理 Python
Python 词云图:wordcloud库的使用
Python 词云图:wordcloud库。安装,使用,常用函数方法,配置对象参数,蒙版,配色集。水浒传词云图代码实例。
382 0
Python 词云图:wordcloud库的使用
|
自然语言处理 Python
手把手教你用Python绘制词云
手把手教你用Python绘制词云
318 0
手把手教你用Python绘制词云