-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataPresentation.py
More file actions
282 lines (236 loc) · 9.23 KB
/
DataPresentation.py
File metadata and controls
282 lines (236 loc) · 9.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
import operator
import re
import string
from collections import Counter
import pprint
import tweepy
import json
from keras_preprocessing.text import Tokenizer
from nltk.stem import PorterStemmer
import pandas as pd
import numpy
from nltk.corpus import stopwords
class DataPresentation:
def __init__(self):
self.__DB_path = 'gender-classifier-DFE-791531.csv'
self.__emoticons_str = r"""
(?:
[:=;] # Eyes
[oO\-]? # Nose (optional)
[D\)\]\(\]/\\OpP] # Mouth
)"""
self.__regex_str = [
self.__emoticons_str,
r'<[^>]+>', # HTML tags
r'(?:@[\w_]+)', # @-mentions
r"(?:\#+[\w_]+[\w\'_\-]*[\w_]+)", # hash-tags
r'http[s]?://(?:[a-z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-f][0-9a-f]))+', # URLs
r'(?:(?:\d+,?)+(?:\.?\d+)?)', # numbers
r"(?:[a-z][a-z'\-_]+[a-z])", # words with - and '
r'(?:[\w_]+)', # other words
r'(?:\S)' # anything else
]
self.__tokens_re = re.compile(r'(' + '|'.join(self.__regex_str) + ')', re.VERBOSE | re.IGNORECASE)
self.__emoticon_re = re.compile(r'^' + self.__emoticons_str + '$', re.VERBOSE | re.IGNORECASE)
self.data_frame = None
self.ps = PorterStemmer()
def read_csv(self, print_graph=True):
"""
Read the csv file and present it
:return: representation of the
"""
data = pd.read_csv(self.__DB_path, encoding='latin-1')
if print_graph:
pd.value_counts(data['gender']).plot.bar()
return data
def build_terms(self, data_frame):
"""
Build lists of terms used by male/female/brand
:param: data_frame data frame that holds the text and gender fields of the DB
:return:
"""
terms_male = []
terms_female = []
terms_brand = []
punctuation = list(string.punctuation)
stop_words = stopwords.words('english') + punctuation + ['rt', 'via']
for index, line in data_frame.iterrows():
tokens = self.preprocess(line['text'], lowercase=True)
for token in tokens:
if token not in stop_words:
token = self.ps.stem(token)
if line['gender'] == 'male':
terms_male.append(token)
if line['gender'] == 'female':
terms_female.append(token)
if line['gender'] == 'brand':
terms_brand.append(token)
return terms_male, terms_female, terms_brand
def tokenize(self, tweet_text):
"""
Tokenize the text of a tweet
:param tweet_text: text filed of the tweet
:return: list of tokens
"""
tweet_text = re.sub(r'[^\x00-\x7f]*', r'', tweet_text)
return self.__tokens_re.findall(tweet_text)
def preprocess(self, tweet_text, lowercase=False):
"""
Pre-process the text and tokens.
:param tweet_text: the text to process
:param lowercase: boolean, true if wanted to change all text to lowercase, false otherwise
:return: list of clean tokens
"""
tokens = self.tokenize(tweet_text)
if lowercase:
tokens = [token if self.__emoticon_re.search(token) else token.lower() for token in tokens]
return tokens
def data_to_df(self, data):
"""
convert the data to data frame and clean it up
:param data: data read from csv file
:return: clean data frame
"""
self.data_frame = data[['text', 'gender']]
self.data_frame.dropna(inplace=True)
self.data_frame = self.data_frame[self.data_frame.gender != 'unknown']
return self.data_frame
def print_common(self):
"""
Working on the data frame to create list of terms for male, female and brand.
Print the top 20 of each list
"""
terms_male, terms_female, terms_brand = self.build_terms(self.data_frame)
pp = pprint.PrettyPrinter()
count_male = Counter()
count_male.update(terms_male)
print('Male most common terms: ', sum(count_male.values()))
pp.pprint(count_male.most_common(20))
print('=' * 80)
count_female = Counter()
count_female.update(terms_female)
print('Female most common terms: ', sum(count_female.values()))
pp.pprint(count_female.most_common(20))
print('=' * 80)
count_brand = Counter()
count_brand.update(terms_brand)
print('Brand most common terms: ', sum(count_brand.values()))
pp.pprint(count_brand.most_common(20))
@staticmethod
def find_most_common_country(data, top):
countries = data['tweet_location'].value_counts()
top_countries = []
for loc, num in countries.items():
top_countries.append((loc, num))
if top < len(top_countries):
return top_countries[0:top]
else:
return top_countries
@staticmethod
def collect_tweets(file_location):
api_key = ""
api_secret_key = ""
access_token = ""
access_token_secret = ""
authenticator = tweepy.OAuthHandler(api_key, api_secret_key)
authenticator.set_access_token(access_token, access_token_secret)
twitter_api = tweepy.API(authenticator)
stream_listener = TwitterStreamer(file_location)
my_twitter_stream = tweepy.Stream(auth=twitter_api.auth, listener=stream_listener)
my_twitter_stream.filter(locations=[-0.510375, 51.28676, 0.334015, 51.691874], is_async=True)
def process_tweets(self, file_location, filter_words=True):
"""
Processes the tweets in the same manner as in Question 1
:param filter_words:
:param file_location: Location of the tweets json
"""
tweet_data = []
tweets_json = open(file_location, 'r')
for line in tweets_json:
if line == '\n':
continue
try:
curr_tweet = json.loads(line)
tweet_data.append(curr_tweet['text'])
except Exception as ex:
#print(ex)
continue
parsed_tweets = []
for dirty_tweet in tweet_data:
tweet_token = self.preprocess(dirty_tweet, lowercase=True)
parsed_tweets.append(tweet_token)
clean_tweets = []
term_freq = {}
punctuation = list(string.punctuation)
stop_words = stopwords.words('english') + punctuation + ['rt', 'via']
for parsed_tweet in parsed_tweets:
txt = ''
for token in parsed_tweet:
txt = txt + token + " " # Todo: concatenate the dirty token or parsed token?
if filter_words and token in stop_words:
continue
token = self.ps.stem(token)
if token not in term_freq:
term_freq[token] = 1
else:
term_freq[token] += 1
clean_tweets.append(txt)
return clean_tweets, term_freq
@staticmethod
def get_most_common_words(tweet_distribution):
sorted_tweets = []
for key, value in tweet_distribution.items():
sorted_tweets.append((key, value))
sorted_tweets.sort(key=operator.itemgetter(1), reverse=True)
return sorted_tweets
def predict_gender(self, model, parsed_tweets, tokenizer, lb_make):
"""
Receives a list of parsed tweets and predicts for each one the gender of the user
:param lb_make:
:param tokenizer:
:param model: A classifier
:param parsed_tweets: A list of parsed tweets
:return: The prediction for each tweet.
"""
tweet_array = numpy.asarray(parsed_tweets)
tweet_matrix = tokenizer.texts_to_matrix(tweet_array, mode='count')
predictions = model.predict_classes(tweet_matrix)
predictions = lb_make.inverse_transform(predictions)
return predictions
@staticmethod
def aggregate_predictions(predictions):
results = {
"Male": 0,
"Female": 0,
"Brand": 0
}
for pred in predictions:
results[pred] += 1
return results
class TwitterStreamer(tweepy.StreamListener):
"""
Listener class inheriting from tweepy's StreamListener class.
"""
def __init__(self, tweet_file):
super(TwitterStreamer, self).__init__()
self.file_location = tweet_file
self.num_of_tweets = 0
def on_connect(self):
print("connected")
return True
def on_data(self, data):
if self.num_of_tweets < 15000:
try:
with open(self.file_location, 'a') as tweets:
tweets.write(data)
self.num_of_tweets += 1
if self.num_of_tweets % 100 == 0:
print(f'Collected {self.num_of_tweets} tweets')
return True
except BaseException as bex:
print("Error on_data: " + str(bex))
def on_error(self, status):
if status == 420:
# returning False in on_data disconnects the stream
return False
return True