-
Notifications
You must be signed in to change notification settings - Fork 2
/
AgreementMetrics.py
151 lines (105 loc) · 5.33 KB
/
AgreementMetrics.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
# coding: utf-8
# In[1]:
import pandas as pd
import scipy
import numpy as np
import re # only for use of regex flags in pandas str methods
# In[2]:
df = pd.read_csv("TermsOfService_Agreement.csv")
# In[3]:
# 0-1 flags for presence of special keywords
df['Arbitration'] = (df.ParagraphText.str.contains('arbitration', flags=re.IGNORECASE)).astype('int8')
df['ThirdParty'] = (df.ParagraphText.str.contains('third[- ]party', flags=re.IGNORECASE)).astype('int8')
df['Waiver'] = (df.ParagraphText.str.contains('waiver', flags=re.IGNORECASE)).astype('int8')
# Some more paragraph stats
df['ParagraphWords'] = (df.ParagraphText.str.count(' ')) + 1 # a space was the best indicator of actual words I could find
# without inserting a great deal of complication.
df['ParagraphSentences'] = (df.ParagraphText.str.count(r'\.[ $]')) # period followed by space or end-of-string
df['Quotes'] = df.ParagraphText.str.count(r'["]')
df['Parentheses'] = df.ParagraphText.str.count(r'[)(]') - 2*df.ParagraphText.str.count(r'\([a-zA-Z0-9]\)')
df['AvgWordLength'] = (df.ParagraphLength - (df.ParagraphWords - 1) - df.ParagraphSentences - df.Quotes -
df.Parentheses)/df.ParagraphWords
# (total chars - #spaces - #periods - #quotechars - #parentheses)/#words
# not exact, but a good approximation.
# In[4]:
print(df.columns)
print(np.sort(df.Company.unique()))
# In[5]:
annotations = sorted(list(df.columns[df.columns.str.contains('Import')]))
companies = np.sort(df.Company.unique())
print(annotations)
print(companies)
# In[6]:
# Define the assignments
assignments = {annotations[0]:tuple(companies[[2,3,4,8,10,12]]),
annotations[1]:tuple(companies[[0,1,3,5,7,8]]),
annotations[2]:tuple(companies[[0,5,6,10,11,12]]),
annotations[3]:tuple(companies[[1,2,4,6,7,11]])}
print(assignments)
# In[7]:
# Inspect null values
nulls = {}
for name in list(assignments.keys()):
a = df[name][df.Company.isin(assignments[name])]
a = a[a.isnull()]
nulls[name] = a.index
for name in list(nulls.keys()):
indices = nulls[name]
print(name + ":")
print(df.loc[indices, ['Company'] + list(nulls.keys())])
# In[8]:
# Reassign nulls to values in neighboring annotation columns
for name in list(nulls.keys()):
indices = nulls[name]
df.loc[indices, name] = np.all(df.loc[indices, list(nulls.keys())].values, axis = 1).astype('int8')
#print(np.all(df.loc[indices, list(nulls.keys())].values, axis = 1).astype('int8'))
print(df.loc[indices, ['Company'] + list(nulls.keys())])
# In[9]:
# Initialize agreement metric matrices
nrows = len(assignments)*(len(assignments) - 1)/2
index = pd.MultiIndex(levels=[list(assignments.keys()), list(assignments.keys())], names = ['rater1','rater2'], labels = [[0,0,0,1,1,2],[1,2,3,2,3,3]])
metrics = pd.DataFrame({"sampleSize":np.NaN, "VI":np.NaN, "cohen":np.NaN, "companies":np.array("",dtype='S32')}, index = index)
print(metrics)
# In[10]:
# Compute the agreement metrics and put in a dataframe
max_variation = np.log(4)
for i in range(0,(len(assignments)-1)):
for j in range((i+1),len(assignments)):
name1 = list(assignments.keys())[i]
name2 = list(assignments.keys())[j]
matrix = pd.crosstab(df[name1],df[name2])
total = matrix.values.sum()
proportions = matrix.values/total
margin1 = np.add.reduce(proportions, axis=0)
margin2 = np.add.reduce(proportions, axis=1)
independent = np.zeros(shape = [len(margin1), len(margin2)], dtype = 'float')
for k in range(0, len(margin2)):
independent[k,:] = margin1*margin2[k]
print(name1 + " " + name2 + ":")
print("Actual:")
print(proportions.round(3))
print("Independent:")
print(independent.round(3))
random = independent.diagonal()
actual = proportions.diagonal()
kappa = (np.sum(actual) - np.sum(random))/(1 - np.sum(random))
entropy1 = -1*np.sum(margin1*np.log(margin1))
entropy2 = -1*np.sum(margin2*np.log(margin2))
mutualInformation = np.sum(proportions*np.log(proportions/independent))
variation = (entropy1 + entropy2 - 2*mutualInformation)/max_variation
metrics.loc[(name1,name2),['cohen','VI','sampleSize','companies']] = [kappa,variation,total,
str(set(assignments[name1]).intersection(set(assignments[name2])))]
print(metrics)
# In[11]:
# Create the final response column(s)
df['responseAND'] = np.array(np.int(), dtype = 'int8')
df['responseOR'] = np.array(np.int(), dtype = 'int8')
for i in range(0,df.shape[0]):
# The intersection of the positive labels (AND)
df.loc[i,"responseAND"] = np.min(df.ix[i][list(assignments.keys())].dropna().values)
# The union of the positive labels (OR)
df.loc[i,"responseOR"] = np.max(df.ix[i][list(assignments.keys())].dropna().values)
# In[12]:
# Write data to csv
df.to_csv("TermsOfService.csv", index=False)
metrics.reset_index().to_csv("AgreementMetrics.csv", index=False)