-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
339 lines (287 loc) · 11.2 KB
/
main.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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
positive_lines = []
negative_lines = []
positive_hashmap = {}
negative_hashmap = {}
deleted_words_positive = {}
deleted_words_negative = {}
p_wi_and_next_word_in_positive = {}
p_wi_and_next_word_in_negative = {}
lambda1 = 0.1
lambda2 = 0.6
lambda3 = 0.3
e = 0.001
with open("rt-polarity.pos", "r") as file_positive:
all_text_positive_file = file_positive.readlines()[0:5000]
i = 0
while i < 5000:
positive_lines.append(all_text_positive_file[i].split())
positive_lines[i].insert(0, "<$>")
positive_lines[i].insert(len(positive_lines[i]), "</$>")
i += 1
with open("rt-polarity.neg", "r") as file_negative:
all_text_negative_file = file_negative.readlines()[0:5000]
i = 0
while i < 5000:
negative_lines.append(all_text_negative_file[i].split())
negative_lines[i].insert(0, "<$>")
negative_lines[i].insert(len(negative_lines[i]), "</$>")
i += 1
def get_key(val, dictionary: dict):
for my_key, my_value in dictionary.items():
if val == my_value:
return my_key
def clean_lines():
global positive_lines, negative_lines
tmp_list = []
for i in positive_lines:
for j in i:
if j == '*' or j == '.' or j == ',' or j == '?' or j == '!' or j == ':' or j == '"' or j == '[' or j == ']':
pass
else:
tmp_list.append(j)
positive_lines = tmp_list.copy()
tmp_list.clear()
for i in negative_lines:
for j in i:
if j == '*' or j == '.' or j == ',' or j == '?' or j == '!' or j == ':' or j == '"' or j == '[' or j == ']':
pass
else:
tmp_list.append(j)
negative_lines = tmp_list.copy()
def create_both_hashmap():
for word in positive_lines:
if word not in positive_hashmap:
positive_hashmap[word] = 1
else:
positive_hashmap[word] += 1
for word in negative_lines:
if word not in negative_hashmap:
negative_hashmap[word] = 1
else:
negative_hashmap[word] += 1
def clean_both_hashmap():
"""""
This method clean both hashmap, first delete 10(number_of_maximum_values) words with highest value(counting) then
delete the words that repeat lower than 2(number_of_maximum_values) times
"""""
number_of_maximum_values = 10
number_of_minimum_values = 2
for iterator in range(number_of_maximum_values):
# [-3] is for <$> and </$>
tmp = positive_hashmap.copy()
value = sorted(tmp.values())[-3]
key = get_key(value, tmp)
deleted_words_positive[key] = positive_hashmap.pop(key)
tmp = negative_hashmap.copy()
value = sorted(tmp.values())[-3]
key = get_key(value, tmp)
deleted_words_negative[key] = negative_hashmap.pop(key)
for key in positive_hashmap:
if positive_hashmap[key] < number_of_minimum_values:
deleted_words_positive[key] = positive_hashmap[key]
for key in deleted_words_positive:
if key in positive_hashmap:
positive_hashmap.pop(key)
for key in negative_hashmap:
if negative_hashmap[key] < number_of_minimum_values:
deleted_words_negative[key] = negative_hashmap[key]
for key in deleted_words_negative:
if key in negative_hashmap:
negative_hashmap.pop(key)
"""""
print("length of positive_hashmap after cleaning:", len(positive_hashmap))
print("length of negative_hashmap after cleaning:", len(negative_hashmap))
print(deleted_words_positive)
print(deleted_words_negative)
"""""
def clean_lines_after_hashmap():
global positive_lines, negative_lines
tmp = []
for word in positive_lines:
if word in positive_hashmap:
tmp.append(word)
positive_lines = tmp.copy()
tmp.clear()
for word in negative_lines:
if word in negative_hashmap:
tmp.append(word)
negative_lines = tmp.copy()
def calculate_probabilities_with_lambda():
"""""
lambda1 + lambda2 + lambda3 = 1
0 < e < 1
e: shows accuracy if a word not in trained dictionary.
"""""
for key in bigram_matrix_positive:
if (key.split()[0] in positive_hashmap) and (key.split()[0] not in deleted_words_positive) and \
(key.split()[1] not in deleted_words_positive):
p_wi_and_next_word_in_positive[key] = (lambda3 * p_wi_and_next_word_in_positive[key]) + \
(lambda2 * p_wi_in_positive[key.split()[0]]) + (lambda1 * e)
for key in bigram_matrix_negative:
if (key.split()[0] in negative_hashmap) and (key.split()[0] not in deleted_words_negative) and \
(key.split()[1] not in deleted_words_negative):
p_wi_and_next_word_in_negative[key] = (lambda3 * p_wi_and_next_word_in_negative[key]) + \
(lambda2 * p_wi_in_negative[key.split()[0]]) + (lambda1 * e)
clean_lines()
create_both_hashmap()
clean_both_hashmap()
# clean the lines that have stop words or repeat lower than 2
clean_lines_after_hashmap()
# probability of each word in language
p_wi_in_positive = {}
for key in positive_hashmap:
p_wi_in_positive[key] = positive_hashmap[key] / len(positive_lines)
p_wi_in_negative = {}
for key in negative_hashmap:
p_wi_in_negative[key] = negative_hashmap[key] / len(negative_lines)
# build bigram matrices and save bigram count of each word
bigram_matrix_positive = {}
bigram_matrix_negative = {}
i = 0
while i < len(positive_lines) - 1:
key = str(positive_lines[i] + " " + positive_lines[i + 1])
if key not in bigram_matrix_positive:
bigram_matrix_positive[key] = 1
else:
bigram_matrix_positive[key] += 1
i += 1
i = 0
while i < len(negative_lines) - 1:
key = str(negative_lines[i] + " " + negative_lines[i + 1])
if key not in bigram_matrix_negative:
bigram_matrix_negative[key] = 1
else:
bigram_matrix_negative[key] += 1
i += 1
# calculate probability of p(wi|w<i-1>)
for key in bigram_matrix_positive:
if (key.split()[0] in positive_hashmap) and (key.split()[0] not in deleted_words_positive) and \
(key.split()[1] not in deleted_words_positive):
p_wi_and_next_word_in_positive[key] = bigram_matrix_positive[key] / positive_hashmap[key.split()[0]]
for key in bigram_matrix_negative:
if (key.split()[0] in negative_hashmap) and (key.split()[0] not in deleted_words_negative) and \
(key.split()[1] not in deleted_words_negative):
p_wi_and_next_word_in_negative[key] = bigram_matrix_negative[key] / negative_hashmap[key.split()[0]]
calculate_probabilities_with_lambda()
# main function
"""
inputs
"""
number_of_line_to_test = 300
with open("rt-polarity.pos", "r") as file_positive:
positive_sentence_data_set = file_positive.readlines()[5000:5000 + number_of_line_to_test].copy()
with open("rt-polarity.neg", "r") as file_negative:
negative_sentence_data_set = file_negative.readlines()[5000:5000 + number_of_line_to_test].copy()
positive_accuracy = 0
negative_accuracy = 0
iterator = 0
while iterator < len(positive_sentence_data_set):
list_input = positive_sentence_data_set[iterator].split()
list_input.insert(0, "<$>")
list_input.insert(len(list_input), "</$>")
"""
clean input
"""
deleted_from_input_positive = []
deleted_from_input_negative = []
for i in list_input:
if i not in positive_hashmap:
deleted_from_input_positive.append(i)
for i in deleted_from_input_positive:
if i in list_input:
list_input.remove(i)
for i in list_input:
if i not in negative_hashmap:
deleted_from_input_negative.append(i)
for i in deleted_from_input_negative:
if i in list_input:
list_input.remove(i)
""
""
# print(list_input)
p_multiplication_positive = p_wi_in_positive[list_input[0]]
i = 1
while i < len(list_input):
two_word = str(list_input[i - 1] + " " + list_input[i])
if two_word in p_wi_and_next_word_in_positive:
p_multiplication_positive *= p_wi_and_next_word_in_positive[two_word]
else:
p_multiplication_positive *= (lambda2 * p_wi_in_positive[list_input[i]]) + (lambda3 * e)
i += 1
i = 1
p_multiplication_negative = p_wi_in_negative[list_input[0]]
while i < len(list_input):
two_word = str(list_input[i - 1] + " " + list_input[i])
if two_word in p_wi_and_next_word_in_negative:
p_multiplication_negative *= p_wi_and_next_word_in_negative[two_word]
else:
p_multiplication_negative *= (lambda2 * p_wi_in_negative[list_input[i]]) + (lambda3 * e)
i += 1
if p_multiplication_positive > p_multiplication_negative:
# print("not filter this")
positive_accuracy += 1
# else:
# print("filter this")
# input_string = input()
iterator += 1
iterator = 0
while iterator < len(negative_sentence_data_set):
list_input = negative_sentence_data_set[iterator].split()
list_input.insert(0, "<$>")
list_input.insert(len(list_input), "</$>")
"""
clean input
"""
deleted_from_input_positive = []
deleted_from_input_negative = []
for i in list_input:
if i not in positive_hashmap:
deleted_from_input_positive.append(i)
for i in deleted_from_input_positive:
if i in list_input:
list_input.remove(i)
for i in list_input:
if i not in negative_hashmap:
deleted_from_input_negative.append(i)
for i in deleted_from_input_negative:
if i in list_input:
list_input.remove(i)
""
""
# print(list_input)
p_multiplication_positive = p_wi_in_positive[list_input[0]]
i = 1
while i < len(list_input):
two_word = str(list_input[i - 1] + " " + list_input[i])
if two_word in p_wi_and_next_word_in_positive:
p_multiplication_positive *= p_wi_and_next_word_in_positive[two_word]
else:
p_multiplication_positive *= (lambda2 * p_wi_in_positive[list_input[i]]) + (lambda3 * e)
i += 1
i = 1
p_multiplication_negative = p_wi_in_negative[list_input[0]]
while i < len(list_input):
two_word = str(list_input[i - 1] + " " + list_input[i])
if two_word in p_wi_and_next_word_in_negative:
p_multiplication_negative *= p_wi_and_next_word_in_negative[two_word]
else:
p_multiplication_negative *= (lambda2 * p_wi_in_negative[list_input[i]]) + (lambda3 * e)
i += 1
if p_multiplication_positive > p_multiplication_negative:
# print("not filter this")
pass
else:
negative_accuracy += 1
# print("filter this")
# input_string = input()
iterator += 1
positive_accuracy = "%.2f" % (positive_accuracy / number_of_line_to_test)
negative_accuracy = "%.2f" % (negative_accuracy / number_of_line_to_test)
print(positive_accuracy)
print(negative_accuracy)
output_string = "\t\t[λ1: " + str(lambda1) + " ,λ2: " + str(lambda2) + " ,λ3: " + str(lambda3) + " ,e: " + str(
e) + "] -> " + "Positive accuracy: " + str(positive_accuracy) + " ,Negative accuracy: " + str(
negative_accuracy) + "\n"
print(output_string)
with open("result.txt", "a+") as result:
result.write(output_string)