Monday, April 4, 2022

Create Word Cloud (WordCloud) using Python



# https://youtu.be/vRbSnlRyJNQ

from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator
import numpy as np
import matplotlib.pyplot as plt
import PIL.Image

#STOPWORDS.add("ABCD")  Add other words to be excluded
#print(STOPWORDS)
text = "Python is a high-level, general-purpose programming language. \
Its design philosophy emphasizes code readability, using significant \
indentation. Its language constructs & OOPs approach aim to help \
programmers write clear, logical code for small and large-scale projects. \
Python is dynamically-typed and garbage-collected. It supports multiple \
programming paradigms, including structured (particularly procedural), \
object-oriented and functional programming. It is often described as a \
\"batteries included\" language due to a comprehensive standard library. \
Guido van Rossum began working on Python in the 1980s as a successor to \
the ABC programming language and released it in 1991 as Python 0.9.0. \
Python 2.0 was released in 2000 and introduced new features such as list \
comprehensions, cycle-detecting garbage collection, reference counting, & \
Unicode support. Python 3, released in 2008, was a major revision that is \
not completely backward-compatible with earlier versions. Python 2 was \
discontinued with in 2020. Python consistently ranks as one of the most \
popular programming languages."

'''
wc = WordCloud(stopwords=STOPWORDS).generate(text)
plt.imshow(wc)
plt.axis("off")
plt.show()
'''

python_mask = np.array(PIL.Image.open("PythonLogo1.png"))
colormap = ImageColorGenerator(python_mask)
wc = WordCloud(stopwords=STOPWORDS, mask=python_mask,
               background_color="white", contour_color="black",
               contour_width=15, min_font_size=3,
               max_words=400).generate(text)
wc.recolor(color_func=colormap)
plt.imshow(wc)
plt.axis("off")
plt.show()

No comments:

Post a Comment