TikTok Sentiment Analysis with Python: Analyzing User Reviews
One of the social media applications that made it to the top eight in 2023 is TikTok. This application has revolutionized the way we watch short videos. Those who enjoy entertaining and funny short videos are likely already familiar with this app. However, not everyone is interested in it. Some praise it, while others criticize it. The main objective of doing sentiment analysis on the TikTok app is:
- To assess user sentiment about the application, which includes user reviews and comments.
- To gain insight into how users feel about the app, whether they have positive or negative experiences, and their likes or dislikes.
- For the developers to utilize it to improve the application’s functionality, solve problems, and respond to user concerns.
- For business people to adjust their marketing strategy on TikTok.
Starting from that perspective, we can use Python to do TikTok review sentiment analysis to see how people react to this application. The availability of various Python modules for analysis gives us a significant edge and significantly speeds up the research process.
So, if you want to know how users respond to this application, you’re in the right place. Now, let’s perform sentiment analysis through the steps in the article.
This article was published as a part of the Data Science Blogathon.
Table of Contents
Step 1: Import Library
import pandas as pd import matplotlib.pyplot as plt from wordcloud import WordCloud, STOPWORDS, ImageColorGenerator import nltk from nltk.sentiment.vader import SentimentIntensityAnalyzer from nltk.corpus import stopwords import string import re nltk.download('stopwords') stemmer = nltk.SnowballStemmer("english")
WordCloud is a library used to create text visualizations based on the number of times the words appear in them so it’s easy to understand.
STOPWORDS is a library used to remove unimportant words from documents or text, such as prepositions and conjunctions. The main goal in implementing the stop words process is to reduce the number of words in a document, which will affect the speed and performance of NLP (natural language processing).
ImageColorGenerator is a library that generates colors using images relevant to the text’s topic.
The SentimentIntensityAnalyzer is a library that analyzes sentiment in text. This library uses a score to determine if the text being analyzed falls into the positive, negative, or neutral category.
So, that’s the main function of the library above. We can create eye-catching visualizations, remove unnecessary words, generate topic-based colors, and evaluate text sentiment. Now, let’s go to the next step.
Step 2: Read the Data
This second step is the most important part because, without relevant data, it can lead to inaccurate analysis. The dataset that we will use is a collection of TikTok reviews downloaded from Kaggle based on ratings on the Google Play Store. Now let’s look at the contents of the dataset.
It turns out that there are ten columns in the dataset, which include reviewId, userName, userImage, content, score, thumbsUpCount, reviewCreatedVersion, at, replyContent, and revisedAt. However, not all of the columns are used for sentiment analysis. We’ll talk about it in the next step.
Step 3: Data Preprocessing
Data preprocessing is a critical step in sentiment analysis. It involves cleaning and preparing data for analysis to ensure the accuracy and effectiveness of the sentiment analysis results. We will use some of the initialized libraries at this point. Preprocessing techniques include removing unwanted characters, such as punctuation, and converting all the text to lowercase to make the analysis process easier.
Another important step in data preprocessing is removing stop words, which are common words that are not essential in determining the sentiment of a text. Stop words can include words like “the,” “is,” and “and.” Removing these words can help reduce noise and improve the accuracy of the sentiment analysis.
Other preprocessing techniques include tokenization, which involves breaking up the text into individual words or phrases, and stemming or lemmatization, which involves reducing words to their base form to account for spelling and word usage variations.
Overall, proper data preprocessing is essential for conducting accurate and effective sentiment analysis, and it is an important step in any natural language processing task.
As I previously stated, we do not use all of the dataset columns. Only two columns will be used: content and score. Therefore, we will create a new dataset containing only two columns.
data = data[["content", "score"]] print(data.head())
At first glance at the dataset, I noticed some columns had null values. However, let’s check whether the two columns we use to analyze TikTok review sentiment have null values or not.
It turns out there are four null values in the content column and one in the score column. Let’s drop these null values and take our analysis further.
data = data.dropna()
Now let’s prepare this data for the sentiment analysis task. Here, we need to clean up the text in the content column to ensure accurate analysis.
stopword=set(stopwords.words('english')) def clean(text): text = str(text).lower() text = re.sub('\[.*?\]', '', text) text = re.sub('https?://\S+|www\.\S+', '', text) text = re.sub('<.*?>+', '', text) text = re.sub('[%s]' % re.escape(string.punctuation), '', text) text = re.sub('\n', '', text) text = re.sub('\w*\d\w*', '', text) text = [word for word in text.split(' ') if word not in stopword] text=" ".join(text) text = [stemmer.stem(word) for word in text.split(' ')] text=" ".join(text) return text data["content"] = data["content"].apply(clean)
The Python code above defines a function named “clean,” which accepts a parameter named “text”. This function takes the input text and performs a series of text-cleaning operations on it to prepare it for sentiment analysis.
Here’s what each line of the function does:
- str(text).lower(): Converts all text to lowercase.
- re.sub(‘\[.*?\]’, ”, text): Removes any text inside square brackets, which is often used to denote tags or URLs.
- re.sub(‘https?://\S+|www\.\S+’, ”, text): Removes any URLs.
- re.sub(‘<.*?>+’, ”, text): Removes any HTML tags.
- re.sub(‘[%s]’ % re.escape(string.punctuation), ”, text): Removes any punctuation.
- re.sub(‘\n’, ”, text): Removes any newlines.
- text = re.sub(‘\w*\d\w*’, ”, text): Removes any words containing numbers.
- text = [word for word in text.split(‘ ‘) if word not in stopword]: Removes any stop words, which are common words that don’t add much meaning to the text (e.g. “the”, “and”).
- ” “.join(text): Joins the remaining words back together into a single string.
- [stemmer.stem(word) for word in text.split(‘ ‘)]: Applies stemming to the words in the text, which means reducing words to their base form (e.g., “running” becomes “run”).
- ” “.join(text): Joins the stemmed words back together into a single string.
- return text: Returns the cleaned text as the output of the function.
Let’s explore the percentage of ratings given to TikTok on the Google Play Store!
ratings = data["score"].value_counts() numbers = ratings.index quantity = ratings.values import plotly.express as px figure = px.pie(data, values=quantity, names=numbers,hole = 0.5) figure.show()
TikTok has garnered an impressive 74% of five-star ratings from users, with only 12.9% giving it a one-star rating. Let’s now take a closer look at the types of words used by TikTok reviewers.
text = " ".join(i for i in data.content) stopwords = set(STOPWORDS) wordcloud = WordCloud(stopwords=stopwords, background_color="white").generate(text) plt.figure( figsize=(15,10)) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show()
Step 4: Sentiment Analysis
We have now reached the final step, sentiment analysis. Firstly, we’ll transform the score column into three new columns: Positive, Negative, and Neutral, based on the sentiment score of each user review. This is done in order to acquire a thorough grasp of the review. Let’s get started.
nltk.download('vader_lexicon') sentiments = SentimentIntensityAnalyzer() data["Positive"] = [sentiments.polarity_scores(i)["pos"] for i in data["content"]] data["Negative"] = [sentiments.polarity_scores(i)["neg"] for i in data["content"]] data["Neutral"] = [sentiments.polarity_scores(i)["neu"] for i in data["content"]] data = data[["content", "Positive", "Negative", "Neutral"]] print(data.head())
Let’s now take a closer look at the type of words used in positive reviews of TikTok.
positive =' '.join([i for i in data['content'][data['Positive'] > data["Negative"]]]) stopwords = set(STOPWORDS) wordcloud = WordCloud(stopwords=stopwords, background_color="white").generate(positive) plt.figure( figsize=(15,10)) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show()
Let’s now explore the commonly used words in negative reviews of TikTok.
negative =' '.join([i for i in data['content'][data['Negative'] > data["Positive"]]]) stopwords = set(STOPWORDS) wordcloud = WordCloud(stopwords=stopwords, background_color="white").generate(negative) plt.figure( figsize=(15,10)) plt.imshow(wordcloud, interpolation='bilinear') plt.axis("off") plt.show()
TikTok has taken the world by storm with its short, amusing videos that people can’t get enough of. But not everyone is a fan of the app. In this post, we have discussed the following:
- How to use Python to do preprocessing on the text data?
- How to use Python to analyze the sentiments of TikTok reviews?
- How to explore the phrases used in positive and negative reviews?
Whether you’re a TikTok fan or not, the range of viewpoints is interesting. Did you find this article about TikTok Reviews’ sentiment analysis useful?
If you have any questions or comments, please leave them below.
The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.