In [37]:
import jieba 
import matplotlib.pyplot as plt
from wordcloud import WordCloud,STOPWORDS,ImageColorGenerator
from PIL import Image
import numpy as np
In [27]:
data = """孩子如果已经长大,就得告别妈妈,四海为家。
牛马有脚,鸟有翅膀,植物旅行又用什么办法?
蒲公英妈妈准备了降落伞,把它送给自己的娃娃。
只要有风轻轻吹过,孩子们就乘着风纷纷出发。
苍耳妈妈有个好办法,她给孩子穿上带刺的铠甲。
只要挂住动物的皮毛,孩子们就能去田野、山洼。
豌豆妈妈更有办法,她让豆荚晒在太阳底下,
啪的一声,豆荚炸开,孩子们就蹦着跳着离开妈妈。
植物妈妈的办法很多很多,不信你就仔细观察。
那里有许许多多的知识,粗心的小朋友却得不到它。
孩子如果已经长大,就得告别妈妈,四海为家。
牛马有脚,鸟有翅膀,植物旅行又用什么办法?
蒲公英妈妈准备了降落伞,把它送给自己的娃娃。
只要有风轻轻吹过,孩子们就乘着风纷纷出发。
苍耳妈妈有个好办法,她给孩子穿上带刺的铠甲。
只要挂住动物的皮毛,孩子们就能去田野、山洼。
豌豆妈妈更有办法,她让豆荚晒在太阳底下,
啪的一声,豆荚炸开,孩子们就蹦着跳着离开妈妈。
植物妈妈的办法很多很多,不信你就仔细观察。
那里有许许多多的知识,粗心的小朋友却得不到它。
孩子如果已经长大,就得告别妈妈,四海为家。
牛马有脚,鸟有翅膀,植物旅行又用什么办法?
蒲公英妈妈准备了降落伞,把它送给自己的娃娃。
只要有风轻轻吹过,孩子们就乘着风纷纷出发。
苍耳妈妈有个好办法,她给孩子穿上带刺的铠甲。
只要挂住动物的皮毛,孩子们就能去田野、山洼。
豌豆妈妈更有办法,她让豆荚晒在太阳底下,
啪的一声,豆荚炸开,孩子们就蹦着跳着离开妈妈。
植物妈妈的办法很多很多,不信你就仔细观察。
那里有许许多多的知识,粗心的小朋友却得不到它。"""
In [28]:
words = jieba.cut(data)
In [29]:
words
Out[29]:
<generator object Tokenizer.cut at 0x00000193CD582C10>
In [ ]:
 
In [18]:
stopwords = set(STOPWORDS)
with open(r"C:\Users\t1856\Desktop\stopwords.txt",encoding = "utf-8")as file:
    for line in file:
        stopwords.add(line.strip())
