This is a concise way to make a word cloud using Python. It can teach you basics of coding while creating a nice graphic.
It’s actually four lines of code, but making the word cloud only takes one line, the final one.
import nltk from wordcloud import WordCloud nltk.download("stopwords") WordCloud(background_color="white", max_words=5000, contour_width=3, contour_color="steelblue").generate_from_text(" ".join([r for r in open("mobydick.txt", "r").read().split() if r not in set(nltk.corpus.stopwords.words("english"))])).to_file("wordcloud.png")
The first two lines lines specify the required packages you must download with these links:
wordcloud. You may also try these links:
wordcloud to download them. The third line downloads the stop words (common words like “the”, “a” and “in”) that you don’t want in your word cloud.
The fourth line is complicated. Calling the
WordCloud() method, you can specify the background color, contour color and other options (found here).
generate_from_text() takes a string of words to put in the word cloud.
" ".join() creates this string of words separated by spaces from a list of words. The
for loop in the square brackets
 creates this list of each word from the input file (in this case,
mobydick.txt) with the
r variable letting you use each word one at a time in the list.
The input file is
split() into its words under the condition (using
if) they aren’t in
to_file() saves the image as
How to use this code
In the code, change
"mobydick.txt" to the name of your text file (keep the quotation marks). Save the code in a file
makewordcloud.py in the text file’s directory, and use a command line interface (such as Terminal) to navigate to the directory.
Run your script using
python makewordcloud.py, and check out your