-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract_dataframe.py
More file actions
221 lines (185 loc) · 7.23 KB
/
extract_dataframe.py
File metadata and controls
221 lines (185 loc) · 7.23 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import json
import pandas as pd
from textblob import TextBlob
import re
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("data/global_twitter_data.json",'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
print('in progress...')
# an example function
def find_statuses_count(self) -> list:
statuses_count = []
for tweet in self.tweets_list:
statuses_count.append(tweet['user']['statuses_count'])
return statuses_count
def find_retweet_text(self) -> list:
full_text = []
for tweet in self.tweets_list:
try:
full_text.append(
tweet['retweeted_status']['text'])
except KeyError:
full_text.append("")
return full_text
def find_original_text(self) -> list:
text = []
for tweet in self.tweets_list:
try:
text.append(tweet['retweeted_status']
['extended_tweet']['full_text'])
except KeyError:
text.append(tweet['full_text'])
return text
def find_sentiments(self, text: list) -> list:
polarity = []
subjectivity = []
for tweet in text:
blob = TextBlob(tweet)
sentiment = blob.sentiment
polarity.append(sentiment.polarity)
subjectivity.append(sentiment.subjectivity)
return polarity, subjectivity
def find_sentiment_polarity(self, polarity, subjectivity) -> list:
sentiment = []
for i in range(len(polarity)):
if polarity[i] > 0:
sentiment.append(1)
elif polarity[i] < 0:
sentiment.append(0)
else:
sentiment.append(-1)
return sentiment
def find_created_time(self) -> list:
created_at = []
for time in self.tweets_list:
created_at.append(time['created_at'])
return created_at
def find_source(self) -> list:
source = []
for x in self.tweets_list:
source.append(x['source'])
return source
def find_screen_name(self) -> list:
screen_name = []
for x in self.tweets_list:
screen_name.append(x['user']['screen_name'])
return screen_name
def find_followers_count(self) -> list:
followers_count = []
for x in self.tweets_list:
if 'retweeted_status' in x.keys():
followers_count.append(
x['retweeted_status']['user']['followers_count'])
else:
followers_count.append(0)
return followers_count
def find_friends_count(self) -> list:
friends_count = []
for x in self.tweets_list:
friends_count.append(x['user']['friends_count'])
return friends_count
def is_sensitive(self) -> list:
is_sensitive = []
for tweet in self.tweets_list:
if 'possibly_sensitive' in tweet.keys():
is_sensitive.append(tweet['possibly_sensitive'])
else:
is_sensitive.append(None)
return is_sensitive
def find_favourite_count(self) -> list:
favorite_count = []
for tweet in self.tweets_list:
if 'retweeted_status' in tweet.keys():
favorite_count.append(
tweet['retweeted_status']['favorite_count'])
else:
favorite_count.append(0)
return favorite_count
def find_retweet_count(self) -> list:
retweet_count = []
for tweet in self.tweets_list:
if 'retweeted_status' in tweet.keys():
retweet_count.append(
tweet['retweeted_status']['retweet_count'])
else:
retweet_count.append(0)
return retweet_count
def find_hashtags(self) -> list:
hashtags = []
for tweet in self.tweets_list:
try:
hashtags.append(tweet['entities']['hashtags'][0]['text'])
except KeyError:
hashtags.append(None)
except IndexError:
hashtags.append(None)
return hashtags
def find_mentions(self) -> list:
mentions = []
for hs in self.tweets_list:
mentions.append(", ".join(
[mention['screen_name'] for mention in hs['entities']['user_mentions']]))
return mentions
def find_lang(self) -> list:
lang = []
for x in self.tweets_list:
lang.append(x['lang'])
return lang
def find_location(self) -> list:
location = []
for tweet in self.tweets_list:
location.append(tweet['user']['location'])
return location
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','retweet_text','sentiment','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_original_text()
retweet_text = self.find_retweet_text()
polarity, subjectivity = self.find_sentiments(text)
sentiment = self.find_sentiment_polarity(polarity, subjectivity)
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()
data = zip(created_at, source, text,retweet_text, sentiment, 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('data/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
_, tweet_list = read_json("data/global_twitter_data.json")
tweet = TweetDfExtractor(tweet_list)
tweet_df = tweet.get_tweet_df(True)
#tweet.find_statuses_count() ##since statuses count is in the test
# use all defined functions to generate a dataframe with the specified columns above