程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[Python practice] - word cloud production

編輯:Python

List of articles

List of articles

  • List of articles
  • Preface
  • One 、 preparation
    • 1. modular
    • 2. typeface
    • 3. Background map ( The shape of the word cloud )
    • 4. Text
  • Two 、 Make word cloud
    • 1. The simplest word cloud ( chinese )
    • 2. The simplest word cloud ( english )
    • 3. Chinese word cloud ( Word segmentation effect )
    • 4. Use a background mask
    • 5. Use a background mask ( Mask extract color )
    • 6. Use a background mask ( Custom color )
    • 7. Use a background mask ( Fine control )
  • 3、 ... and 、 This article resources
  • summary


Preface

One 、 preparation

1. modular

#pip install modular 
wordcloud
matplotlib # Data visualization 
jieba # Thesaurus 
pillow
numpy

Possible problems :
wordcloud Installation needs visual C++14.0 whl install

wordcloud() Parameter interpretation :

Parameter name meaning font_path Can be used to specify the font path , Include otf and ttfwidth The width of the word cloud , The default is 400height Height of word cloud , The default is 200mask Mask , It can be used to customize the shape of word cloud min_font_size Minimum font size , The default is 4max_font_size Maximum font size , The default is the height of the word cloud max_words Maximum number of times , The default is 200stopwords Stop words that will be ignored , If not specified, the default stop word thesaurus is used background_color The background color , The default is blackmode The default is RGB Pattern , If RGBA Pattern and background_color Set to None, The background will be transparent

2. typeface

1、 You can choose from the fonts that come with your computer , Copy to the run script path , You can also specify a path ;

C:\Windows\Fonts


2、 Download other favorite fonts and put them in the specified path ;

notes : Here are some Fonts , You can download it yourself ;

3. Background map ( The shape of the word cloud )


4. Text

chinese :

 Communicate with you , It's like whispering with your soul , free , Let the sound of heartbeat come out , I really want to stretch out my arms to hug you. The window is silent . Only the sound of winter rain beating on the steps is like the sound of a zither furnace , It is already the extravagant hope in the dream, even without . With Jin Junmei , Enough romance is hidden in the smoke , Sit still as a thinker , Thoughts and feelings have blended with you . The fragrance of lips and teeth comes not only from the tea in the cup, but also from the trivial days waiting for a long-awaited date , As you read my book, you are intoxicated. Your soul has become a stranger running to me. I will eventually hold your hand . This may be the Jianghu . Cozy , Let sadness and I will not let pain , Hidden in your heart is a snow that has sent mutual concern
Start from a place that is too low to be low . Like a negative number on a coordinate axis , It is more like an angular stone that can move around . Life is a knife , Cutting iron like mud, it cuts me , I honed it . Finally one day I came to the zero point on the coordinate axis .
I'm still not smooth . And life is no softer . I looked , Cast into the unseen distance . I put my back , Leave it to the hometown you can't go back to . The sonorous steps like the hooves of a steed once became a legend of my hometown .
And the right path , It has never been a straight line . The road with nine twists and eighteen turns . My tears can be led out by a small flower on the roadside . Mount Tai is on top of the mountain, but it can't change my eyes . Compromise is deep in my spine, and I can't bend it down . This is my half life experience . I have no spare power to change . The only foresight in the sunset is to return . Return to a stone that is too low to be low , You don't know? . You don't know? , I don't blame you .

english :

Three passions, simple but overwhelmingly strong, have governed my life: the longing for love, the search for knowledge,
and unbearable pity for the suffering of mankind. These passions, like great winds, have blown me hither and thither, in a
wayward course, over a deep ocean of anguish, reaching to the verge of despair. I have sought love, first, because it brings
ecstasy --- ecstasy so great that I would have sacrificed all the rest of life for a few hours of this joy. I have sought it, next,
because it relieves loneliness --- that terrible loneliness in which one shivering consciousness looks over the rim of the world
into cold unfathomable lifeless abyss. I have sought it, finally, because in the union of love I have seen, in a mystic miniature,
the prefiguring vision of the heaven that saints and poets have imagined. This is what I sought, and though it might seem
too good for human life, this is what --- at last --- I have found. With equal passion I have sought knowledge. I have wished
to understand the hearts of men, I have wished to know why the stars shine. And I have tried to apprehend the Pythagorean
power by which number holds away above the flux. A little of this, but not much, I have achieved. Love and knowledge, so
far as they were possible, led upward toward the heavens. But always pity brought me back to earth. Echoes of cries of pain
reverberated in my heart. Children in famine, victims tortured by oppressors, helpless old people a hated burden to their sons,
and the whole world of loneliness, poverty, and pain make a mockery of what human life should be. I long to alleviate the evil,
but I cannot, and I too suffer. This has been my life. I have found it worth living, and I would gladly live it again if the chance
were offered to me.

Two 、 Make word cloud

1. The simplest word cloud ( chinese )

The effect is as follows :

The code is as follows :

##chinese
from wordcloud import WordCloud
import matplotlib.pyplot as plt
# Open text
text = open("bulletchinese.txt", encoding="utf-8").read()
# Generating objects
wc = WordCloud(font_path="msyh.ttc", width=800, height=600, mode="RGBA", background_color=None).generate(text)
# Show word cloud
plt.imshow(wc, interpolation='bilinear')
plt.axis("off")
plt.show()
# Save to file
wc.to_file("bulletchinese.png")

