-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTweet_Analysis.py
49 lines (40 loc) · 1.46 KB
/
Tweet_Analysis.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
import tensorflow as tf
import keras
import csv
import tweepy
import numpy as np
import ssl
import re
import twitter_secrets
ssl._create_default_https_context = ssl._create_unverified_context
# Oauth keys
consumer_key = twitter_secrets.consumer_key
consumer_secret = twitter_secrets.consumer_secret
access_token = twitter_secrets.access_token
access_token_secret = twitter_secrets.access_token_secret
# Authentication with Twitter
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
# model being loaded
model = keras.models.load_model("ltsm_model_5_epoch_better.tf")
def tweet_analyzer(name: str, tweet_id: str):
replies = []
text = []
for tweet in tweepy.Cursor(api.search, q='to:'+name, result_type='recent', timeout=999999).items(50):
replies.append((tweet.created_at,tweet.text))
for reply in replies:
temp = ' '.join(re.sub("(@[A-Za-z0-9]+)|([^0-9A-Za-z \t])|(\w+:\/\/\S+)"," ",reply[1]).split())
if len(temp) > 10:
text.append(temp)
preds = []
with tf.device("CPU:0"):
preds = model.predict(text)
average_sentiment_score = np.average(preds)
score_reply = []
for i in range(len(text)):
print('Score', 'Replies')
print(preds[i],text[i])
score_reply.append((preds[i],text[i]))
print('Average Score: {}'.format(average_sentiment_score))
return average_sentiment_score, score_reply