Drawing a Stunning "Dream of the Red Chamber" Word Cloud with Python 3
Word clouds, which I’m sure you’ve all seen, are created using wordcloud, a famous Python library. This article will detail how to use wordcloud to create a word cloud for “Dream of the Red Chamber,” one of China’s Four Great Classical Novels.
1. Preparation
This involves three parts:
2. The wordcloud and jieba libraries, which can be installed using pip install wordcloud
and pip install jieba
.
3. Preparing a Chinese font file.
The .txt
text file and font file are bundled together for your convenience to replicate this tutorial’s example.
2. Drawing the “Dream of the Red Chamber” Word Cloud
Here’s the code directly:
from wordcloud import WordCloud
import jieba
text = "".join(jieba.cut(open("红楼梦.txt").read()))
wordcloud = WordCloud(font_path="kaibold.ttf").generate(text)
# Display the generated image:
plt.imshow(wordcloud, interpolation='bilinear')
plt.axis("off")
plt.margins(x=0, y=0)
plt.show()