-
Notifications
You must be signed in to change notification settings - Fork 4
/
kappa.py
85 lines (82 loc) · 1.94 KB
/
kappa.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
import os
import openpyxl
path="C://Users/Nasif/Desktop/GitHubdataProject/710results"
os.chdir(path)
#for sentiment
wb=openpyxl.load_workbook('politefinaldata.xlsx')
ws=wb['polite']
bucket={}
total=0
true_pos=0
false_pos=0
label='polite'
for i in range(2,591):
if ws['C'+str(i)].value==None:
break
a=ws['C'+str(i)].value
if a=='impolite':
a='neutral'
if a==None:
print (i)
b=ws['G'+str(i)].value
if (a,b) not in bucket.keys():
bucket[(a,b)]=1
else:
bucket[(a,b)]+=1
if a==label:
total+=1
if b==label:
true_pos+=1
else:
if b==label:
false_pos+=1
precision= (true_pos/total)*100
recall= (true_pos/(true_pos+false_pos))*100
f_measure= (2*(precision*recall))/(precision+recall)
print (precision,recall,f_measure)
print(i)
print(bucket)
#calculate weighted kohen's kappa
observation_sum=0
for k in bucket.keys():
if k[0]==k[1]:
observation_sum+=0
elif "neutral" in k:
observation_sum+=bucket[k]
else:
observation_sum=observation_sum+bucket[k]*2
#calculate kohen's kappa
agreement=0
total=0
values=[]
for k in bucket.keys():
total+=bucket[k]
if k[0] not in values:
values.append(k[0])
if k[1] not in values:
values.append(k[1])
if k[0]==k[1]:
agreement+=bucket[k]
print (agreement,total, values)
expected_freq={}
for i in bucket.keys():
expected_freq[i]=0
row_total=0
col_total=0
for j in bucket.keys():
if j[0]==i[0]:
row_total+=bucket[j]
if j[1]==i[1]:
col_total+=bucket[j]
f=(col_total*row_total)/total
expected_freq[i]=f
expectation_sum=0
for k in expected_freq.keys():
if k[0]==k[1]:
expectation_sum+=0
elif "neutral" in k:
expectation_sum+=expected_freq[k]
else:
expectation_sum=expectation_sum+expected_freq[k]*2
weighted_k=1-(observation_sum/expectation_sum)
print (weighted_k)