forked from Shamya/multiLingRep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing_italian.py
326 lines (248 loc) · 11 KB
/
testing_italian.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
import pandas as pd
import csv
import numpy as np
import gensim
from libraries.tweetUtilities import tweetTokenizer, tweetPreProcess
from libraries.sentiment_classifier import AvgVector, vector, classifyusingAvgVectors, w2vec_model, classify, sentimentAnalysisSpanishDataset
import math
import io
from numpy import dot
from numpy.linalg import norm
from libraries.fileUtilities import load_pkl_file
from numpy import array
def cosineSimilarity(a,b):
if(norm(a) == 0 or norm(b) == 0):
return 0
return dot(a, b)/(norm(a)*norm(b))
DATA = "data/semevalSimilarity/test/"
OUTPUT = "data/semevalSimilarity/test/"
def accuracy_averaged(result, GoldOutput):
avg_error = 0
cnt = len(result)
for ind in range(len(result)):
avg_error += (abs(result[ind] - GoldOutput[ind]))
print avg_error/cnt
return avg_error/cnt
def vector(model, word):
if word in model:
return array(model[word])
else:
return np.zeros(300)
def Similarity(model, testfile, goldstandardfile):
# model = w2vec_model(language)
outputfile = goldstandardfile
filename = testfile
f = io.open(DATA+filename, encoding="utf-8")
p_data = pd.read_csv(f, sep='\t')
Output = []
for i in xrange(len(p_data)):
result = 4.0 * cosineSimilarity(vector(model, p_data['word1'][i].lower()), vector(model, p_data['word2'][i].lower()))
print p_data['word1'][i], p_data['word2'][i], result
Output.append(result)
filename = outputfile
f = io.open(OUTPUT+filename, encoding="utf-8")
gold_output_data = pd.read_csv(f, sep='\t')
# Similarity(en_es_model, "subtask2-crosslingual/data/en-es.test.data.txt", "subtask2-crosslingual/keys/en-es.test.gold.txt");
def w2vec_model(filename):
print "LOADING WORD2VEC MODEL - " + filename
model = gensim.models.Word2Vec.load("data/" + filename)
# model = gensim.models.Word2Vec.load_word2vec_format('../data/word_vectors_eng.txt', binary=False)
print "LOADED WORD2VEC MODEL - " + filename
return model
def load_factorie_model(filename):
model = load_pkl_file(filename)
return model
def sentimentAnalysisEnglishDataset():
FILE = 'data/EnglishSentimentDataSet/English_sentiment_twitter.tsv'
X = []
Y = []
with open(FILE, 'rU') as csvfile:
spamreader = csv.reader(csvfile, dialect=csv.excel_tab, delimiter='\t')
for row in spamreader:
if( len(row[1:]) > 1):
Tweet = ' '.join(row[1:])
if(Tweet.strip()=='Not Available'):
continue
else:
if(row[1]=='Not Available'):
continue
Tweet = row[1]
print Tweet
print Tweet.strip()
if(row[0]=='positive'):
Y.append(1)
elif(row[0]=='neutral'):
Y.append(0)
elif(row[0]=='negative'):
Y.append(-1)
else:
assert False
X.append(tweetPreProcess(Tweet,'english'));
return np.array(X), Y
def sentimentAnalysisItalianDataset():
FILE = 'data/ItalianSentimentDataSet/italian_sentiment_twitter.csv'
X = []
Y = []
with open(FILE, 'rU') as csvfile:
spamreader = csv.reader(csvfile, dialect=csv.excel_tab, delimiter=',')
for row in spamreader:
if( len(row[2:]) > 1):
Tweet = ' '.join(row[2:])
if(Tweet.strip()=='Not Available'):
continue
else:
if(row[2]=='Not Available'):
continue
Tweet = row[2]
if(row[0]=='0'):
Y.append(0)
elif(row[0]=='1'):
Y.append(1)
else:
assert False
t = tweetPreProcess(Tweet,'italian')
print t
X.append(t);
return np.array(X), Y
X,Y = sentimentAnalysisItalianDataset()
indices_pos = [i for i, e in enumerate(Y) if e == 1]
indices_neg = [i for i, e in enumerate(Y) if e == 0]
neg_len = len(indices_neg)
pos_len = len(indices_pos)
train_X_neg = X[indices_neg][:neg_len/2]
train_X_pos = X[indices_pos][:pos_len/2]
train_Y_neg = np.full((len(train_X_neg)), 0)
train_Y_pos = np.full((len(train_X_pos)), 1)
test_X_neg = X[indices_neg][neg_len/2:]
test_X_pos = X[indices_pos][pos_len/2:]
test_Y_neg = np.full((len(test_X_neg)), 0)
test_Y_pos = np.full((len(test_X_pos)), 1)
train_X = np.concatenate((train_X_neg,train_X_pos), axis=0)
train_Y = np.concatenate((train_Y_neg,train_Y_pos), axis=0)
test_X = np.concatenate((test_X_neg,test_X_pos), axis=0)
test_Y = np.concatenate((test_Y_neg,test_Y_pos), axis=0)
entire_italian_X = np.concatenate((train_X,test_X), axis=0)
entire_italian_Y = np.concatenate((train_Y,test_Y), axis=0)
train_italian_df = pd.DataFrame({'Sentence':(train_X),'ClassifiedOutput':(train_Y)})
test_italian_df = pd.DataFrame({'Sentence':(test_X),'ClassifiedOutput':(test_Y)})
entire_italian_df = pd.DataFrame({'Sentence':(entire_italian_X),'ClassifiedOutput':(entire_italian_Y)})
X,Y = sentimentAnalysisEnglishDataset()
indices_pos = [i for i, e in enumerate(Y) if e == 1]
indices_neg = [i for i, e in enumerate(Y) if e == -1]
indices_neu = [i for i, e in enumerate(Y) if e == 0]
neu_len = len(indices_neu)
neg_len = len(indices_neg)
pos_len = len(indices_pos)
train_X_neu = X[indices_neu][:neu_len/2]
train_X_neg = X[indices_neg][:neg_len/2]
train_X_pos = X[indices_pos][:pos_len/2]
train_Y_neu = np.full((len(train_X_neu)), 0)
train_Y_neg = np.full((len(train_X_neg)), 0)
train_Y_pos = np.full((len(train_X_pos)), 1)
test_X_neu = X[indices_neu][neu_len/2:]
test_X_neg = X[indices_neg][neg_len/2:]
test_X_pos = X[indices_pos][pos_len/2:]
test_Y_neu = np.full((len(test_X_neu)), 0)
test_Y_neg = np.full((len(test_X_neg)), 0)
test_Y_pos = np.full((len(test_X_pos)), 1)
train_X = np.concatenate((train_X_neg,train_X_pos), axis=0)
train_Y = np.concatenate((train_Y_neg,train_Y_pos), axis=0)
test_X = np.concatenate((test_X_neg,test_X_pos), axis=0)
test_Y = np.concatenate((test_Y_neg,test_Y_pos), axis=0)
entire_english_X = np.concatenate((train_X,test_X), axis=0)
entire_english_Y = np.concatenate((train_Y,test_Y), axis=0)
train_english_df = pd.DataFrame({'Sentence':(train_X),'ClassifiedOutput':(train_Y)})
test_english_df = pd.DataFrame({'Sentence':(test_X),'ClassifiedOutput':(test_Y)})
entire_english_df = pd.DataFrame({'Sentence':(entire_english_X),'ClassifiedOutput':(entire_english_Y)})
train_spanish_df, test_spanish_df, entire_spanish_df = sentimentAnalysisSpanishDataset()
en_es_model = load_factorie_model('en_fr_it_es_word_vectors_com.txt')
print "Trained on Italian , Tested on Italian"
train_df = train_italian_df
test_df = test_italian_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on English , Tested on Italian"
train_df = train_english_df
test_df = entire_italian_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on spanish , Tested on Italian"
train_df = train_spanish_df
test_df = entire_italian_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
es_en_it_train_df = pd.concat([train_english_df, train_spanish_df, train_italian_df], ignore_index=True)
es_en_it_test_df = pd.concat([test_english_df, test_spanish_df, test_italian_df], ignore_index=True)
es_en_it_entire_df = pd.concat([entire_english_df, entire_spanish_df, entire_italian_df], ignore_index=True)
print "Trained on spanish+english+Italian, Tested on Italian"
train_df = es_en_it_train_df
test_df = test_italian_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on spanish+english+Italian , Tested on English"
train_df = es_en_it_train_df
test_df = test_english_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on spanish+english+Italian , Tested on Spanish"
train_df = es_en_it_train_df
test_df = test_spanish_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on spanish+english+Italian , Tested on spanish+english+Italian"
train_df = es_en_it_train_df
test_df = es_en_it_test_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "USING ACS **********************************************************"
# USING ACS GETTING RESULTS :
en_es_model = load_factorie_model('acs_en_fr_it_es_word_vectors_model.txt')
print "Trained on Italian , Tested on Italian"
train_df = train_italian_df
test_df = test_italian_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on English , Tested on Italian"
train_df = train_english_df
test_df = entire_italian_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on spanish , Tested on Italian"
train_df = train_spanish_df
test_df = entire_italian_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on spanish+english+Italian, Tested on Italian"
train_df = es_en_it_train_df
test_df = test_italian_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on spanish+english+Italian , Tested on English"
train_df = es_en_it_train_df
test_df = test_english_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on spanish+english+Italian , Tested on Spanish"
train_df = es_en_it_train_df
test_df = test_spanish_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)
print "Trained on spanish+english+Italian , Tested on spanish+english+Italian"
train_df = es_en_it_train_df
test_df = es_en_it_test_df
print "Length of Train : ", str(len(train_df))
print "Length of Test : ", str(len(test_df))
classifyusingAvgVectors(train_df,test_df,dimensionOfVector=300, model=en_es_model)