python中wordcloud生成词云_python使用WordCloud生成词云

前言

当我们想快速了解 书籍、小说、电影剧本中的内容是时,则可以采用绘制 WordCloud 词云图,显示主要的关键词,可以非常直观的看到结果

核心代码

from wordcloud import WordCloud

import matplotlib.pyplot as plt

import jieba

from PIL import Image

import numpy as np

# 生成词云函数

def create_word_cloud(words):

# 使用结巴分词

text = " ".join(jieba.cut(words,cut_all=False, HMM=True))

wc = WordCloud(

font_path="./wc.ttf",

max_words=100,

width=2000,

height=1200,

)

wordcloud = wc.generate(text)

# 写词云图片

wordcloud.to_file("wordcloud.jpg")

# 显示词云文件

plt.imshow(wordcloud)

plt.axis("off")

plt.show()

测试

ok,现在我们来传入一段文字,生成词云图片分析一下

s= """

1.life lies in movement. 生命在于运动

2.sport is the source of all life. 运动是生命的源泉.

3.to keep on, day after day practice go down, and only activities to keep the enthusiasm of adequate training and improve motor skills.

日复一日地坚持练下去吧,只有活动适量才能保持训练的热情和提高运动的技能.——塞涅卡

4.activity is the basis of life! 活动是生活的基础!——歌德

5.people's sound, not only by foods, especially to rely on motion.人的健全,不但靠饮食,尤靠运动.

6.the olympic motto is "higher, faster, stronger." 奥林匹克的格言是“更高,更快,更强”.

7.the health of the body for motionless and destruction, for sports practice and keep for a long time. --socrates 身体的健康因静止不动而破坏,因运动练习而长期保持.——苏格拉底

chenqionghe

chenqionghe

chenqionghe

chenqionghe

chenqionghe

muscle

muscle

muscle

muscle

muscle

yeah buddy! Light weight baby

yeah buddy! Light weight baby

yeah buddy! Light weight baby

chenqionghe, go to the gym,yeah buddy! Light weight baby

I was in the gym lifting weights.

"""

create_word_cloud(s)

运行结果如下

分析这张图片,不验难看出,这段文字表达了:chenqionghe喜欢去运动,和light weight baby这几个词关系密切

原文:https://www.cnblogs.com/chenqionghe/p/12312934.html

你可能感兴趣的:(python中wordcloud生成词云_python使用WordCloud生成词云)