Modeling Twitter feeds

Now, the first exercise is to generate the word-cloud model from the data based on the frequency of the words used. To do so, we need to work only with the Tweet_Text column, so we extract into a separate variable and change the data structure of the text to a string:

df=tweet['Tweet_Text']
#change the text to str
text=str(df)

Now we can generate the word cloud easily using the WordCloud Python library. We have used that before in Chapter 7Modeling with Unstructured Data, so preliminary preprocessing steps have been escaped here:

wordcloud = WordCloud(background_color="white",width=1000, height=860, margin=2).generate(text)
import matplotlib.pyplot as plt
plt.imshow(wordcloud)
rcParams['figure.figsize'] = 20, 10
plt.axis("off")
plt.show()

The snippet should generate a word cloud similar to the one given as follows:

Figure 13.2: Word cloud generated from Twitter text feed based on the frequency of words used in the tweet

It is possible to save the generated file directly into the file. It can be done as follows:

wordcloud.to_file('wordcloud.png')

The snippet should save the generated graph model to a file named wordcloud.png.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.133.233