forked from candlewill/document_rating
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfusion_model_vader.py
299 lines (237 loc) · 8.64 KB
/
fusion_model_vader.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
__author__ = 'NLP-PC'
def linear_fusion(corpus, lexicon, mark):
valence_mean = []
valence_true = []
def VA_mean(text):
sum_valence = 0
count = 0
word_list = text.split()
for word in word_list:
for line in lexicon:
if word == line:
count = count + 1
sum_valence = sum_valence + lexicon[line]
return 5 if count == 0 else sum_valence / count
for i, text in enumerate(corpus):
V = VA_mean(text)
valence_mean.append(V)
valence_true.append(mark[i])
return valence_mean, valence_true
from load_data import load_pickle
idfs = load_pickle('./data/vocab_idf.p')
def tfidf(t, d):
d = d.split()
tf = float(d.count(t)) / sum(d.count(w) for w in set(d))
# idf = sp.log(float(len(D)) / (len([doc.split() for doc in D if t in doc.split()])))
return tf * idfs[t]
def tf(t, d):
d = d.split()
tf = float(d.count(t)) / float(len(d))
return tf
def linear_fusion_sqr(corpus, lexicon, mark):
valence_mean = []
valence_true = []
def VA_mean(text):
sum_valence = 0
sum_valence_sqr = 0
count = 0
word_list = text.split()
for word in word_list:
for line in lexicon:
if word == line:
count = count + 1
sum_valence_sqr = sum_valence_sqr + lexicon[line] ** 2
sum_valence = sum_valence + lexicon[line]
return 5 if count == 0 else sum_valence_sqr / sum_valence
for i, text in enumerate(corpus):
V = VA_mean(text)
valence_mean.append(V)
valence_true.append(mark[i])
return valence_mean, valence_true
def nonlinear_max_fusion(corpus, lexicon, mark):
valence_mean = []
valence_true = []
def VA_mean(text):
max_valence = 0
word_list = text.split()
for word in word_list:
for line in lexicon:
if word == line:
if lexicon[word] > max_valence:
max_valence = lexicon[word]
return 5 if max_valence == 0 else max_valence
for i, text in enumerate(corpus):
V = VA_mean(text)
valence_mean.append(V)
valence_true.append(mark[i])
return valence_mean, valence_true
def linear_fusion_tf(corpus, lexicon, mark):
valence_mean = []
valence_true = []
def VA_mean(text):
sum_valence = 0
count = 0
word_list = text.split()
for word in word_list:
for line in lexicon:
if word == line:
word_tf = tf(word, corpus[i])
count = count + word_tf
sum_valence = sum_valence + word_tf * lexicon[word]
return 5 if count == 0 else sum_valence / count
for i, text in enumerate(corpus):
V = VA_mean(text)
valence_mean.append(V)
valence_true.append(mark[i])
return valence_mean, valence_true
def linear_fusion_tfidf(corpus, lexicon, mark):
valence_pred = []
valence_true = []
def VA_mean(text):
sum_valence = 0
count = 0
word_list = text.split()
for word in word_list:
for line in lexicon:
if word == line:
word_tfidf = tfidf(word, corpus[i])
count = count + word_tfidf
sum_valence = sum_valence + word_tfidf * lexicon[word]
return 5 if count == 0 else sum_valence / count
for i, text in enumerate(corpus):
V = VA_mean(text)
valence_pred.append(V)
valence_true.append(mark[i])
print(valence_true[:200])
print(valence_pred[:200])
return valence_pred, valence_true
def linear_fusion_geo(corpus, lexicon, mark):
valence_pred = []
valence_true = []
def VA_mean(text):
sum_valence = 1
count = 0
word_list = text.split()
for word in word_list:
for line in lexicon:
if word == line:
count = count + 1
sum_valence = sum_valence * lexicon[line]
return 5 if count == 0 else sum_valence ** (1. / count)
for i, text in enumerate(corpus):
V = VA_mean(text)
valence_pred.append(V)
valence_true.append(mark[i])
print(valence_true[:200])
print(valence_pred[:200])
return valence_pred, valence_true
def linear_fusion_geo_tf(corpus, lexicon, mark):
valence_pred = []
valence_true = []
def VA_mean(text):
sum_valence = 1
count = 0
word_list = text.split()
for word in word_list:
for line in lexicon:
if word == line:
word_tf = tf(word, corpus[i])
count = count + word_tf
sum_valence = sum_valence * (lexicon[word] ** word_tf)
out = 5 if count == 0 else sum_valence ** (1. / count)
print(out)
return out
for i, text in enumerate(corpus):
V = VA_mean(text)
valence_pred.append(V)
valence_true.append(mark[i])
print(valence_true[:200])
print(valence_pred[:200])
return valence_pred, valence_true
def linear_fusion_geo_tfidf(corpus, lexicon, mark):
valence_pred = []
valence_true = []
def VA_mean(text):
sum_valence = 1.
count = 0
word_list = text.split()
for word in word_list:
for line in lexicon:
word_tfidf = tfidf(word, corpus[i])
if word == line:
count = count + word_tfidf
sum_valence = sum_valence * (lexicon[word] ** word_tfidf)
out = 5 if count == 0 else sum_valence ** (1. / count)
print(out)
return out
for i, text in enumerate(corpus):
V = VA_mean(text)
valence_pred.append(V)
valence_true.append(mark[i])
print(valence_true[:200])
print(valence_pred[:200])
return valence_pred, valence_true
from preprocess_imdb import clean_str
def process(corpus):
return [clean_str(sent) for sent in corpus]
from regression import linear_regression, linear_regression_multivariant
from sklearn import cross_validation
def cv(data, target, multivariant=False):
X_train, X_test, Y_train, Y_test = cross_validation.train_test_split(data, target, test_size=0.2, random_state=0)
if multivariant is False:
linear_regression(X_train, X_test, Y_train, Y_test, plot=False)
else:
linear_regression_multivariant(X_train, X_test, Y_train, Y_test, cost_fun='Ridge_Regression')
if __name__ == '__main__':
from load_data import load_vader
normalize = True
corpus, ratings = load_vader(['movie_reviews'])
corpus = process(corpus)
# lexicon = load_lexicon(get_file_path('lexicon'))
from load_data import load_anew
from file_name import get_file_path
import numpy as np
words, valences, _ = load_anew(get_file_path('anew'))
mark = np.array(ratings) + np.ones(len(ratings), dtype=float) * 5
lexicon = dict()
for i, word in enumerate(words):
lexicon[word] = valences[i]
# # the following could use to check the same words in corpus and lexicon
# from visualization import show_common_term
# show_common_term(corpus, lexicon)
# exit()
# valence_mean, valence_true = linear_fusion(corpus, lexicon, mark)
# print('start.....')
# cv(valence_mean, valence_true, multivariant=False)
# print('OK')
# valence_mean, valence_true = linear_fusion_sqr(corpus, lexicon, mark)
# print('start.....')
# cv(valence_mean, valence_true, multivariant=False)
# print('OK')
# valence_mean, valence_true = nonlinear_max_fusion(corpus, lexicon, mark)
# print('start.....')
# cv(valence_mean, valence_true, multivariant=False)
# print('OK')
# valence_mean, valence_true = linear_fusion_tf(corpus, lexicon, mark)
# print('start.....')
# cv(valence_mean, valence_true, multivariant=False)
# print('OK')
#
# valence_mean, valence_true = linear_fusion_tfidf(corpus, lexicon, mark)
# print('start.....')
# cv(valence_mean, valence_true, multivariant=False)
# print('OK')
#
# valence_mean, valence_true = linear_fusion_geo(corpus, lexicon, mark)
# print('start.....')
# cv(valence_mean, valence_true, multivariant=False)
# print('OK')
########################
# valence_mean, valence_true = linear_fusion_geo_tf(corpus, lexicon, mark)
# print('start.....')
# cv(valence_mean, valence_true, multivariant=False)
# print('OK')
valence_mean, valence_true = linear_fusion_geo_tfidf(corpus, lexicon, mark)
print('start.....')
cv(valence_mean, valence_true, multivariant=False)
print('OK')