Twitter Data Analysis (Part 3) - Sentiment Analysis
Sentiment Analysis
In this blog, we will be learning what Sentiment Analysis is and why we use it but also carry out sentiment analysis on our tweets.
What is Sentiment Analysis?
This is a process where text is analyzed to determine the attitude or emotion of a text. This text can range from a single word to a huger document. Sentiment analysis is often used within businesses. For instance, analysis can be done on reviews of a product or service. Businesses can analyze their consumer's reviews using various techniques and determine their true emotions and benefit from this. If a company has a huger amount of comment/reviews, a word cloud could be used to only show the most common words used in a matter of minutes (even seconds depending on the amount). This, however, would have taken hours or even days if humans were to analyze this manually. In our Twitter Analysis, we will be using the sentiment function which outputs two properties, Subjectivity, and Polarity. Subjectivity tells you how subjective or opinionated a text is. This varies from +1 (very opinionated) to 0 (very bias). Polarity tells you how positive, negative, or neutral a text is (+1 being very positive, -1 being very negative).
Lets Start
# Cleaning the text by removing hastags, @'s, URL's. def CleanTxt(text): text = re.sub(r"@(\w+)", ' ', text) # This wil remove any @ mentions
text = re.sub('@[^\s]+','',text) # This will remove any hashtags (#) text = re.sub('https?:\/\/\S+', '', text) # This will remove any URl's text = re.sub('RT[\s]+', '', text) # This will remove any RT mentions
return text # Cleaning the text df['Tweets'] = df['Tweets'].apply(CleanTxt) # Showing the cleaned text df
# Creating a funtion to get the subjectivity. def getSubjectivity(text): return TextBlob(text).sentiment.subjectivity # Creating a function to get the polarity. def getPolarity(text): return TextBlob(text).sentiment.polarity # Creating two columns to store subjectivity and polarity df['Subjectivity'] = df['Tweets'].apply(getSubjectivity) df['Polarity'] = df['Tweets'].apply(getPolarity) # Outputting the new data frame df
# Creating a wordcloud to create a visualisation of the frequent words. allWords = ''.join([twts for twts in df['Tweets']]) # Stores all the words in one string wordCloud = WordCloud(width=1000, height=1000, random_state=2 ,
max_font_size=200).generate(allWords)
plt.imshow(wordCloud, interpolation="Bilinear") # Interpolation is the process of
estimating values that fall between known values. plt.axis('off') plt.show() lol = WordCloud()
# Deciding whether the text is +ve (+1), neutral (0) or -ve (-1) def getAnalysis(score): if score < 0: return 'Negative' elif score == 0: return 'Neutral' else: return 'Positive' df['Analysis'] = df['Polarity'].apply(getAnalysis) df
# Plotting a graph with Subjectivity against Polarity plt.figure(figsize=(8,6)) for i in range(1, df.shape[0]): plt.scatter(df["Polarity"][i], df["Subjectivity"][i], color='Blue') # Labelling axis's and title plt.title('Twitter Sentiment Analysis') plt.xlabel('Polarity') plt.ylabel('Subjectivity') plt.show()
df['Analysis'].value_counts()
# Finding percentage of positive tweets ptweets = df[df.Analysis == 'Positive'] ptweets = ptweets['Tweets'] ptweets round( (ptweets.shape[0] / df.shape[0]) * 100, 1)
# Finding percentage of negative tweets ntweets = df[df.Analysis == 'Negative'] ntweets = ntweets['Tweets'] ntweets round( (ntweets.shape[0] / df.shape[0]) * 100, 1)
# Finding percentage of neutral tweets neutweets = df[df.Analysis == 'Neutral'] neutweets = neutweets['Tweets'] ntweets round( (neutweets.shape[0] / df.shape[0]) * 100, 1)
# Creating a bar chart to visualise the count plt.title('Twitter Sentiment Analysis') plt.xlabel('Sentiment') plt.ylabel('Count') df['Analysis'].value_counts().plot(kind='bar') plt.show()
In this blog, we'll explore what Sentiment Analysis is and why it's crucial, along with how we can utilize it to conduct sentiment analysis reviews on both our tweets and other user feedback. Understanding sentiment allows us to gain insights into user perceptions, refine our offerings, and enhance communication strategies effectively. Integrating sentiment analysis reviews into our practices helps us boost engagement, elevate customer satisfaction levels, and solidify our brand's digital footprint. It's a pivotal approach for interpreting user feedback and fostering deeper connections with our audience.
ReplyDelete