-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtweet2vec.py
295 lines (217 loc) · 9.43 KB
/
tweet2vec.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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
from giovanniScripts import clean_en_txt, clean_es_txt
from sklearn.feature_extraction.text import TfidfVectorizer
import numpy as np
import os, csv , io
def load_vectors(fname):
print('Loading vectors ... ')
fin = io.open(fname, 'r', newline='\n', errors='ignore')
n, d = map(int, fin.readline().split())
data = {}
for line in fin:
tokens = line.rstrip().split(' ')
'''
print ("tokens are ", tokens )
('tokens are ', ['link', '0.0526', '0.1061', '-0.0083', '0.0913', '0.0443',...., '0.0270']) '''
data[tokens[0]] = map(float, tokens[1:])
print('Loading vectors Done')
return data
def tweet2vec(wordVec, clean_tweet_tokens):
'''
:param wordVec: a dictionary for the word: vector
:param tweet: the author tweet to be vectorized
tfidf
:return: the tweet vector which is >>>in our supposition >>>>the average of word2vec vectors multiplied by the word tfIdf
'''
foundwords= 0
sum =[0]*300 #vector of zeors in dimention of
words_count = len(clean_tweet_tokens)
print('words_count', words_count)
for word in clean_tweet_tokens:
#wrd = word.encode("utf-8")
wrd=word
try:
print(wrd)
w_vec = wordVec[wrd]
w_vec = list(w_vec)
print("word found with a value ", w_vec)
if (len(w_vec) != 0):
foundwords += 1
sum = np.sum([sum, w_vec], axis=0)
print('cur sum', sum)
except KeyError:
print(wrd, "does not exist in the words vectors !!!")
continue
#average
if(foundwords > 0):
avg = [x / foundwords for x in sum] # average over found vectors
else:
avg=[0] * 300
return avg , words_count , foundwords
####=======================================================================================
def tfIdf_weighted_tweet2vec(wordVec, clean_tweet_tokens, user_word_tfIdf):
'''
:param wordVec: a dictionary for the word: vector
:param tweet: the author tweet to be vectorized
tfidf
:return: the tweet vector which is >>>in our supposition >>>>the average of word2vec vectors multiplied by the word tfIdf
'''
foundwords = 0
sum = [0] * 300 # vector of zeors in dimention of 300 = word vector dim
words_count = len(clean_tweet_tokens)
print('words_count', words_count)
for word in clean_tweet_tokens:
# wrd = word.encode("utf-8")
wrd = word
try:
print(wrd)
w_vec = wordVec[wrd]
w_vec = list(w_vec)
print("word found with a value " , w_vec)
if (len(w_vec) != 0):
foundwords += 1
#multiply by tfIdf
wrd_tfIdf= user_word_tfIdf[wrd]
w_vec= [ i* wrd_tfIdf for i in w_vec]
sum = np.sum([sum, w_vec], axis=0)
#print('cur sum', sum)
except KeyError:
print(wrd, "does not exist in the words vectors !!!")
continue
# average
if (foundwords > 0):
avg = [x / foundwords for x in sum] # average over found vectors
else:
avg = [0] * 300
return avg, words_count, foundwords
def user_word_tfIdf(curr_author, authors):
# each author dict contains: lang, Id, lable, tweets
#this function will get the user tweets, rest_users_tweets : clean them : then find the tfidf for each word
user_id = curr_author["Id"]
user_lang = curr_author["lang"]
word_tfIdf = {}
if (user_lang == "en"):
user_doc = clean_en_txt().tokenize(str(curr_author["tweets"]))
other_users_docs = [user["tweets"] for user in authors if
user["Id"] != user_id and user["lang"] == "en"]
#clean
#other_users_docs=clean_en_txt().tokenize(str(other_users_docs)) ### too much memory
elif (user_lang == "es"):
other_users_docs = [user["tweets"] for user in authors if
user["Id"] != user_id and user["lang"] == "es"]
user_doc = clean_es_txt().tokenize(str(curr_author["tweets"]))
#other_users_docs = clean_es_txt().tokenize(str(other_users_docs))
else:
print("language", user_lang, "has not been intialized yet")
vectorizer = TfidfVectorizer()
response = vectorizer.fit_transform([str(user_doc), str(other_users_docs)])
word_index=vectorizer.vocabulary_ #dict { 'car': 19, 'is': 10, 'driven': 1,......}
#print(word_index)
user_tfIdf_array=response.toarray()[0]#[0] is the array of user_doc
for word,index in word_index.items():
word_tfIdf[word] =user_tfIdf_array[index]
#print(word_tfIdf) {'car': 0.30814892740486904, 'is': 0.30814892740486904, 'driven': 0.30814892740486904, ,,,,}
return word_tfIdf
def user2vec(wordVec, clean_tweets_tokens, user_word_tfIdf):
foundwords = 0
sum = [0] * 300 # vector of zeors in dimention of
words_count = len(clean_tweets_tokens)
print('words_count', words_count)
for word in clean_tweets_tokens:
# wrd = word.encode("utf-8")
wrd = word
try:
w_vec = wordVec[wrd]
w_vec = list(w_vec)
if (len(w_vec) != 0):
foundwords += 1
# multiply by tfIdf
wrd_tfIdf = user_word_tfIdf[wrd]
w_vec = np.array(w_vec) * wrd_tfIdf
sum = np.sum([sum, w_vec], axis=0)
print('cur sum', sum)
except KeyError:
print(wrd, "does not exist in the words vectors !!!")
continue
# average
if (foundwords > 0):
avg = [x / foundwords for x in sum] # average over found vectors
else:
avg = [0] * 300
return avg, words_count, foundwords
#==================================================================
'''
if __name__ == "__main__":
###read the input (train) data:
# --------------------------------
# training_path = config.Data['train_data_path']
training_path = "Data/pan19-author-profiling-training-2019-01-28"
# langs = config.Data['languages']
langs = ['en']
authors = loadAuthors(training_path, langs)
### intialize the output csv file if does not exist before:
# ------------------------------------------------------------
tweet2vec_fname = "Data/simple_tweet2vec_allusers_bigfile_es_001.csv"
exists = os.path.isfile(tweet2vec_fname)
if (exists):
print("output csv file already exist, will append to it!! ")
else:
# user2vec:
# row=['user_Id', 'class' ,'clean_words_count',
# 'vectorized_words_count', 'tweet_vector']
# if Tweet2vec row is :
row = ['user_Id', 'class', 'tweet_num', 'orginal_tweet', 'clean_tweet_tokens', 'clean_words_count',
'vectorized_words_count', 'tweet_vector']
with open(tweet2vec_fname, 'a') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
### load fastText word vectors
# --------------------------------
word2vec_fname = "Data/fastText/cc.en.300.vec" # English big file
words_vectors_data = load_vectors(word2vec_fname)
# words_vectors_data= dict()
# tweet2vec_file = open(tweet2vec_fname, 'a')
###clean tweet and calculate the tweets vectors:
# ------------------------------------------
global_found_words = 0
global_words_count = 0
# tweet2vec_file.write('{')
# if Tweet2vec :
for x in authors:
print('USER', x['Id'])
tweets = x['tweets']
lang = x["lang"]
tweet_vec = []
tweet_num = 0
for orginal_tweet in tweets:
tweet_num += 1
print('tweet is', orginal_tweet)
# clean the tweet
if (lang == "en"):
clean_tweet_tokens = clean_en_txt().tokenize(orginal_tweet)
elif (lang == "es"):
clean_tweet_tokens = clean_es_txt().tokenize(orginal_tweet)
else:
print("language", lang, "has not been initialized yet")
### if weighted tweet2vec:
# author_word_tfIdf = user_word_tfIdf(x, authors)
# tweet_vector, words_count, vectorized_words_count = tfIdf_weighted_tweet2vec(words_vectors_data,
# clean_tweet_tokens,
# author_word_tfIdf)
# else
tweet_vector, words_count, vectorized_words_count = tweet2vec(words_vectors_data, clean_tweet_tokens)
print("tweet vector:", tweet_vector)
global_found_words += vectorized_words_count
global_words_count += words_count
# write a new row for this tweet in csv file
row = [x['Id'], x['lable'], tweet_num, orginal_tweet.encode("utf-8"), clean_tweet_tokens,
words_count,
vectorized_words_count, tweet_vector]
with open(tweet2vec_fname, 'a') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
print("new Row added to file")
print("curr global_words_count", global_words_count)
print("curr global_found_words", global_found_words)
ratio = (float(global_found_words)) / global_words_count
print("global ratio of founded words is", ratio)
'''