import tweepy
from textblob import TextBlob
from wordcloud import WordCloud
import pandas as pd
import numpy as np
import re
import matplotlib.pyplot as plt
plt.style.use("fivethirtyeight")
consumerKey = 'Isi punya kamu'
consumerSecret = 'Isi punya kamu'
accessToken = 'Isi punya kamu'
accessTokenSecret = 'Isi punya kamu'
# Creating the authentication object
authenticate = tweepy.OAuthHandler(consumerKey, consumerSecret)
# Setting the access token and access token secret
authenticate.set_access_token(accessToken, accessTokenSecret)
# Creating the API object while passing the authentication information
api = tweepy.API(authenticate, wait_on_rate_limit= True)
account = str(input("Enter the twitter account you would like to see: @"))
num = int(input("Enter the number of recent tweets you would like to use: "))
posts = api.user_timeline(screen_name = account, count=num, lang= 'id',tweet_mode = 'extended')
print("Show the ",num," recent tweets \n")
i=1
for tweet in posts[0:num]:
print(str(i) + ') '+ tweet.full_text + '\n')
i+=1
# Creating a data frame with a column called Tweets so it looks nice
df = pd.DataFrame( [tweet.full_text for tweet in posts] , columns=['Tweets'])
df.index = df.index + 1
df.head()
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
text = re.sub('yang', '', text) # This will remove any RT mentions
text = re.sub('saya', '', text) # This will remove any RT mentions
text = re.sub('dan', '', text) # This will remove any RT mentions
text = re.sub('kita', '', text) # This will remove any RT mentions
text = re.sub('dengan', '', text) # This will remove any RT mentions
text = re.sub('ini', '', text) # This will remove any RT mentions
text = re.sub('untuk', '', text) # This will remove any RT mentions
text = re.sub('dalam', '', text) # This will remove any RT mentions
text = re.sub('telah', '', text) # This will remove any RT mentions
text = re.sub('harus', '', text) # This will remove any RT mentions
text = re.sub('biasa', '', text) # This will remove any RT mentions
text = re.sub('tempat', '', text) # This will remove any RT mentions
text = re.sub('bagi', '', text) # This will remove any RT mentions
text = re.sub('akan', '', text) # This will remove any RT mentions
text = re.sub('pada', '', text) # This will remove any RT mentions
text = re.sub('dari', '', text) # This will remove any RT mentions
text = re.sub('di', '', text) # This will remove any RT mentions
text = re.sub('ke', '', text) # This will remove any RT mentions
text = re.sub('Saya', '', text) # This will remove any RT mentions
text = re.sub('para', '', text) # This will remove any RT mentions
text = re.sub('lebih', '', text) # This will remove any RT mentions
text = re.sub('adalah', '', text) # This will remove any RT mentions
text = re.sub('juga', '', text) # This will remove any RT mentions
text = re.sub('hari', '', text) # This will remove any RT mentions
return text
# Cleaning the text
df['Tweets'] = df['Tweets'].apply(CleanTxt)
# Showing the cleaned text
df
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(background_color='white', width=2500, height=2000, random_state=2 ).generate(allWords)
plt.imshow(wordCloud, interpolation="Bilinear") # Interpolation is the process of
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()
Post a Comment for "Source Code Sentimen Analisis Twitter dengan Python"