-
Notifications
You must be signed in to change notification settings - Fork 1
/
pa_sentiment_analysis.txt
73 lines (59 loc) · 1.94 KB
/
pa_sentiment_analysis.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# -*- coding: utf-8 -*-
"""PA: Sentiment Analysis.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1Tylhg6mMPb5xQX8xAlKQt6fsOaUNvpbx
"""
#Description: A Sentiment Analysis Program
import tweepy
from textblob import TextBlob
#from wordcloud import WordCloud
#import pandas as pd
#import numpy as np
#import matplotlib.pyplot as plt
#plt.style.use('fivethirtyeight')
#loading data
from google.colab import files
uploaded = files.upload()
#get data
mykeys = open('keys.txt','r').read().splitlines()
# Twitter API Credentials
api_key = mykeys[0].split()[-1]
api_key_secret = mykeys[1].split()[-1]
access_token = mykeys[2].split()[-1]
access_token_secret = mykeys[3].split()[-1]
# Connecting to the API
auth_handler = tweepy.OAuthHandler(consumer_key = api_key, consumer_secret = api_key_secret)
#set access to the access token
auth_handler.set_access_token(access_token, access_token_secret)
# creating the API
api = tweepy.API(auth_handler)
#search_term = 'stocks'
search_term = input('\nEnter the word to search: ')
tweet_amount = 200
polarity = 0 #indicates if the text is positive or negative
positive, negative, neutral = 0, 0, 0
tweets = tweepy.Cursor(api.search, q = search_term, lang = 'en').items(tweet_amount)
for tweet in tweets:
final_text = tweet.text.replace('RT', '')
if final_text.startswith(' @'):
position = final_text.index(':')
final_text = final_text[position+2:]
if final_text.startswith('@'):
position = final_text.index(' ')
final_text = final_text[position+2:]
#print(final_text)
analysis = TextBlob(final_text)
polarity += analysis.polarity
tweet_polarity = analysis.polarity
if tweet_polarity > 0:
positive += 1
elif tweet_polarity < 0:
negative += 1
else:
neutral += 1
polarity += tweet_polarity
print('overall polarity ',polarity)
print('positive pol ', positive)
print('negative pol ', negative)
print('neutral pol ', neutral)