forked from Paulcy10x/Twitter-Data-Analysis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_dataframe.py
161 lines (118 loc) · 5.96 KB
/
extract_dataframe.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import json
import pandas as pd
from textblob import TextBlob
def read_json(json_file: str)->list:
"""
json file reader to open and read json files into a list
Args:
-----
json_file: str - path of a json file
Returns
-------
length of the json file and a list of json
"""
tweets_data = []
for tweets in open(json_file,'r'):
tweets_data.append(json.loads(tweets))
return len(tweets_data), tweets_data
class TweetDfExtractor:
"""
this function will parse tweets json into a pandas dataframe
Return
------
dataframe
"""
def __init__(self, tweets_list):
self.tweets_list = tweets_list
# an example function
def find_statuses_count(self)->list:
user = [x.get('user', {}) for x in self.tweets_list]
statuses_count = [x.get('statuses_count', 0) for x in user]
return statuses_count
def find_full_text(self)->list:
text = [x.get('retweeted_status', {}).get('extended_tweet',{}).get('full_text', '') \
for x in self.tweets_list]
return text
def find_sentiments(self, text)->list:
text = [x.get('retweeted_status', {}) for x in self.tweets_list]
extended_tweet = [x.get('extended_tweet', {}) for x in text]
full_text = [x.get('full_text', '') for x in extended_tweet]
sentimentedText = [TextBlob(x) for x in full_text]
polarity = []
subjectivity = []
for i in range(len(sentimentedText)):
polarity.append(sentimentedText[i].sentiment.polarity)
subjectivity.append(sentimentedText[i].sentiment.subjectivity)
return polarity, subjectivity
def find_created_time(self)->list:
created_at = [x.get('created_at', None) for x in self.tweets_list]
return created_at
def find_source(self)->list:
source = [x.get('source', '') for x in self.tweets_list]
return source
def find_screen_name(self)->list:
users = [x.get('user', {}) for x in self.tweets_list]
screen_name = [x.get('screen_name') for x in users]
return screen_name
def find_followers_count(self)->list:
followers_count = [x.get('user', {}).get('followers_count', 0) for x in self.tweets_list]
return followers_count
def find_friends_count(self)->list:
friends_count = [x.get('user', {}).get('friends_count', 0) for x in self.tweets_list]
return friends_count
def is_sensitive(self)->list:
is_sensitive = [x.get('possibly_sensitive', None) for x in self.tweets_list]
return is_sensitive
def find_favourite_count(self)->list:
fav_count = [x.get('retweeted_status', {}).get('favorite_count', 0) for x in self.tweets_list]
return fav_count
def find_retweet_count(self)->list:
retweeted_status = [x.get('retweeted_status', {}) for x in self.tweets_list]
retweet_count = [x.get('retweet_count', None) for x in retweeted_status]
return retweet_count
def find_hashtags(self)->list:
hashtags = [x.get('hashtags', None) for x in self.tweets_list]
return hashtags
def find_mentions(self)->list:
mentions = [x.get('mentions', None) for x in self.tweets_list]
return mentions
def find_location(self)->list:
location = [x.get('user', {}).get('location', None) for x in self.tweets_list]
return location
def find_lang(self)->list:
lang = [x.get('retweeted_status', {}).get('lang', None) for x in self.tweets_list]
return lang
def get_tweet_df(self, save=False)->pd.DataFrame:
"""required column to be generated you should be creative and add more features"""
columns = ['created_at', 'source', 'original_text','polarity','subjectivity', 'lang', 'favorite_count', 'retweet_count',
'original_author', 'followers_count','friends_count','possibly_sensitive', 'hashtags', 'user_mentions', 'place']
created_at = self.find_created_time()
source = self.find_source()
text = self.find_full_text()
polarity, subjectivity = self.find_sentiments(text)
lang = self.find_lang()
fav_count = self.find_favourite_count()
retweet_count = self.find_retweet_count()
screen_name = self.find_screen_name()
follower_count = self.find_followers_count()
friends_count = self.find_friends_count()
sensitivity = self.is_sensitive()
hashtags = self.find_hashtags()
mentions = self.find_mentions()
location = self.find_location()
# print(type(created_at), type(source), type(text), type(polarity), type(subjectivity), type(lang), type(fav_count), type(retweet_count), type(screen_name), type(follower_count), type(friends_count), type(sensitivity), type(hashtags), type(mentions), type(location))
# print(type(sensitivity))
data = zip(created_at, source, text, polarity, subjectivity, lang, fav_count, retweet_count, screen_name, follower_count, friends_count, sensitivity, hashtags, mentions, location)
df = pd.DataFrame(data=data, columns=columns)
if save:
df.to_csv('processed_tweet_data.csv', index=False)
print('File Successfully Saved.!!!')
return df
if __name__ == "__main__":
# required column to be generated you should be creative and add more features
columns = ['created_at', 'source', 'original_text','clean_text', 'sentiment','polarity','subjectivity', 'lang', 'favorite_count', 'retweet_count',
'original_author', 'screen_count', 'followers_count','friends_count','possibly_sensitive', 'hashtags', 'user_mentions', 'place', 'place_coord_boundaries']
_, tweet_list = read_json("./data/covid19.json")
tweet = TweetDfExtractor(tweet_list)
tweet_df = tweet.get_tweet_df()
# use all defined functions to generate a dataframe with the specified columns above