Generating wordcloud with tweets about the series “Anne with an E”
Tags: nlp, wordcloud, python, ai
Published: 2020-02-14 03:36:20

> The series “Anne with an E” is gaining more and more space in the media for talking about important issues in a sensitive and subtle way, such as: racism, feminism and self-acceptance, in addition to making us reflect on how the simple things of world can be magnificent and that with imagination and kindness, we can go through all the situations of life.


## What is NLP?

NLP or Natural Language Processing is a branch of Artificial intelligence that is dedicated to understanding the relationship of human language (spoken, written) and machine language.
So I decided to generate a word cloud with with the users' tweets about this series.


P.S: I made the codes used in this article available in my [Github](https://github.com/lauraDamacenoAlmeida/Twitter_Analysis) repository.

## What is wordcloud?

[Word cloud](https://github.com/amueller/word_cloud) or Wordcloud is used in the exploration of texts, to better understand which words are frequent in the sentences, and thus perform the possible treatments in them or better understand for example: what your users or customers are saying about a certain product or company . 

## Advantages of using wordcloud in analysis

We can analyze thousands of text feedbacks and bring up the most cited terms, which help us to understand:
* points of your brand that delight customers;
* items that do not meet the needs of your audience;
* problematic relationships during the shopping day;

And with all this we will have the most important topics related to the business and prioritize which ones are more important to prioritize, that is, we make more assertive decisions.

### Extraction of tweets

For extract tweets from Twitter you need to have an account in [Twitter Develper](https://developer.twitter.com/) and install the [TwitterSearch](https://pypi.org/project/TwitterSearch/) library, with this we can conect in Twitter's API and extract ours data. 

> Who knows next blog I can show you how can generate the API Keys and tokens in Twitter Developer! ;)

{% gist https://gist.github.com/lauraDamacenoAlmeida/1f4d3464a3be8a0706adc83393d077ef %}

To make the filter in tweets, we use the ".set_keywords()" methods and we can filter to the tweet's language with the method ".set_language()"

In this method I create a file called "tweets.json" for storage all those tweets, where I load it on my jupiter notebook turning it into a Dataframe to make it easier to manipulate.

## Pre processing

It's very important make a pre processing in the tweets, because most of then will come with "stop words" or words considered irrelevant for our processing (preposition, articles and pronouns), we can remove the accents in the words, extra spaces and so on. That is, in this pre processing we transform this unstructured data into structured and multi-dimensional representations.

For example:
> Batman is better than Superman

After pre processing:
> Batman better Superman

![](https://media0.giphy.com/media/1485wMdlDLG7mg/giphy.gif?cid=790b7611648329afb0a4c574eb8dae670f8911ca942e021c&rid=giphy.gif)

In other words, our NLP model or our analyzes will be able to focus on the words that are really important to the business.

For this I create a method to remove the accents, numbers and spaces.

{% gist https://gist.github.com/lauraDamacenoAlmeida/aca0239bf23c4a92a391f59d9ff0b215 %}

[Stop words](https://www.geeksforgeeks.org/removing-stop-words-nltk-python/) can affect our wordcloud because this type of visualization catches which phrases are most frequent among all and certainly the words “and, with, that, the” are very frequent. In this method I am generating tokens in the sentences so that I can facilitate the stop words and some punctuation (‘,’ ’.’, ’#’,etc...).

{% gist https://gist.github.com/lauraDamacenoAlmeida/bec0bfeafc9c6e9fb0ac04bdea9d1498 %}

It's very important to say that we need import the stop words, so we will import NLTK library, in my example I imported portuguese stop words!
```
 stopwords = nltk.corpus.stopwords.words('portuguese')
```

## Generating wordcloud
To generate a wordcloud we need to pass a list of words, so I generated a list from our pre-processed phrases called "todas_palavras".

To generate a wordcloud, we need to instantiate this "WordCloud ()" object and then use the ".generate ()" command to generate this wordcloud. So our code would look like this:
```
word_cloud = WordCloud( ).generate(todas_palavras)

```
To display this word cloud we use the following commands:

```
plt.imshow(word_cloud, interpolation=”bilinear”)
plt.show()
```


I created a method so that we can reuse our wordcloud in an easier way, but you can choose not to use methods.

{% gist https://gist.github.com/lauraDamacenoAlmeida/0a2328cc2a063223a59a988f1823a3fe %}

And your result should look like this:
![](https://miro.medium.com/max/2788/1*C0svep__DYmlOAtzxHJesA.png)

Before concluding, it is important to note that wordcloud is an excellent visualization tool that facilitates the analysis of texts or text analysis, allowing the visualization of the frequency distribution of words and thus we can improve our pre-processing or the understanding of our users.

That's all folks!!

![](https://miro.medium.com/max/776/1*lv_3ooWwCkzrzevc_TTAmA.gif)

My [LinkedIn](https://www.linkedin.com/in/laura-damaceno/)