-
Notifications
You must be signed in to change notification settings - Fork 30
/
predict.py
354 lines (304 loc) · 13.2 KB
/
predict.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
from config import Config
from dataobject import CoNLLDataset
from data_utils import get_processing_word
from model import Model
import os
import re
import csv
class EntityRecognition():
maximum = 4
unprocessed_file_folder = "startupdaily"
model = None
doc_start_name = "-DOCSTART- -X- O O"
unprocessed_words = None
#process_folder = 'articles/200_testing'
process_folder = 'articles/checking'
process_result = 'articles_result'
def __init__(self):
config = Config()
self.model = Model(config)
self.model.build()
self.model.restore_session(config.dir_model)
def process(self):
with open(self.process_result+"/"+"files.csv", 'w') as filecsv:
filewriterfiles = csv.writer(filecsv, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)
filewriterfiles.writerow(['Name of the file', 'Final founders', 'Final organisations', 'All names', 'All organisations'])
for file in os.listdir(self.process_folder):
if file not in '.DS_Store' and os.path.isdir(self.process_folder+"/"+file) != True:
print("Processing: {}".format(file))
text_file = open(self.process_folder+"/"+file, "r")
text = text_file.read()
words_raw = self.split_sentences(text)
self.unprocessed_words = words_raw
preds = self.model.predict(words_raw)
person = []
full_name = []
names = []
full_org_name = []
org_name = []
for i, word in enumerate(words_raw):
#print("Word: {} | Prediction: {}".format(word, preds[i]))
if preds[i] == "I-PER" and preds[i] not in person:
if self.refine_check(word, "person") == True:
person.append(word)
full_name.append(word)
else:
if len(full_name) > 0:
names.append(full_name)
full_name = []
elif preds[i] == "I-ORG" and preds[i] not in org_name:
if self.refine_check(word, "org") == True:
full_org_name.append(word)
else:
if len(full_org_name) > 0:
org_name.append(full_org_name)
full_org_name = []
else:
if len(full_name) > 0:
names.append(full_name)
full_name = []
if len(full_org_name) > 0:
org_name.append(full_org_name)
full_org_name = []
word = "{} -X- O {}".format(word, preds[i])
#print(person)
names = self.remove_duplicates(names, "person")
all_names, merged_pers = self.further_process(names, "person")
#Organisation
orgs = self.remove_duplicates(org_name, "org")
all_orgs, merged_orgs = self.further_process(orgs, "org")
#Important organisation
final_org = self.find_important(merged_orgs, text)
#Important names
imporatant_per = self.find_important(merged_pers, text)
#Further filtering of names
final_per = []
for each in imporatant_per:
if len(each.split(" ")) < 2:
for name in merged_pers:
if each in name and each != name and name not in final_per:
final_per.append(name)
elif each not in final_per:
final_per.append(each)
#May be we can say if there is not final_per value then look for merged_orgs and if they have multiple word in name, we can pick it
if len(final_per) < 1:
for name in merged_pers:
if len(name.split(" ")) > 1:
final_per.append(name)
#If still empty
if len(final_per) < 1 and len(merged_pers) > 0:
final_per.append(merged_pers[0])
#final_per is good as it will list only names displayed in two letters
print(final_per)
#We can even say first in the final_org will be the startup of the article if that is about startup.
print(final_org)
final_org_str = ""
if len(final_org) > 0:
final_org_str = final_org[0]
#Further purify
final_org_str = self.find_count_word_and_purify_org(merged_orgs, text, final_org_str)
self.write_the_result(file, ",".join(merged_pers), ",".join(merged_orgs), ",".join(final_per), final_org_str)
filewriterfiles.writerow([file, "|".join(final_per), final_org_str, "|".join(merged_pers), "|".join(merged_orgs)])
'''
//problem is most of the time highest weighted name may not be a startup and this cannot be applied with level of accuracy we have with model
def find_highest_weighted_org(self, items, text):
relavant = {}
final_org = ""
highest = 0
for item in items:
mo = re.findall(re.escape(item), text)
relavant[item] = len(mo)
for key, value in relavant.items():
if highest == 0:
highest = value
final_org = key
else:
if value >= highest:
highest = value
final_org = key
return final_org
'''
def find_count_word_and_purify_org(self, items, text, org):
relavant = {}
final_org = ""
for item in items:
mo = re.findall(re.escape(item), text)
relavant[item] = len(mo)
for key, value in relavant.items():
if value > 3 and "|" not in key and org != key and org in key:
final_org = key
if final_org == "":
final_org = org
return final_org
def find_important(self, items, text):
relavant = {}
most_important = []
advanced = []
for item in items:
mo = re.findall(re.escape(item), text)
relavant[item] = len(mo)
for key, value in relavant.items():
if value > 1 and "|" not in key:
most_important.append(key)
return most_important
def write_the_result(self, file, names, orgs, imp_names, imp_orgs):
print("Writing {} result to {}_result file in articles_result folder".format(file,file))
with open(self.process_result+"/"+file+"_result", "w") as f:
f.write("All names:")
f.write("\n")
f.write(names)
f.write("\n")
f.write("\n")
f.write("All organisations:")
f.write("\n")
f.write(orgs)
f.write("\n")
f.write("\n")
f.write("Important names:")
f.write("\n")
f.write(imp_names)
f.write("\n")
f.write("\n")
f.write("Important Organisations:")
f.write("\n")
f.write(imp_orgs)
def refine_check(self, item, _type):
check = ["–", ".", "“", "’","\xa0"]
check_status = True
if _type == "person" or _type == "org" :
for each_check in check:
if each_check in item:
check_status = False
return check_status
def remove_duplicates(self, items, _type):
processed = []
if _type == "person":
for item in items:
if item not in processed:
processed.append(item)
elif _type == "org":
for item in items:
if item not in processed:
processed.append(item)
return processed
def further_process(self, items, _type):
total = []
merged = []
if _type == "person":
for item in items:
if len(item) >= 2:
divide = []
for each in item:
if "," in each:
divide.append(each)
total.append(divide)
divide = []
else:
divide.append(each)
if len(divide) > 0:
total.append(divide)
merged.append(" ".join(divide))
else:
total.append(item)
merged.append("".join(item))
else:
total.append(item)
merged.append("".join(item))
elif _type == "org":
for item in items:
if len(item) >= 2:
divide = []
for each in item:
if "Image:" in each:
continue
elif "," in each:
divide.append(each)
total.append(divide)
divide = []
else:
divide.append(each)
if len(divide) > 0:
total.append(divide)
merged.append(" ".join(divide))
else:
total.append(item)
merged.append("".join(item))
else:
total.append(item)
merged.append("".join(item))
return total, merged
def split_sentences(self, text):
word_raw = text.strip().split(" ")
words = []
for word in word_raw:
word = re.sub(u'\u201c','',word)
word = re.sub(u'\u201d','',word)
word = re.sub(u'\u201e','',word)
word = re.sub(u'\u201f','',word)
word = word.replace('[','')
word = word.replace(']','')
if "." in word:
list_words = re.split("(\.)", word)
for each in list_words:
if each != "":
words.append(each)
elif "‘" in word:
list_words = re.split("(\‘)", word)
for each in list_words:
if each != "":
words.append(each)
elif "," in word:
list_words = re.split("(\,)", word)
for each in list_words:
if each != "":
words.append(each)
elif '”' in word:
list_words = re.split('(\”)', word)
for each in list_words:
if each != "":
words.append(each)
elif '\n' in word:
list_words = re.split('(\n)', word)
for each in list_words:
if each != "":
words.append(each)
elif ':' in word:
list_words = re.split('(\:)', word)
for each in list_words:
if each != "":
words.append(each)
elif ';' in word:
list_words = re.split('(\;)', word)
for each in list_words:
if each != "":
words.append(each)
elif '?' in word:
list_words = re.split('(\?)', word)
for each in list_words:
if each != "":
words.append(each)
elif '’' in word:
list_words = re.split('(\’)', word)
for each in list_words:
if each != "":
words.append(each)
elif '/' in word:
list_words = re.split('(\/)', word)
for each in list_words:
if each != "":
words.append(each)
elif '"' in word:
list_words = re.split('(\")', word)
for each in list_words:
if each != "":
words.append(each)
elif '!' in word:
list_words = re.split('(\!)', word)
for each in list_words:
if each != "":
words.append(each)
else:
words.append(word)
return words
er = EntityRecognition()
er.process()