-
Notifications
You must be signed in to change notification settings - Fork 0
/
metrics.py
199 lines (171 loc) · 5.89 KB
/
metrics.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
import pandas as pd
import urllib
import ast
import ast
import numpy as np
import nltk
from gensim.parsing.preprocessing import strip_punctuation
from gensim.parsing.preprocessing import strip_multiple_whitespaces
from gensim.parsing.preprocessing import strip_short
nltk.download('averaged_perceptron_tagger')
nltk.download('punkt')
nltk.download('maxent_ne_chunker')
nltk.download('words')
nltk.download('treebank')
# -------- # -------- # -------- # -------- # -------- # -------- #
df1 = pd.read_csv('ner_fetched.csv')
# Check if any empty list in Persons
ff = df1[df1.Persons == '[]']
print('empty fethced Person list',ff.Target_Public_Status.value_counts())
df = pd.read_csv('original_data.csv')
df = df.loc[:,['Acquiror_Full_Name','Target_Name','Acq CEO', 'Target CEO','Source','Source Link','Links']]
# -------- # -------- # -------- # -------- # -------- # -------- #
''' Drop all rows where Acq CEO = nan'''
c = 0
d = 0
e = 0
for i in range(df2.shape[0]):
# list of fetched names
res = ast.literal_eval(df2.Persons[i])
# if NER person name extraction not good
# example:
j = []
for z in res:
# to remove dot after middle name if present
z = strip_punctuation(z)
# if three words in name present
if len(z.split(' ')) == 3:
# if middle name present then length also 3 but middle name is one word mostly A. or Al so length of middle name is fixed to 2
if len(z.split(' ')[1]) > 2:
j.append(z.rsplit(' ', 1)[0])
# else append the original text
else:
j.append(z)
elif len(z.split(' ')) >3:
# if name is 'Robert G Isaman Stanadyne'
j.append(z.rsplit(' ', 1)[0])
# include condition to remove first text when name is 'Robert G Isaman Stanadyne' -> if length of thrid word is of middle name length
else:
j.append(z)
res = j
# if item is string
if isinstance(df2['Acq CEO'][i],str):
'''ss is the manually labeled result'''
# strip whitespace at end or first
ss = df2['Acq CEO'][i].strip()
orig = strip_multiple_whitespaces(strip_punctuation(ss))
if orig in res:
# removing short words might remove m,iddle name that must be effective
c+=1
# remove middle name from fetched data if middle name present
# to resolve problem of index 36: Amin Khoury and Amin J Khoury
elif ss in [strip_short(i) for i in res]:
c+=1
# remove middle name from original data and then compare it with fetched data
elif strip_short(ss) in res:
c+=1
# else Person name is string but not fetched using algorithm
else:
# if only first name or last name is fetched: example fethced 'Wallace' whereas original text has 'Timothy R Wallace'
# check if splitting the name works
ss_split = ss.split(' ')
flag = 0
for x in ss_split:
if x in res:
c+=1
flag = 1
break
else:
continue
# if after running above loop there is no match
if flag == 0:
# print(df2['Acq CEO'][i],i)
e = e+1
else:
# these are the cases when instance in nan
# that is in original data there is no labeling
d+=1
print('Tcq CEO:', ' | Correctly identified: ',c,' | Not identified: ',e)
# -------- # -------- # -------- # -------- # -------- #
''' Acq CEO'''
c = 0
d = 0
e = 0
for i in range(df2.shape[0]):
# list of fetched names
res = ast.literal_eval(df2.Persons[i])
# if NER person name extraction not good
# example:
j = []
for z in res:
# to remove dot after middle name if present
z = strip_punctuation(z)
# if three words in name present
if len(z.split(' ')) == 3:
# if middle name present then length also 3 but middle name is one word mostly
if len(z.split(' ')[1]) > 2:
j.append(z.rsplit(' ', 1)[0])
# else append the original text
else:
j.append(z)
elif len(z.split(' ')) >3:
# if name is 'Robert G Isaman Stanadyne'
j.append(z.rsplit(' ', 1)[0])
# include condition to remove first text when name is 'Robert G Isaman Stanadyne' -> if length of thrid word is of middle name length
else:
j.append(z)
res = j
# if item is string
if isinstance(df2['Target CEO'][i],str):
'''ss is the manually labeled result'''
# strip whitespace at end or first
ss = df2['Target CEO'][i].strip()
orig = strip_multiple_whitespaces(strip_punctuation(ss))
if orig in res:
# removing short words might remove m,iddle name that must be effective
c+=1
# remove middle name from fetched data if middle name present
# to resolve problem of index 36: Amin Khoury and Amin J Khoury
elif ss in [strip_short(i) for i in res]:
c+=1
# remove middle name from original data and then compare it with fetched data
elif strip_short(ss) in res:
c+=1
# else Person name is string but not fetched using algorithm
else:
# if only first name or last name is fetched: example fethced 'Wallace' whereas original text has 'Timothy R Wallace'
# check if splitting the name works
ss_split = ss.split(' ')
flag = 0
for x in ss_split:
if x in res:
c+=1
flag = 1
break
else:
continue
# if after running above loop there is no match
if flag == 0:
print(df2['Target CEO'][i],i)
print(orig)
print(res)
e = e+1
else:
# these are the cases when instance in nan
# that is in original data there is no labeling
d+=1
print('Target CEO:', ' | Correctly identified: ',c,' | Not identified: ',e)
# -------- # -------- # -------- # -------- # -------- #
c = 0
for i in range(df2.shape[0]):
# list of fetched names
res = ast.literal_eval(df2.Jobs[i])
if 'CEO' in res:
c+=1
elif 'Chief Executive Officer' in res:
c+=1
elif 'Executive' in res:
c+=1
else:
print(res)
print('Correctly Identified job titles: ',c)