-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
201 lines (164 loc) · 6.93 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
import re
from ngram import *
import sys
import random
import numpy as np
def pre_process_filter(line: str) -> list[str]:
for word in re.split('[\W\s]', line):
if word != '':
yield word.lower()
def read_training_datasets() -> (UnigramModel, BigramModel, list, UnigramModel, BigramModel, list):
def create_models(dataset_file_address) -> (UnigramModel, BigramModel, list[list[str]]):
uni = UnigramModel()
bi = BigramModel(uni)
test = [] # List of test dataset
# Reading Negative Dataset
with open(dataset_file_address, 'r') as file:
for line in file:
for part in re.split('[.]', line):
use_for_test = random.random() < Consts.TEST_SET_PERCENTAGE
sentence_list = []
prev_word = ''
for word in pre_process_filter(part):
if use_for_test:
sentence_list.append(word)
else:
uni[word] += 1
bi[prev_word, word] += 1
prev_word = word
if use_for_test and len(sentence_list) > 0:
test.append(sentence_list)
return uni, bi, test
neg, neg_bi, neg_test = create_models(Consts.NEGATIVE_DATASET)
pos, pos_bi, pos_test = create_models(Consts.POSITIVE_DATASET)
# Removing very low or very high frequent words
pos.clean(Consts.LOWER_FREQUENCY_CUTOFF, Consts.UPPER_FREQUENCY_CUTOFF)
neg.clean(Consts.LOWER_FREQUENCY_CUTOFF, Consts.UPPER_FREQUENCY_CUTOFF)
pos_bi.clean(Consts.LOWER_FREQUENCY_CUTOFF, Consts.UPPER_FREQUENCY_CUTOFF)
neg_bi.clean(Consts.LOWER_FREQUENCY_CUTOFF, Consts.UPPER_FREQUENCY_CUTOFF)
return neg, neg_bi, neg_test, pos, pos_bi, pos_test
def find_class(neg, pos, sentence: list[str]):
use_logarithm = Consts.USE_LOGARITHM
negative_probability = neg.get_probability_of_sentence(sentence, use_logarithm=use_logarithm)
positive_probability = pos.get_probability_of_sentence(sentence, use_logarithm=use_logarithm)
if negative_probability > positive_probability:
return neg
return pos
def test_bigram_model(neg_bi: BigramModel, neg_test: list[list[str]], pos_bi: BigramModel):
random.shuffle(neg_test)
# Initializing parameters with 0
max_precision = 0
max_l1 = 0
max_l2 = 0
max_e = 0
step = 0.1
# Searching all possible values
for lambda1 in np.arange(0, 1, step):
for lambda2 in np.arange(0, 1, step):
for epsilon in np.arange(0.1, 1, step):
# print(lambda1, lambda2, epsilon)
# Setting parameters
Consts.LAMBDA_1 = lambda1
Consts.LAMBDA_2 = lambda2
Consts.LAMBDA_3 = 1 - (lambda1 + lambda2)
Consts.EPSILON = epsilon
# Counting number of correct and wrong classifications
correct_count = 0
wrong_count = 0
if (lambda1 + lambda2) < 1:
for sentence in neg_test:
if neg_bi == find_class(neg_bi, pos_bi, sentence):
correct_count += 1
else:
wrong_count += 1
else:
# If sum of l1 and l2 is more than 1
wrong_count += 1
# print(correct_count, wrong_count)
# Finding maximum
precision = correct_count / (correct_count + wrong_count)
if precision > max_precision:
max_precision = precision
max_l1 = lambda1
max_l2 = lambda2
max_e = epsilon
return max_l1, max_l2, max_e, max_precision
def test_unigram_model(neg: UnigramModel, neg_test: list[list[str]], pos: UnigramModel):
random.shuffle(neg_test)
# Initializing parameters with 0
max_precision = 0
max_l1 = 0
max_l2 = 0
max_e = 0
step = 0.1
# Searching all possible values
for lambda1 in np.arange(0, 1, step):
for epsilon in np.arange(0.1, 1, step):
# print(lambda1, lambda2, epsilon)
# Setting parameters
Consts.LAMBDA_1_1 = lambda1
Consts.LAMBDA_1_2 = 1 - lambda1
Consts.EPSILON = epsilon
# Counting number of correct and wrong classifications
correct_count = 0
wrong_count = 0
for sentence in neg_test:
if neg == find_class(neg, pos, sentence):
correct_count += 1
else:
wrong_count += 1
# Calculating precision
if (correct_count + wrong_count) > 0:
precision = correct_count / (correct_count + wrong_count)
else:
precision = 0
if precision > max_precision:
max_precision = precision
max_l1 = lambda1
max_l2 = 1 - lambda1
max_e = epsilon
return max_l1, max_l2, max_e, max_precision
def main():
try:
neg, neg_bi, neg_test, pos, pos_bi, pos_test = read_training_datasets()
# Getting from user
input_str = input('Choose one of models:\n\n1- Unigram model\n2- Bigram model\n')
is_unigram = input_str.startswith('1')
# Testing dataset to find parameters
print('Testing. Please wait ...')
if not is_unigram:
# Testing bigram dataset and set parameters
l1, l2, e, p = test_bigram_model(pos_bi, pos_test, neg_bi)
print('Found', l1, 'for LAMBDA 1 and', l2, 'for LAMBDA 2 and', e, 'for epsilon.')
Consts.LAMBDA_1 = l1
Consts.LAMBDA_2 = l2
Consts.LAMBDA_3 = 1 - l1 - l2
Consts.EPSILON = e
neg_model = neg_bi
pos_model = pos_bi
else:
# Testing unigram dataset and set parameters
l1, l2, e1, p = test_unigram_model(pos, pos_test, neg)
print('Found', l1, 'for LAMBDA 1 and', l2, 'for LAMBDA 2 and', e1, 'for epsilon.')
Consts.LAMBDA_1 = l1
Consts.LAMBDA_2 = l2
Consts.EPSILON_1 = e1
neg_model = neg
pos_model = pos
print('Precision is', p, '\n')
# Getting inputs loop
while True:
input_str = input('Enter an opinion: ')
if input_str == '!q':
break
input_list = [i for i in pre_process_filter(input_str)]
if neg_model == find_class(neg_model, pos_model, input_list):
print('\nfilter this\n')
else:
print('\nnot filter this\n')
except KeyboardInterrupt:
print('\nExiting due to a keyboard interrupt...', file=sys.stderr)
else:
print('\nExiting ...')
if __name__ == '__main__':
main()