News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

通过Python的wordcloud库将单词生成词云(心形形状)

posted on 2023-05-21 17:13     read(509)     comment(0)     like(7)     collect(5)


foreword

Hello everyone, I am Kongkong star. In this article, I will share with you how to generate words through Python's wordcloud词云(心形形状) library .

1. What is the wordcloud library?

The wordcloud library for Python is a Python package for generating word clouds . It can arrange the words with high frequency in a piece of text into a word cloud graph according to their frequency of occurrence and color depth, so as to better display the information in the text.
Using the wordcloud library, you can specify parameters such as the shape, color, and font of the word cloud, and control attributes such as the arrangement and size of words in the word cloud. You can use the wordcloud library to generate various types of word clouds, such as word frequency statistics, sentiment analysis, etc.

2. Install the wordcloud library

pip install wordcloud

3. View the wordcloud library version

 pip show wordcloud

Name: wordcloud
Version: 1.8.2.2
Summary: A little word cloud generator
Home-page: https://github.com/amueller/word_cloud
Author: Andreas Mueller
Author-email: t3kcit+wordcloud@gmail.com
License: MIT
Requires: matplotlib, numpy, pillow
Required-by:

4. How to use

1. Import library

import jieba
import numpy as np
from PIL import Image
from wordcloud import WordCloud

2. Define the image path

local = '/Users/kkstar/Downloads/video/pic/'

3. Define the text that needs word segmentation

text = "大家好我是空空star我爱发动态我喜欢使用搜索引擎模式进行分词"

4. Use jieba search engine mode word segmentation

cut_text = " ".join(jieba.cut_for_search(text))

5. Load the heart image

mask = np.array(Image.open(local+"heart.png"))

6. Create a word cloud object

wc = WordCloud(font_path="STHeitiMedium.ttc",  # 设置字体
               background_color='white',  # 设置背景颜色
               mask=mask,  # 设置背景图片
               max_words=2000,  # 设置最大显示的词数
               max_font_size=100,  # 设置字体最大值
               width=500,  # 设置画布的宽度
               height=500,  # 设置画布的高度
               )

7. Generate word cloud

wc.generate(cut_text)

8. Save the word cloud map

wc.to_file(local+"wordcloud.png")

9. Word Cloud Effect

Summarize

To set the heart shape of WordCloud, you need to use the mask parameter to specify the path of the heart image, use Python's Pillow library to load the heart image, and pass it to the WordCloud object.

Introduction to jieba library and pillow library:
Segment text through Python's jieba library
Add text watermark to pictures through Python's PIL library



Category of website: technical article > Blog

Author:evilangel

link:http://www.pythonblackhole.com/blog/article/25298/9489f76b1050b5ac3258/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

7 0
collect article
collected

Comment content: (supports up to 255 characters)