TikTok Sentiment Analysis with Python: Analyzing User Reviews
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.
Table of contents
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:
- Assessing User Sentiment:
- Understand user sentiments through reviews and comments.
- Uncover the spectrum of experiences, discerning positives, negatives, likes, and dislikes.
- Application Enhancement:
- Aid developers in refining functionality and addressing user concerns.
- Utilize feedback for iterative improvements, creating a user-centric platform.
- 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.
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.
Frequently Asked Questions
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.
A. TikTok likely employs sentiment analysis. Analyzing user comments and reactions helps the platform understand user sentiments, enabling improvements and personalized content recommendations.
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.
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.