-
Notifications
You must be signed in to change notification settings - Fork 0
/
Twitter User Sentiment.py
69 lines (56 loc) · 2.59 KB
/
Twitter User Sentiment.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
import tweepy
import pandas as pd
import sys
import numpy as np
from preprocess_tweet import preprocess
from initialize_expertai_client import ExpertAiClient
client = ExpertAiClient()
"""authentication function"""
def twitter_api():
try:
consumer_key = "1NjIffys"
consumer_secret = "hfmkzvsZUOJBaN4ZIxFfcDv"
access_token = "1304687571HFsjPXfN"
access_secret = "BDax0LsCbgfB56VUAP1cFZxqB6OhIM4"
except KeyError:
sys.stderr.write("TWITTER_* envirnoment variable not set\n")
sys.exit(1)
auth = tweepy.OAuthHandler(consumer_key,consumer_secret)
auth.set_access_token(access_token,access_secret)
api = tweepy.API(auth,wait_on_rate_limit= True)
return api
def sentiment(text):
"""returns overall sentiment"""
output = client.specific_resource_analysis(body = {"document":{"text": text}},
params = {'language': 'en','resource': 'sentiment'})
return output.sentiment.overall
def named_entity_extraction(text):
"""extract named entities"""
output = client.specific_resource_analysis(body={"document": {"text": text}},
params={'language': 'en', 'resource': 'entities'})
return [entity.lemma for entity in output.entities]
def key_phrase_extraction(text):
"""extract key phrases"""
output = client.specific_resource_analysis(body={"document": {"text": text}},
params={'language': 'en', 'resource': 'relevants'})
return [lemma.value for lemma in output.main_lemmas]
def tweet_user(username):
"""Extracting user information"""
max_tweets = 15
# Creation of query method using parameters
tweets = tweepy.Cursor(api.user_timeline,id=username,tweet_mode = 'extended').items(max_tweets)
tweets_list = [[preprocess(tweet.full_text), tweet.created_at, tweet.id_str, tweet.user.screen_name,] for tweet in tweets]
# print(tweets_list)
# Creation of dataframe from tweets_list
# Did not include column names to simplify code
tweets_df = pd.DataFrame(tweets_list)
tweets_df.columns = ['tweets', 'time', 'twitter_id', 'screen_name',]
tweets_df['tweet_sentiment'] = tweets_df['tweets'].apply(lambda x: sentiment(x))
tweets_df['tweet_key_phrase'] = tweets_df['tweets'].apply(lambda x: key_phrase_extraction(x))
tweets_df['tweet_NER'] = tweets_df['tweets'].apply(lambda x: named_entity_extraction(x))
# print(tweets_df.to_csv(f'{user}'))
if __name__ == '__main__':
api = twitter_api()
users = ['@mipaltan']
for user in users:
tweet_user(user)