WordCloud with a Mask

Write a text file

The %% magic allows you to write a file to disk directly from juypter


In [1]:
%%writefile text.txt
Amazon sells refurbished in new condition items, I have ordered 2 items from Amazon that were refurbished new condition. Both items had to be returned due to poor quality. Amazon needs to check these vendors. Customer service does not listen and seems not to care what you are saying. When you contact them they listen to half of what you say and think they have fixed the problem, they refuse to let you speak to anyone over a customer service supervisor, I have requested callbacks that never come.


Writing text.txt

Command line commands

Use ! to run any bash command


In [2]:
!cat text.txt


Amazon sells refurbished in new condition items, I have ordered 2 items from Amazon that were refurbished new condition. Both items had to be returned due to poor quality. Amazon needs to check these vendors. Customer service does not listen and seems not to care what you are saying. When you contact them they listen to half of what you say and think they have fixed the problem, they refuse to let you speak to anyone over a customer service supervisor, I have requested callbacks that never come.

In [3]:
from PIL import Image
import numpy as np
from IPython.display import Image as ImageJupyter

from wordcloud import WordCloud, STOPWORDS

# Taken from https://amueller.github.io/word_cloud/auto_examples/masked.html
def wc(mask_path, text_path='text.txt', save_to='amazon_wordcloud.png'):
    # Read the whole text.
    text = open(text_path).read()

    # read the mask image
    mask = np.array(Image.open(mask_path))

    stopwords = set(STOPWORDS)
    stopwords.add("said")

    wc = WordCloud(background_color="white", max_words=2000, mask=mask,
                   stopwords=stopwords)
    
    # generate word cloud
    wc.generate(text)

    # store to file
    wc.to_file(save_to)

    # show
    return ImageJupyter(save_to)

In [4]:
# small, uncropped image
wc('resources/amazon_logo_original.png')


Out[4]:

In [5]:
# small and cropped
wc('resources/amazon_logo_cropped.png')


Out[5]:

In [6]:
# larger image
# http://s3.r29static.com//bin/public/e41/0,0,2000,1050/x/1590455/image.png
wc('resources/amazon_logo_big.png')


Out[6]: