TikTok Sentiment Analysis with Python: Analyzing User Reviews

Ata Amrullah Last Updated : 12 Oct, 2024
7 min read

Introduction

Unlocking the Pulse of TikTok: Dive into the world of sentiment analysis to decode the heartbeat of user emotions on the ever-popular TikTok platform. This article delves into the realm of Python-powered analytics, unraveling the highs and lows encapsulated in user reviews and comments. Join the journey as we explore how sentiment analysis not only gauges user sentiments but also steers the evolution of TikTok’s dynamic content landscape.

This article was published as a part of the Data Science Blogathon.

What is the Importance of TikTok Sentiment Analysis?

TikTok, a platform revolutionizing short video consumption, garners diverse reactions. While it captivates some with humor, others remain indifferent or critical. Analyzing TikTok sentiments becomes pivotal for various reasons:

  1. Assessing User Sentiment:
    • Understand user sentiments through reviews and comments.
    • Uncover the spectrum of experiences, discerning positives, negatives, likes, and dislikes.
  2. Application Enhancement:
    • Aid developers in refining functionality and addressing user concerns.
    • Utilize feedback for iterative improvements, creating a user-centric platform.
  3. Business Strategy Adjustment:
    • Enable businesses to align marketing strategies with prevailing sentiments on TikTok.
    • Adapt promotional approaches based on user reactions for enhanced engagement.

To delve into TikTok’s user sentiments, this article utilizes Python, leveraging its diverse modules for efficient sentiment analysis. If you’re keen on comprehending user responses to TikTok, follow along as we navigate through the steps of sentiment analysis in Python.

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.

Python Code:

import pandas as pd

data = pd.read_csv("tiktok_reviews.csv")

print(data.head())

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())
 new dataset preview
new dataset preview

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.

print(data.isnull().sum())
 null values
null values

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()

Clean up

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:

  1. str(text).lower(): Converts all text to lowercase.
  2. re.sub(‘\[.*?\]’, ”, text): Removes any text inside square brackets, which is often used to denote tags or URLs.
  3. re.sub(‘https?://\S+|www\.\S+’, ”, text): Removes any URLs.
  4. re.sub(‘<.*?>+’, ”, text): Removes any HTML tags.
  5. re.sub(‘[%s]’ % re.escape(string.punctuation), ”, text): Removes any punctuation.
  6. re.sub(‘\n’, ”, text): Removes any newlines.
  7. text = re.sub(‘\w*\d\w*’, ”, text): Removes any words containing numbers.
  8. 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”).
  9. ” “.join(text): Joins the remaining words back together into a single string.
  10. [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”).
  11. ” “.join(text): Joins the stemmed words back together into a single string.
  12. 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()
Sentiment Analysis

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()
Sentiment Analysis

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())
 new column in the dataset
new column in the dataset

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()
Sentiment Analysis
positive words

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()
 negative words
negative words

Conclusion

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.

Frequently Asked Questions

Q1. Is Python good for sentiment analysis?

A. Yes, Python excels in sentiment analysis. Its rich ecosystem, including NLTK and TextBlob libraries, simplifies text processing, making it a preferred language for sentiment analysis tasks.

Q2. Does TikTok use sentiment analysis?

A. TikTok likely employs sentiment analysis. Analyzing user comments and reactions helps the platform understand user sentiments, enabling improvements and personalized content recommendations.

Q3. Which Python tool for sentiment analysis?

A. Python offers various tools for sentiment analysis. Popular choices include NLTK, a powerful natural language processing library, and TextBlob, a simplified tool with sentiment analysis capabilities.

Q4. How do you do sentiment analysis in Python?

A. Perform sentiment analysis in Python using libraries like NLTK or TextBlob. Tokenize and process text, then leverage pre-trained models or custom datasets to determine sentiment polarity, providing insights into user opinions.

The media shown in this article is not owned by Analytics Vidhya and is used at the Author’s discretion.

Let me introduce you to someone who is passionate about learning and teaching Python programming, data science, and the Internet of Things (IoT) - that's me! My name is Ata Amrullah.

I am a self-taught programmer who enjoys sharing knowledge with others. I believe that everyone can learn to code and become a successful developer, and that's why I started my journey as a Python tutorial maker. I have created a variety of tutorials on different topics such as data analysis, web scraping, machine learning, and IoT, which can be found on my YouTube channel: https://www.youtube.com/@PythonProjecTutorial.

Apart from creating tutorials, I also enjoy working on personal projects that help me enhance my skills and explore new technologies. One of my recent projects involved building an IoT-based healthcare system that uses a Raspberry Pi as communication protocol gateway to control vary protocols.

I am a firm believer in the power of technology to change people's lives for the better, and that's what motivates me to keep learning and sharing my knowledge with others. Whether you are a beginner or an experienced programmer, I am always happy to help and answer any questions you may have about Python programming, data science, or IoT.

Responses From Readers

We use cookies essential for this site to function well. Please click to help us improve its usefulness with additional cookies. Learn about our use of cookies in our Privacy Policy & Cookies Policy.

Show details