- [Instructor] So to finish up
our trending topics discussion
let's take the top trending topics from New York City
and turn them into a word cloud visualization.
So for that purpose we're going to define
a dictionary called topics and we're going to be using
this dictionary to store key value pairs
where the keys are the trending topic texts
and the values are the number of tweets
associated with that trending topic.
And remember in a word cloud
the visualization is going to make bigger words
for the items that are more frequent.
So to create the dictionary let's use a little loop here.
We're going to walk through
the New York City trends list that we created
back up above here in the preceding video.
And for each of the items in that list
we're going to create a new key
in the topics dictionary, which is initially empty.
The key is going to be the given trends name
so that will often be a hashtag or just a string of text.
And the value is going to be
the corresponding trends tweet volume.
And remember that up above here
we filtered out any of the items
that have none as their tweet volume.
So all of the tweets that are going to be
processed in this case are going to have
10,000 or more tweets associated with them.
All of the trends will have 10,000 or more tweets
and because it's a relatively small number
of topics that we're dealing with
the word cloud is going to probably be
somewhat sparse when it is completed.
So let's go ahead and execute that
to acquire the key value pairs.
And we can take a quick look at what topics looks like
and you'll see that we actually got
a decent number of key value pairs.
The top tweet topic at the moment is whatever Hanbin is
and there's over 2.4 million tweets for that right now
and that goes all the way down to the smallest
of the topics which has as of right this moment
11,000 plus tweets today.
And there are some other trending topics probably as well
but those were fewer than 10,000 tweets
and therefore did not show up in the list.
And always keep in mind that when you look at stuff
like this coming back live from Twitter
you could get some really crazy stuff back
including lots of curse words, for example.
So just be aware that you may see things you don't like
in the results that you get back from Twitter.
So at this point we have our dictionary
that we're going to use to create the word cloud.
Previously we didn't use a dictionary
so this is a little bit different
from what we showed you back in the NLP chapter.
So next up we're going to import the word cloud class
from the word cloud library
and this time when I create the word cloud
I'm going to include some additional key word arguments.
We do have a width and a height
so we're specifying that we want
600 pixels wide and 900 pixels tall.
We're not going to use a mask image this time
so our actual word cloud will be rectangular
and using the dimensions that we've specified.
This key word argument is new to us in this example
prefer horizontal with the value .5
means to try to use as a guideline
that 50% of the words should be horizontally oriented
versus vertically oriented.
And it's free to ignore that
in order to fit the content appropriately
but it's just a suggestion that you can provide.
We also indicated that we didn't want
any fonts smaller than 10 point size.
And we did give a color map to choose colors in this case
and we set the background color to white as well.
Now once we've created the word cloud object
we can use it to generate the word cloud.
Previously we used the generate method for that,
here we're using fit words
which is capable of receiving as its argument
a dictionary of key value pairs
like the one that we created up above.
So we'll go ahead and execute that
to create the word cloud and let's also write that out
to a file on disk as a PNG image
by using the to file method of the word cloud object.
And I'm now going to go ahead and open up that image
and bring it onto my screen here.
So this is, whoops, this is the word cloud
that we just generated and as you can see
some of the words are horizontal some of them are vertical.
The one that's the biggest is the one
that had the largest tweet count
and these smaller items that you see
in various locations throughout the word cloud
represent the ones that had smaller overall tweet counts.