Title | Tugas SI Crawling Data Twitter dan Sentiment Analysis |
---|---|
Author | Nico Munasatya |
Pages | 9 |
File Size | 521.3 KB |
File Type | |
Total Downloads | 6 |
Total Views | 73 |
Nama : Nico Munasatya NIM : A11.2017.10512 Kelompok : A11.4513 Tugas Crawling Data Twitter dan Sentiment Analisis terhadap Data Twitter menggunakan Polarity TextBlob TwitterConfig.py # -*- coding: utf-8 -*- """ @author: Nico Munasatya """ import tweepy #Konfigurasi twit...
Nama
: Nico Munasatya
NIM
: A11.2017.10512
Kelompok
: A11.4513
Tugas Crawling Data Twitter dan Sentiment Analisis terhadap Data Twitter menggunakan Polarity TextBlob TwitterConfig.py # -*- coding: utf-8 -*""" @author: Nico Munasatya """ import tweepy #Konfigurasi twitter CONSUMER_KEY = 'VKQ2d2VNbu2fpb8N2hnNkjTgk' CONSUMER_SECRET= 'Mc0R8iMx8LeS0Q2XrdnaeV4tjBi4HD6G4ormkouRVz7SG6QBfW' #Konfigurasi Akses ACCESS_TOKEN = '883712780854894592-bD2t0pSshVpYh6QCiu96ke04pJUXdwU' ACCESS_SECRET = 'xctB7BN3xuGdZpId0ahVdm2LPYtTyvfIWE4QzkA7vcUCi' def twitter_init(): # Autentikasi dan akses menggunakan key auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET) auth.set_access_token(ACCESS_TOKEN, ACCESS_SECRET) # Return API dengan autentikasi: api = tweepy.API(auth) return api
AnalysisFactory.py # -*- coding: utf-8 -*""" @author: Nico Munasatya """ from textblob import TextBlob import re def clean_tweet(tweet): return ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)", " ", tweet).split()) #sentiment analisis dengan polarity menggunakan library textblob def analize_sentiment(tweet): analysis = TextBlob(clean_tweet(tweet)) if analysis.sentiment.polarity > 0: return 1 elif analysis.sentiment.polarity == 0: return 0 else: return -1 def analize_subjectorobject(tweet): #Kita dapat melihat bahwa polaritas [-1],[0],[1] untuk -1 termasuk negatif,0 neutral #dan 1 termasuk positif .Misal terdapat polarity 0,8, yang berarti pernyataannya positif #dan subyektivitas 0,75 merujuk bahwa itu adalah opini publik dan bukan informasi faktual. analysis = TextBlob(clean_tweet(tweet)) if analysis.sentiment.polarity > 0: return 'Positif' elif analysis.sentiment.polarity == 0: return 'Neutral' else: return 'Negatif'
PieChartGenerator.py # -*- coding: utf-8 -*""" @author: Nico Munasatya """ import matplotlib.pyplot as plt def showPieChart(positive,neutral,negative): labels = 'Positive', 'Neutral', 'Negative' color = ['yellowgreen','gray','lightcoral'] value = [positive, neutral, negative] explode = (0.1, 0.1, 0.1) plt.pie(value,autopct='%1.1f%%',colors=color, labels=labels , shadow= True,explode = explode , startangle=140) plt.show() plt.savefig('Hasil Analisis/analysis.png')
SentimentAnalysis.py File untuk melakukan crawling data terhadap twitter dan sentiment analisis menggunakan polarity textblob import pandas as pd
# To handle data
import numpy as np
# For number computing
from IPython.display import display from TwitterConfig import * from AnalysisFactory import * from PieChartGenerator import showPieChart extractor = twitter_init() #Mengubah Query menjadi Params tweets = extractor.search(q="Presiden Jokowi",lang="en",count="200") print("Number of tweets extracted: {}.\n".format(len(tweets))) # Library Pandas untuk membuat sebauh Dataframe data = pd.DataFrame(data=[tweet.text for tweet in tweets], columns=['Tweets']) data['length'] = np.array([len(tweet.text) for tweet in tweets]) data['ID'] = np.array([tweet.id for tweet in tweets]) data['Date'] = np.array([tweet.created_at for tweet in tweets]) data['Source'] = np.array([tweet.source for tweet in tweets]) data['Likes'] = np.array([tweet.favorite_count for tweet in tweets]) data['RTs'] = np.array([tweet.retweet_count for tweet in tweets]) data['Sentiment Analysis'] = np.array([ analize_sentiment(tweet) for tweet in data['Tweets'] ]) data['Keterangan'] = np.array([ analize_subjectorobject(tweet) for tweet in data['Tweets'] ]) # Import hasil kedalam csv, json and excel data.to_csv('Hasil Analisis/analysis.csv') data.to_json('Hasil Analisis/analysis.json') data.to_excel('Hasil Analisis/analysis.xls')
#Untuk menampilkan data didalam konsol/terminal IPython display(data.head(200)) #Untuk menampilkan persentase Tweets Positif,Negatif dan Neutral pos_tweets = [ tweet for index, tweet in enumerate(data['Tweets']) if data['Sentiment Analysis'][index] > 0] neu_tweets = [ tweet for index, tweet in enumerate(data['Tweets']) if data['Sentiment Analysis'][index] == 0] neg_tweets = [ tweet for index, tweet in enumerate(data['Tweets']) if data['Sentiment Analysis'][index] < 0] print("Percentage of positive tweets: {}%".format(len(pos_tweets)*100/len(data['Tweets']))) print("Percentage of neutral tweets: {}%".format(len(neu_tweets)*100/len(data['Tweets']))) print("Percentage of negative tweets: {}%".format(len(neg_tweets)*100/len(data['Tweets']))) #untuk menampilan Piechart terhadap sentiment analisis showPieChart(positive=len(pos_tweets),neutral=len(neu_tweets),negative=len(neg_tweets))
Output Crawling data dan analysis sentiment dengan query Presiden Jokowi terekstraksi 100 tweet
Data tweet diatas dikelompokan menjadi positif,negatif,neutral agar bisa tercetak dipie chart Tweet Positif
Tweet Negatif
Tweet Neutral
Output dikonsol/terminal IPython Terdapat 100 crawling data dan analisis sentiment pada tweet yang tampil diterminal IPyhton.Serta menampilan Pie Chart
Output File to xls Menyimpan data crawling dan analysis sentiment kemudian diconvert ke file xls...