Get startedGet started for free

Twitter sentiment analysis

1. Twitter sentiment analysis

As the final step of analyzing tweet text, let us extract user perceptions and opinions from tweets using sentiment analysis.

2. Lesson Overview

In this lesson, we will understand the concept of sentiment analysis. We will then perform sentiment analysis on tweets on "biofuels" and interpret people's opinions on the topic.

3. Sentiment analysis

Sentiment analysis is the process of retrieving information about a consumer's perception of a product or brand. It is used to extract and quantify positive, negative, and neutral opinions as well as emotions like trust, joy, and anger from the text.

4. Significance of sentiment analysis

Customer perception of a brand is an important factor that influences purchasing decisions. Sentiment analysis is useful to understand the pulse of what customers feel about a brand. It is a proactive approach to listen to customers and engage with them directly.

5. How sentiment analysis works

Let's understand how sentiment analysis works. Sentiment analysis uses pre-defined sentiment libraries to calculate scores for various sentiments. These libraries have been trained on several documents and scored based on the meaning or intent of words. For example, horrible has a negative meaning and awesome has a positive meaning. When sentiment analysis is performed on a corpus, each relevant word is scored based on its nearness to a positive or negative word from the sentiment library. The same concept is extended to words expressing specific emotions like joy, trust, anger, and fear.

6. Sentiment analysis steps

The following are the steps involved in performing sentiment analysis. First, extract tweets on the topic of interest.

7. Sentiment analysis steps

Next, extract sentiment scores for the text using the syuzhet package.

8. Sentiment analysis steps

Plot the sentiment scores.

9. Sentiment analysis steps

finally, visualize and interpret customer perception and emotions.

10. Extract tweets for sentiment analysis

Let's extract 5000 tweets on the mobile phone “galaxy fold” using search_tweets().

11. Perform sentiment analysis

Next, we use the get_nrc_sentiment() function from the syuzhet package to extract sentiment scores for the text. get_nrc_sentiment() takes the column storing the tweet text as the argument.

12. View sentiment scores

Let's view the sentiment scores saved as a data frame with rows and columns representing the tweets and the emotions respectively. The column values are the sentiment scores for the tweets against each emotion.

13. Sum of sentiment scores

The next step is to get the sum of the sentiment scores for each emotion using the colSums() function. This function takes the extracted sentiment scores as input.

14. Data frame of sentiment scores

We then convert the output to a data frame having the sum of sentiment scores for each emotion in a column. The score of 211 for anger indicates that 211 words in the corpus were classified under the emotion anger by the sentiment libraries.

15. Data frame of sentiment scores

Next, we convert the row names of this data frame into a column “sentiment” and combine it with the sentiment scores using the cbind() function. We also remove the row names of the new data frame by setting row.names equal to "NULL".

16. Data frame of sentiment scores

We can now see a data frame with sentiments in one column and their respective scores in the second column.

17. Plot and visualize sentiments

Its now time to plot and visualize the sentiments using ggplot(). This function takes the following arguments: The data frame with the sentiment scores. Under aesthetics, the x-axis is set to "sentiment", y-axis to "score", and fill to "sentiment". The geometry is set to geom_bar() with the argument stat set to "identity". theme() is set to have the sentiment labels rotated 45 degrees and placed below the x-axis.

18. Visualize the sentiments

It is interesting to see that the tweets on Galaxy Fold have a high score on positive emotions, trust, and anticipation, which is great for a brand that is set to launch a new product.

19. Let's practice!

We learned to perform sentiment analysis and visualize the sentiment scores. Let's practice!

Create Your Free Account

or

By continuing, you accept our Terms of Use, our Privacy Policy and that your data is stored in the USA.