forked from TeamLIR/SigmaLaw-PBSA-Final
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ensembleOne.py
313 lines (271 loc) · 10.5 KB
/
ensembleOne.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
# stacked generalization with linear meta model on blobs dataset
import csv
from numpy import dstack
import process_input
from ensembleonepredict import get_predictlist
# create stacked model input dataset as outputs from the ensemble
def stacked_dataset(yhat):
stackX = None
for i in yhat:
ypred=i
# stack predictions into [rows, members, probabilities]
if stackX is None:
stackX = i
else:
stackX = dstack((stackX, i))
# flatten predictions to [rows, members x probabilities]
stackX = stackX.reshape((stackX.shape[0], stackX.shape[1] * stackX.shape[2]))
return stackX
# fit a model based on the outputs from the ensemble members
def fit_stacked_model(members):
# create dataset using ensemble
stackedX = stacked_dataset(members)
# fit standalone model
return stackedX
# make a prediction with the stacked model
def stacked_prediction(yhat, model):
# create dataset using ensemble
stackedX = stacked_dataset(yhat)
# make a prediction
yhat = model.predict(stackedX)
prob = model.predict_proba(stackedX)
return yhat,prob
def get_input(text,petitioner,defendant):
isEmptyPet = False
# text = input("Enter the sentence: ")
# petitioner = str(input("Enter Petitioner Party Member/s: "))
# defendant = str(input("Enter Defendant Party Member/s: "))
text=text
petitioner=petitioner
defendant=defendant
# print(text)
# print(petitioner)
# print(defendant)
if (petitioner == ''):
isEmptyPet = True
petitioner_list = petitioner.split(",")
defendant_list = defendant.split(",")
pet_count = len(petitioner_list)
# print(f"pet count {pet_count}")
party = f"[{petitioner_list},{defendant_list}]"
csv_file = './user_input/raw_input.csv'
with open(csv_file, 'w', newline='') as input_file:
writer = csv.writer(input_file)
writer.writerow(['Sentence', 'party', 'Sentiment'])
writer.writerow([text, party, 0])
process_input.process_input(csv_file)
return pet_count,isEmptyPet
def getoverall_sentiment(positive,negative,neutral):
if (len(positive)!=0 or len(negative)!=0):
if (len(positive)>len(negative)):
result= 2
elif(len(positive)<len(negative)):
result=0
else:
count=len(positive)
if(count==1):
if(positive[0]>negative[0]):
result=2
else:
result=0
else:
pos_avg= sum(positive)/count
neg_avg=sum(negative)/count
if (pos_avg> neg_avg):
result = 2
else:
result = 0
else:
result=1
return result
def pred(text,petitioner,defendant,models_list, opt_list, tokenizer,loaded_model):
isEmptyPet = False
# text = input("Enter the sentence: ")
# petitioner = str(input("Enter Petitioner Party member/s: "))
# defendant = str(input("Enter Defendant Party Member/s: "))
if (petitioner == ''):
isEmptyPet = True
petitioner_list = petitioner.split(",")
defendant_list = defendant.split(",")
aspects = []
if (len(petitioner_list) > 0 and petitioner_list[0] != ''):
for i in petitioner_list:
aspects.append(i)
if (len(defendant_list) > 0 and defendant_list[0] != ''):
for j in defendant_list:
aspects.append(j)
pet_count = len(petitioner_list)
party = f"[{petitioner_list},{defendant_list}]"
words = text.split(" ")
check = all(item in words for item in aspects)
if(check):
print("aaaaaaaa")
else:
print("eeeee")
csv_file = '/raw_input.csv'
with open(csv_file, 'w', newline='') as input_file:
writer = csv.writer(input_file)
writer.writerow(['Sentence', 'party', 'Sentiment'])
writer.writerow([text, party, 0])
process_input.process_input(csv_file)
pred_list = get_predictlist(models_list, opt_list, tokenizer)
# evaluate model on test set
yhat, prob = stacked_prediction(pred_list, loaded_model)
neg_words = ["no", "never"]
# print ("prediction.......",yhat)
# acc = accuracy_score(testy, yhat)
# print('Stacked Test Accuracy: %.3f' % acc)
# f1 = f1_score(testy, yhat, average='macro')
# print('Stacked f1 score: %.3f' % f1)
class_names = ['Negative', 'Neutral', 'Positive']
print("-------------------------Results----------------------------------------------------")
print(("Sentence : {}".format(text)))
pet_dict = {}
def_dict = {}
pet_positive = []
pet_negative = []
pet_neutral = []
def_positive = []
def_negative = []
def_neutral = []
#---------------------------------------------------
negation = False
neg_count = 0
for i in neg_words:
if (i in words):
negation = True
neg_count += 1
if (petitioner_list[0] != "" and defendant_list[0] != ""):
if (negation and neg_count % 2 != 0):
for i in range(len(yhat)):
if (yhat[i] == 0):
yhat[i] = 2
elif (yhat[i] == 2):
yhat[i] = 0
if (len(petitioner_list) == 1 and petitioner_list[0] != "" and len(defendant_list) == 1 and defendant_list[
0] != ""):
if (yhat[0] == yhat[1] and yhat[0] != 1):
if (max(prob[0]) > max(prob[1])):
if (yhat[1] == 0):
yhat[1] = 2
elif (yhat[1] == 2):
yhat[1] = 0
elif (max(prob[1]) > max(prob[0])):
if (yhat[0] == 0):
yhat[0] = 2
elif (yhat[0] == 2):
yhat[0] = 0
# ---------
pet_flag = 0
def_flag = 0
for i in range(len(yhat)):
if (not isEmptyPet):
if (pet_flag == 0):
print("Sentiments for Petitioner--->")
pet_flag = 1
if (i < pet_count):
print((" {} - {}".format(aspects[i], class_names[yhat[i]]))),
pet_dict[aspects[i]] = class_names[yhat[i]]
if (yhat[i] == 0):
pet_negative.append(prob[i][0])
elif (yhat[i] == 1):
pet_neutral.append(prob[i][0])
else:
pet_positive.append(prob[i][2])
else:
if (def_flag == 0):
print("Sentiments for Defendant--->")
def_flag = 1
print((" {} - {}".format(aspects[i], class_names[yhat[i]]))),
def_dict[aspects[i]] = class_names[yhat[i]]
if (yhat[i] == 0):
def_negative.append(prob[i][0])
elif (yhat[i] == 1):
def_neutral.append(prob[i][0])
else:
def_positive.append(prob[i][2])
else:
if (def_flag == 0):
print("Sentiments for Defendant--->")
def_flag = 1
print((" {} - {}".format(aspects[i], class_names[yhat[i]]))),
def_dict[aspects[i]] = class_names[yhat[i]]
pet_overall = getoverall_sentiment(pet_positive, pet_negative, pet_neutral)
def_overall = getoverall_sentiment(def_positive, def_negative, def_neutral)
print("Overall Sentiments for Petitioner--->")
print((" {} - {}".format("Petitioner", class_names[pet_overall]))),
print("Overall Sentiments for Defendant--->")
print((" {} - {}".format("Defendant", class_names[def_overall]))),
return text, pet_dict, def_dict, class_names[pet_overall], class_names[def_overall]
if __name__ == '__main__':
models = ["bert_spc", 'bert_atae_lstm', "gcn_bert", "ram_bert", "lcf_bert"]
# loaded_model = pickle.load(open('finalized_model.sav', 'rb'))
# models_list,opt_list,tokenizer= get_model(models)
# while (True):
# print('\n')
# print("-----------------------------------------------------------")
# print('\n')
# isEmptyPet = False
# text = input("Enter the sentence: ")
# petitioner = str(input("Enter Petitioner Party member/s: "))
# defendant = str(input("Enter Defendant Party Member/s: "))
#
#
# if (petitioner == ''):
# isEmptyPet = True
#
# petitioner_list = petitioner.split(",")
# defendant_list = defendant.split(",")
#
# aspects=[]
# if (len(petitioner_list)>0):
# for i in petitioner_list:
# aspects.append(i)
# if (len(defendant_list)>0):
# for j in defendant_list:
# aspects.append(j)
# pet_count = len(petitioner_list)
#
# party = f"[{petitioner_list},{defendant_list}]"
#
# csv_file = '/raw_input.csv'
#
# with open(csv_file, 'w', newline='') as input_file:
# writer = csv.writer(input_file)
# writer.writerow(['Sentence', 'party', 'Sentiment'])
# writer.writerow([text, party, 0])
#
# process_input.process_input(csv_file)
#
# pred_list= get_predictlist(models_list,opt_list,tokenizer)
#
# # evaluate model on test set
# yhat = stacked_prediction(pred_list, loaded_model)
# #print ("prediction.......",yhat)
# #acc = accuracy_score(testy, yhat)
# # print('Stacked Test Accuracy: %.3f' % acc)
# # f1 = f1_score(testy, yhat, average='macro')
# # print('Stacked f1 score: %.3f' % f1)
# class_names = ['Negative', 'Neutral', 'Positive']
# print("-------------------------Results----------------------------------------------------")
# print(("Sentence : {}".format(text)))
#
# pet_flag = 0
# def_flag = 0
# for i in range(len(yhat)):
# if (not isEmptyPet):
# if (pet_flag == 0):
# print("Sentiments for Petitioner--->")
# pet_flag = 1
# if (i < pet_count):
# print((" {} - {}".format(aspects[i], class_names[yhat[i]]))),
# else:
# if (def_flag == 0):
# print("Sentiments for Defendant--->")
# def_flag = 1
# print((" {} - {}".format(aspects[i], class_names[yhat[i]]))),
# else:
# if (def_flag == 0):
# print("Sentiments for Defendant--->")
# def_flag = 1
# print((" {} - {}".format(aspects[i], class_names[yhat[i]]))),