-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstocks.py
85 lines (67 loc) · 2.64 KB
/
stocks.py
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import requests
import pandas as pd
import json
import flair
import regex as re
sentiment_model = flair.models.TextClassifier.load('en-sentiment')
def get_data(tweet):
data = {
'id': tweet['id_str'],
'created_at': tweet['created_at'],
'text': tweet['full_text']
}
return data
def clean(tweet):
whitespace = re.compile(r"\s+")
web_address = re.compile(r"(?i)http(s):\/\/[a-z0-9.~_\-\/]+")
user = re.compile(r"(?i)@[a-z0-9_]+")
# we then use the sub method to replace anything matching
tweet = whitespace.sub(' ', tweet)
tweet = web_address.sub('', tweet)
tweet = user.sub('', tweet)
return tweet
def stock_analysis(ticker):
BEARER_TOKEN = 'AAAAAAAAAAAAAAAAAAAAAFW9QAEAAAAAnOQAdHMvdF3L0Bc6g8reSOpPAtg%3D5OvcbaeX2HoXqlhzQdtkVImgjwPLDmvYmYRMMcP2QXAUHNVVLf'
params = {
'q': ticker,
'tweet_mode': 'extended',
'lang': 'en',
'count': '100'
}
response = requests.get(
'https://api.twitter.com/1.1/search/tweets.json', params=params,
headers={
'authorization': 'Bearer ' + BEARER_TOKEN
})
df = pd.DataFrame()
for tweet in response.json()['statuses']:
row = get_data(tweet)
df = df.append(row, ignore_index=True)
# we will append probability and sentiment preds later
probs = []
sentiments = []
# use regex expressions (in clean function) to clean tweets
df['text'] = df['text'].apply(clean)
# sentiment_model = flair.models.TextClassifier.load('en-sentiment')
for tweet in df['text'].to_list():
# make prediction
sentence = flair.data.Sentence(tweet)
sentiment_model.predict(sentence)
# extract sentiment prediction
probs.append(sentence.labels[0].score) # numerical score 0-1
sentiments.append(sentence.labels[0].value) # 'POSITIVE' or 'NEGATIVE'
# add probability and sentiment predictions to tweets dataframe
df['probability'] = probs
df['sentiment'] = sentiments
if(len(df)!=0):
counts = df['sentiment'].value_counts().to_dict()
for key, value in counts.items():
if (key == 'POSITIVE'):
positives = int(value / len(df) * 100)
if (key == 'NEGATIVE'):
negatives = int(value / len(df) * 100)
message = ""
message += ("Here's what people are saying about " + ticker + ":" + "\n" "POSITIVE: " + str(positives) + "%" + "\n" "NEGATIVE: " + str(negatives) + "%")
return message
else:
return "Sorry your request could not be completed."