Sentiment
Analysis Twitter kategori Black Campaign
Twitter
adalah situs web dimiliki dan dioperasikan oleh Twitter, Inc., yang menawarkan jaringan
sosial berupa microblog. Disebut microblog karena situs ini
memungkinkan penggunanya mengirim dan membaca pesan blog seperti pada umumnya
namun terbatas hanya sejumlah 140 karakter yang ditampilkan pada halaman profil
pengguna. Twitter memiliki karakteristik dan format penulisan yang unik dengan
simbol ataupun aturan khusus.Pesan dalam Twitter dikenal dengan sebutan tweet.
Analisis
sentimen, yang disebut juga dengan opinion mining, merupakan salah satu
cabang ilmu dari data mining yang bertujuan untuk menganalisis, memahami,
mengolah, dan mengekstrak data tekstual yang berupa opini terhadap entitas
seperti produk, servis, organisasi, individu, dan topik tertentu [8]. Analisis
ini digunakan untuk mendapatkan suatu informasi tertentu dari suatu kumpulan
data yang ada. Analisis sentimen berfokus pada pengolahan opini yang mengandung
polaritas, yaitu memiliki nilai sentimen positif ataupun negatif.
2. Lakukan
pendaftaran Twitter Apps pada https://apps.twitter.com , untuk mendapatkan key
untuk mengakses twiiter, jangan lupa harus memiliki akun twiiter dan blog /
website
5. Lakukan
Collecting data dan Export data menjadi .txt
Buat
projek baru dan import library dengan menginstall package install di file à
settingàproject
interpreter + modul dan search apa yang dibutuhkan
6. Buat
file baru bernama filetwitter.py berisikan source code sebagai berikut
#Import the necessary
methods from tweepy library
from tweepy.streaming import StreamListener from tweepy import OAuthHandler from tweepy import Stream #Variables that contains the user credentials to access Twitter API consumer_key = 'xxxxxxxxxxxxxxxxxxxxxxxxx' consumer_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' access_token = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx’ access_token_secret = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' #This is a basic listener that just prints received tweets to stdout. class StdOutListener(StreamListener): def on_status(self, status): try: with open('source/prabowo.txt', 'a') as record: record.write(str(status.text) + '\n') return True except BaseException as e: print("Error on_data: %s" % str(e)) print(status.text) return True # def on_direct_message(self, data): # # print(data.text) # return True # def on_data(self, raw_data): # # if status.retweeted_status: # # return # print(raw_data) # return True def on_error(self, status_code): if status_code == 420: return False if __name__ == '__main__': #This handles Twitter authetification and the connection to Twitter Streaming API l = StdOutListener() auth = OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret) stream = Stream(auth, l) #This line filter Twitter Streams to capture data by the keywords: 'python', 'javascript', 'ruby' stream.filter(track=['#prabowo', 'prabowo']) |
Contohnya
seperti diatas, jangan lupa untuk mengisi consumer_key, consumer_secret,
access_token, access_token_secret yang didapat ketika mempuat Twitter App.
8. Lakukan
Sentiment Analysis terhadap data yang berhasil kita kumpulkan dan buat laporan
dalam bentuk csv dengan membuat source code sebagai berikut :
from __future__ import division import csv from string import punctuation positive_word = [] negative_word = [] positive_counts = [] negative_counts = [] clean_tweets = [] header = [("tweets","positif","negatif","posiftif %","negative %")] # header = [("tweets","positif","negatif","clean tweets")] tweets = open("source/prabowo.txt").read() tweets_list = tweets.split('\n') pos_sent = open("source/positive.txt").read() positive_words = pos_sent.split('\n') neg_sent = open("source/negative.txt").read() negative_words = neg_sent.split('\n') for tweet in tweets_list: text_positif=[] text_negatif=[] positive_counter = 0 negative_counter = 0 tweet_procces_2 = tweet.replace('!', '').replace('.', '') tweet_processed = tweet_procces_2.lower() for p in list(punctuation): tweet_processed = tweet_processed.replace(p, '') clean_tweets.append(tweet_processed) words = tweet_processed.split(' ') word_count = len(words) for word in words: if word in positive_words: text_positif.append(word) positive_counter = positive_counter+1 elif word in negative_words: text_negatif.append(word) negative_counter = negative_counter+1 positive_counts.append((positive_counter / word_count)*100) negative_counts.append((negative_counter / word_count)*100) positive_word.append(text_positif) negative_word.append(text_negatif) # output = zip(tweets_list, positive_counts, negative_counts, clean_tweets) output = zip(clean_tweets, positive_word, negative_word, positive_counts, negative_counts) title = header writer = csv.writer(open('source/sentiment_analysis_result.csv', 'wb')) writer.writerows(title) writer.writerows(output) |


maaf, mau nanya itu hasil tweet positif dan negatifnya perkalimat belum dilabelin ya?
BalasHapus