In [19]:
stopwords
Out[19]:
{'',
 '!',
 '&',
 '(',
 ')',
 '+',
 ',',
 '-',
 '.',
 '/',
 '0',
 '1',
 '2',
 '3',
 '4',
 '5',
 '6',
 '7',
 '8',
 '9',
 ':',
 ';',
 '\\',
 'a',
 'about',
 'above',
 'after',
 'again',
 'against',
 'all',
 'also',
 'am',
 'an',
 'and',
 'any',
 'are',
 "aren't",
 'as',
 'at',
 'be',
 'because',
 'been',
 'before',
 'being',
 'below',
 'between',
 'both',
 'but',
 'by',
 'can',
 "can't",
 'cannot',
 'com',
 'could',
 "couldn't",
 'did',
 "didn't",
 'do',
 'does',
 "doesn't",
 'doing',
 "don't",
 'down',
 'during',
 'each',
 'else',
 'ever',
 'few',
 'for',
 'from',
 'further',
 'get',
 'had',
 "hadn't",
 'has',
 "hasn't",
 'have',
 "haven't",
 'having',
 'he',
 "he'd",
 "he'll",
 "he's",
 'hence',
 'her',
 'here',
 "here's",
 'hers',
 'herself',
 'him',
 'himself',
 'his',
 'how',
 "how's",
 'however',
 'http',
 'i',
 "i'd",
 "i'll",
 "i'm",
 "i've",
 'if',
 'in',
 'into',
 'is',
 "isn't",
 'it',
 "it's",
 'its',
 'itself',
 'just',
 'k',
 "let's",
 'like',
 'me',
 'more',
 'most',
 "mustn't",
 'my',
 'myself',
 'nbsp',
 'no',
 'nor',
 'not',
 'of',
 'off',
 'on',
 'once',
 'only',
 'or',
 'other',
 'otherwise',
 'ought',
 'our',
 'ours',
 'ourselves',
 'out',
 'over',
 'own',
 'r',
 'same',
 'shall',
 "shan't",
 'she',
 "she'd",
 "she'll",
 "she's",
 'should',
 "shouldn't",
 'since',
 'so',
 'some',
 'such',
 'than',
 'that',
 "that's",
 'the',
 'their',
 'theirs',
 'them',
 'themselves',
 'then',
 'there',
 "there's",
 'therefore',
 'these',
 'they',
 "they'd",
 "they'll",
 "they're",
 "they've",
 'this',
 'those',
 'through',
 'to',
 'too',
 'under',
 'until',
 'up',
 'very',
 'was',
 "wasn't",
 'we',
 "we'd",
 "we'll",
 "we're",
 "we've",
 'were',
 "weren't",
 'what',
 "what's",
 'when',
 "when's",
 'where',
 "where's",
 'which',
 'while',
 'who',
 "who's",
 'whom',
 'why',
 "why's",
 'with',
 "won't",
 'would',
 "wouldn't",
 'www',
 'you',
 "you'd",
 "you'll",
 "you're",
 "you've",
 'your',
 'yours',
 'yourself',
 'yourselves',
 '、',
 '。',
 '一定',
 '一次',
 '不',
 '与',
 '专业',
 '为',
 '任职',
 '优先',
 '会',
 '你',
 '信息',
 '公司',
 '其他',
 '具',
 '具备',
 '及',
 '可',
 '各种',
 '后',
 '和',
 '商品',
 '培训',
 '完成',
 '定期',
 '对',
 '岗位',
 '工作',
 '年',
 '并',
 '强',
 '您',
 '或',
 '接受',
 '月',
 '有',
 '机',
 '现',
 '的',
 '相关',
 '等',
 '系统',
 '者',
 '职位',
 '能',
 '行业',
 '要求',
 '金融',
 '!',
 '(',
 ')',
 ',',
 ':',
 ';',
 '?'}
In [20]:
filteredwords = [word for word in words if word not in stopwords]
Building prefix dict from the default dictionary ...
Dumping model to file cache C:\Users\t1856\AppData\Local\Temp\jieba.cache
Loading model cost 1.575 seconds.
Prefix dict has been built successfully.
In [22]:
#filteredwords 
In [23]:
cleanwords = " ".join(filteredwords )
In [25]:
#cleanwords
In [39]:
# 读取背景图片
image = Image.open("C:/Users/t1856/Desktop/star.png")
graph = np.array(image)

# 创建词云对象
wc = WordCloud(
    font_path='simhei.ttf',  # 设置字体路径以支持中文,确保路径正确
    mask=graph,  # 设置背景图
    background_color='white',  # 设置背景颜色
    max_words=2000,  # 设置最大显示的词数
    stopwords=stopwords,  # 设置停用词
    max_font_size=100,  # 设置字体最大值
    random_state=42,
    width=600,  # 设置图片的宽度
    height=400,  # 设置图片的高度
    margin=2,  # 设置词的边缘
)

# 生成词云
wordcloud = wc.generate(cleanwords )

# 从背景图片生成颜色
image_colors = ImageColorGenerator(graph)

# 显示词云图
plt.figure(figsize=(10, 10))
plt.imshow(wordcloud.recolor(color_func=image_colors), interpolation="bilinear")
plt.axis('off')  # 不显示坐标轴
plt.show()
In [44]:
# 创建词云对象
wc = WordCloud(
    font_path='simhei.ttf',  # 设置字体路径以支持中文
    mask=graph,  # 设置背景图
    background_color='white',  # 设置背景颜色
    max_words=2000,  # 设置最大显示的词数
    stopwords=stopwords,  # 设置停用词
    max_font_size=100,  # 设置字体最大值
    random_state=42,
    color_func=lambda *args, **kwargs: "red",  # 设置固定颜色为蓝色
    width=600,  # 设置图片的宽度
    height=400,  # 设置图片的高度
    margin=2  # 设置词的边缘
)

# 生成词云
wordcloud = wc.generate(cleanwords )

# 显示词云图
plt.figure(figsize=(10, 10))
plt.imshow(wordcloud, interpolation="bilinear")
plt.axis('off')  # 不显示坐标轴
plt.show()
In [ ]: