Are you ready? 1
Here's the solution. 2
Going to the top of the file, I'll add another constant and I'll call this constant "SKULL_FILE" and it's going 3
to point to the "skull-icon.png", 4
this one right here, Shift+Enter on this cell, scroll back down and now it's time to put all of this text 5
that's in the "shakespeare-hamlet.txt" file from the NLTK resources into a single string, 6
so I'll call a variable "hamlet_corpus" and set that equal to a "nltk.corpus.gutenberg. 7
words('shakespeare-hamlet.txt')" the same file name as right 8
here. The word list is going to be equal to "[''.join()]" and I'll join the 9
word up from my loop, so "for word in hamlet_corpus" will give us our list of words and then I'll say "hamlet_ 10
as_string = ' '.join(word_list)". That's our play as a string. 11
Now all I need to do is maybe go "skull_icon = Image.open()", 12
so here we're using pillow, feeding the relative path to the file and now it's time to create that mask. 13
So I'll say "image_mask" is equal to a new pillow image object with mode equal to "RGB", 14
the size equal to the "skull_icon.size" and color equal to white - (255, 255, 255)" inside 15
a tuple. The rgb_array for this skull image will simply be a numpy array created from the image mask. 16
Now we can create our word cloud, "word_cloud" is gonna be equal to "WordCloud( 17
mask = rgb_array)" from Hamlet. I'm going to choose my background color as "white" and then I'm 18
going to pick a color map. The color map that I'm going to go for for my skull image is gonna be called 19
"bone". I figure that's gonna be a nice combination. And for the maximum words, "max_words" argument, 20
I'm going to pick... I'll start out with 50 and then I'm going to up it, just so you can see how a higher 21
number will actually look a lot better. Then I'll use "word_cloud.generate()" to generate my image, but 22
of course I have to feed in which string I want to use, 23
so "hamlet_as_string". 24
That's it for the word cloud. 25
Now it's all gonna be 26
matplotlib. "plt.figure()" sets the size of our figure, 27
I'm gonna go again with 16 and 8. 28
I think that's a good science for me and for the video. I'll use the "imshow()" method, feed in the word_ 29
cloud along with an interpolation of "bilinear", 30
so I think that's spelled right. 31
Good. 32
I'll remove the axes, so "axis('off')" 33
and then I'll show my chart. 34
The moment of truth of course is hitting Shift+Enter on this. 35
Let's see what it looks like. 36
All right. 37
So I've got a value error: "canvas size is too small". 38
Any idea what went wrong? 39
The reason is I've got an entirely white canvas. 40
The RGB array is fully white. 41
There is no place for the word cloud to draw anything and that's because I haven't called 42
"image_mask.paste()" with the skull icon. 43
So I'll need to write "image_mask. 44
paste(skull_icon, box = skull_icon)" and if I refresh my cell now, then this error 45
should disappear. 46
So here's my image and nobody in the world will be able to tell that this is indeed a skull image. 47
The culprit this time is the maximum number of words. This number here is too small. 48
It doesn't actually help us see any of the detail in the image. 49
So I'm gonna change it from 50 to 600 and refresh my cell. 50
For this many words your computer might actually run for a little while because it's doing a lot of 51
work to generate this image, but I think the wait was worth it. 52
We've got a beautiful word cloud here with a fantastic color scheme and a lot of the details in the 53
word cloud being made visible by the high number of words in the word cloud. 54
Now that we're done with you to practice word clouds and using the NLTK resources it's time to create 55
a word cloud 56
for our ham and our spam messages. Those we will create from our dataset and I'll see you in the next lesson. 57
Take care.