2. The simplest word cloud ( english )

The effect is as follows :

The code is as follows

from wordcloud import WordCloud
import matplotlib.pyplot as plt
# Open text
text = open("bulletEnglish.txt", encoding="utf-8").read()
# print(text)
# print(type(text)) # <class 'str'>
# Generating objects
#wc = WordCloud().generate(text)
wc = WordCloud(font_path="msyh.ttc", width=800, height=600, mode="RGBA", background_color=None).generate(text)
# Show word cloud
plt.imshow(wc, interpolation='bilinear') # interpolation Set interpolation , Set the color 、 Arrange, etc
plt.axis("off") # Turn off the axis
plt.show()
# Save the word cloud picture to a file
wc.to_file("bulletEnglish.png")

3. Chinese word cloud ( Word segmentation effect )

The effect is as follows :

The code is as follows :

from wordcloud import WordCloud
import matplotlib.pyplot as plt
import jieba
# Open text
text = open("bulletchinese.txt", encoding="utf-8").read()
# Chinese word segmentation
text = ' '.join(jieba.cut(text)) # utilize jieba Word segmentation to form a list , Separate the words in the list with spaces and spell them into a string .
print(text[:10000]) # Before printing 100 Characters
# Generating objects
wc = WordCloud(font_path="msyh.ttc", width=800, height=600, mode="RGBA", background_color=None).generate(text)
# Show word cloud
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
# Save to file
wc.to_file("bulletchinese2.png")

4. Use a background mask

The effect is as follows :

The code is as follows :

from wordcloud import WordCloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import jieba
# Open file
text = open("bulletchinese.txt", encoding="utf-8").read()
# Chinese word segmentation
text = ' '.join(jieba.cut(text))
print(text[:1000])
# Generating objects
mask = np.array(Image.open("background.png")) # Use masked pictures
wc = WordCloud(mask=mask, font_path="msyh.ttc", width=800, height=600, mode="RGBA", background_color=None).generate(text)
# Show word cloud
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
# Save the file
wc.to_file("bulletchinese3.png")

5. Use a background mask ( Mask extract color )

The effect is as follows :

The code is as follows :


from wordcloud import WordCloud, ImageColorGenerator
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import jieba
# Open file
text = open("bulletchinese.txt", encoding="utf-8").read()
# Chinese word segmentation
text = ' '.join(jieba.cut(text))
print(text[:1000])
# Generating objects
mask = np.array(Image.open("background1.png"))
wc = WordCloud(mask=mask, font_path="msyh.ttc", width=1000, height=800,mode="RGBA", background_color=None).generate(text)
# Generate colors from pictures
image_colors = ImageColorGenerator(mask)
wc.recolor(color_func=image_colors)
# Show word cloud
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
# Save the file
wc.to_file("bulletchinese4.png")

6. Use a background mask ( Custom color )

The effect is as follows :

The code is as follows :

from wordcloud import WordCloud
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import random
import jieba
# Open text
text = open("bulletchinese.txt", encoding="utf-8").read()
# Chinese word segmentation
text = ' '.join(jieba.cut(text))
print(text[:1000])
# Color function :( word , The size of the word , The position of the word , The orientation of words , The path of words , Random state )
def random_color(word, font_size, position, orientation, font_path, random_state):
s = 'hsl(0, %d%%, %d%%)' % (random.randint(60, 80), random.randint(60, 80))
# hsl For hue (Hue)、 saturation (saturation)、 brightness (luminance)
print(s)
return s
# Generating objects
mask = np.array(Image.open("background.png"))
wc = WordCloud(color_func=random_color, mask=mask, font_path="msyh.ttc", mode="RGBA", background_color=None).generate(
text) # color_func=random_color That is, use the function to specify the color of the word
# Show word cloud
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
# Save to file
wc.to_file("bulletchinese5.png")

7. Use a background mask ( Fine control )

The effect is as follows :

The code is as follows :

from wordcloud import WordCloud, ImageColorGenerator
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import jieba.analyse # Used to extract keywords
# Open text
text = open("bulletchinese.txt", encoding="utf-8").read()
# Extract keywords and weights
freq = jieba.analyse.extract_tags(text, topK=200, withWeight=True) # Extract the keywords in the file ,topK Indicates the quantity extracted ,withWeight=True Indicates that the weight of the keyword will be returned .
print(freq[:200])
freq = {
i[0]: i[1] for i in freq} # Dictionaries
# Generating objects
mask = np.array(Image.open("background2.png"))
wc = WordCloud(mask=mask, font_path="msyh.ttc", mode="RGBA", background_color=None).generate_from_frequencies(freq)
# Generate colors from pictures
image_colors = ImageColorGenerator(mask)
wc.recolor(color_func=image_colors)
# Show word cloud
plt.imshow(wc, interpolation="bilinear")
plt.axis("off")
plt.show()
# Save the file
wc.to_file("bulletchinese6.png")

3、 ... and 、 This article resources

Download resources


summary

Share :
Sometimes in solitude there is humiliation and endurance , Anger and resistance are brewing . The silence at this time often contains a strong explosive force . The loneliest moment in life , It often becomes the catalyst of life .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved