-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathannotation_tools.py
More file actions
554 lines (473 loc) · 15.7 KB
/
Copy pathannotation_tools.py
File metadata and controls
554 lines (473 loc) · 15.7 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
# standard library
import re
from collections import namedtuple
from datetime import datetime
# third party
import pandas as pd
from IPython.display import HTML, display, clear_output
class Annotator():
def __init__(self):
self.annotations = list()
@property
def annotated_phrases(self):
return [annotation.phrase for annotation in self.annotations]
def to_dataframe(self):
return pd.DataFrame(self.annotations)
def from_dataframe(self, df):
self.annotations = [
annotation for annotation in df.itertuples(
index=False, name='Annotation')
]
return None
class DocEvaluator(Annotator):
"""
Evaluate accuracy of the ner in a spacy Doc.
"""
Annotation = namedtuple('Annotation', ['id', 'idx', 'type', 'timestamp'])
def __init__(self):
super().__init__()
def __call__(self, doc):
lines = HTML_from_doc(doc).html_lines
out = self._interface(doc._.id, lines)
return out
def _interface(self, id, lines):
for html in lines:
display(HTML(html))
while True:
idx = input('idx and [+] false positve, [-] false negative: ')
if idx and idx != '.':
try:
annotation = idx[-1:]
int(idx[:-1])
# annotation = None
# while annotation not in ['+', '-']:
# annotation = input(
# '[+] false positve, [-] false negative: '
# )
self.annotations.append(
self.Annotation(id, idx, annotation, datetime.now())
)
except ValueError:
pass
clear_output()
display(HTML(html))
else:
break
clear_output()
if idx == '.':
return '.'
return None
class HTML_from_doc():
"""
Separate tokens, idx and ner of a spaCy `Doc` into lines and render as html.
"""
Token = namedtuple('Token', ['text', 'idx', 'ent'])
class_name = 'a'
style = (
"<style>"
"* {"
"box-sizing: border-box;"
"}"
f"table.{class_name},"
f"th.{class_name},"
f"td.{class_name} {{"
"border-right: 1px solid black;"
"}"
f"table.{class_name} {{"
"margin-bottom: 24px !important;"
"}"
"</style>"
)
def __init__(self, doc, char_limit=80):
self.doc = doc
self.char_limit = char_limit
self.lines = self.doc_to_lines(doc, char_limit)
self.max_lines = max(self.lines)
self.html_lines = self.lines_to_html_lines(self.lines)
def lines_to_html_lines(self, lines):
def cell(x):
return f"<td class='{self.class_name}'>{x}</td>"
def concat(lst):
return ''.join(lst)
html_lines = list()
for line in lines:
indeces = [cell(token.idx) for token in lines[line]]
ents = [cell(token.ent) for token in lines[line]]
texts = [
cell(token.text) if token.ent == ''
else cell('<mark>' + token.text + '</mark>')
for token in lines[line]
]
html = (
f"{self.style}"
f"<h3>{self.doc._.id}</h3>"
f"<table class='{self.class_name}'>"
"<tr>"
f"<th rowspan='3' class='{self.class_name}'>"
f"{line:02} / {self.max_lines}</th>"
f"{concat(ents)}"
"</tr>"
"<tr>"
f"{concat(texts)}"
"</tr>"
"<tr>"
f"{concat(indeces)}"
"</tr>"
"</table>"
)
html_lines.append(html)
return html_lines
@classmethod
def doc_to_lines(cls, doc, char_limit):
lines = dict()
line = list()
line_length = 0
line_idx = 0
for token in doc:
text = token.text if token.text != '\n' else '^'
line_length += len(text)
line.append(cls.Token(text, token.i, token.ent_type_))
if line_length > char_limit:
line_length = 0
lines[line_idx] = line
line_idx += 1
line = list()
return lines
class PhraseAnnotator(Annotator):
"""
Annotator
=========
Stores annotations as Annotation objects (`namedtuple`).
Annotations are stored under the 'annotations' attribute.
Attributes
==========
name: Name of the class (used for display)
data: Tuple of LexisNexis data and column name with text
info: Tuple of info data and column name with phrase key
n: Number of samples
annotations: List of stored annotations
annotated_phrases: List of annotated phrases
Methods
=======
__call__: Run annotator
to_dateframe: Return annotations as `DataFrame`
"""
name = 'Phrase Annotator'
Annotation = namedtuple(
'Annotation', ['phrase', 'id', 'annotation', 'timestamp']
)
def __init__(self, data, info=None, n=5):
"""
Initialize annotator.
Parameters
==========
:param data: `tuple` of `DataFrame`, `str`
Tuple containing:
- DataFrame with the source data
- Name of the column containing the text
The annotator will search the key-column for phrase matches.
The matched records will be displayed.
Optional key-word arguments
===========================
:param info: `tuple` of `DataFrame`, `str`
Tuple containing:
- DataFrame with the phrase info
- Name of the column containing the phrase key
The annotator will search the key-column for phrase matches.
The matched records will be displayed.
:param n: `int`, default=5
Number of samples to annotate per phrase.
If n=0 no sampling will take place.
"""
super().__init__()
self.data = data
self.info = info
self.n = n
def __call__(self, phrases):
"""
Annotate search results.
The annotator skips phrases that were annotated in a previous session.
Parameters
==========
:param phrases: iterable
Iterable of phrases to annotate.
Returns
=======
:__call__: None
"""
for phrase in phrases:
if phrase in self.annotated_phrases:
continue
user = self._interface(phrase)
if user == '.':
break
return None
def _interface(self, phrase):
search = PhraseSearch(
self.name, phrase, self.data, info=self.info, n=self.n
)
test = search()
if not test:
self.annotations.append(
self.Annotation(phrase, None, 'n/f', datetime.now())
)
clear_output()
return None
for row, html in search():
user = ''
while user not in ['+', '-', '?', '.']:
display(HTML(html))
user = input(
"Please annotate the sample above:\n"
"[+] if the sample matches.\n"
"[-] if the sample does not.\n"
"[?] when unsure.\n"
"[.] to exit (current phrase will NOT be saved).\n"
)
clear_output()
if user == '.':
return user
self.annotations.append(
self.Annotation(phrase, row.id, user, datetime.now())
)
return None
class PhraseExplorer():
"""
Annotator
=========
Stores annotations as Annotation objects (`namedtuple`).
Annotations are stored under the 'annotations' attribute.
Attributes
==========
name: Name of the class (used for display)
data: Tuple of LexisNexis data and column name with text
info: Tuple of info data and column name with phrase key
n: Number of samples
annotations: List of stored annotations
annotated_phrases: List of annotated phrases
Methods
=======
__call__: Run annotator
to_dateframe: Return annotations as `DataFrame`
"""
name = 'Phrase Explorer'
def __init__(self, data, info=None):
"""
Initialize phrase explorer.
Parameters
==========
:param data: `tuple` of `DataFrame`, `str`
Tuple containing:
- DataFrame with the source data
- Name of the column containing the text
The annotator will search the key-column for phrase matches.
The matched records will be displayed.
Optional key-word arguments
===========================
:param info: `tuple` of `DataFrame`, `str`
Tuple containing:
- DataFrame with the phrase info
- Name of the column containing the phrase key
The annotator will search the key-column for phrase matches.
The matched records will be displayed.
"""
self.data = data
self.info = info
self.annotations = list()
def __call__(self, phrase=None):
"""
Annotate search results.
The annotator skips phrases that were annotated in a previous session.
Parameters
==========
:param phrases: iterable
Iterable of phrases to annotate.
Returns
=======
:__call__: None
"""
if not phrase:
phrase = input("Input new search phrase\n")
clear_output()
while True:
user = self._interface(phrase)
if user is '.':
break
return None
def _interface(self, phrase):
while True:
search = PhraseSearch(
self.name, phrase, self.data, info=self.info, n=0
)
test = search()
if not test:
phrase = input("Input new search phrase\n")
clear_output()
continue
for _, html in search():
display(HTML(html))
user = input(
"Press <enter> to go to next record\n"
"Input new search phrase\n"
"Input '.' to exit\n"
)
clear_output()
if user == '.':
return user
if user:
phrase = user
break
return None
class PhraseSearch():
style = """
* {
box-sizing: border-box;
}
h1, h2, h3, h4, p, .container {
margin: 0 !important;
padding: 12px;
}
h1 {
background-color: black;
color: white;
text-transform: uppercase;
}
hr {
margin: 0 !important;
border: none;
border-top: 1px solid black;
}
.box {
border: 1px solid black;
}
.container {
overflow: scroll;
}
"""
def __init__(self, name, phrase, data, info=None, n=0):
self.name = name
self.phrase = phrase
self.regex = rf"\b{phrase}\b"
self.data = data[0]
self.column = data[1]
self.info = info
self.n = n
@property
def results(self):
results = self.data.loc[
self.data[self.column].str.contains(self.regex, regex=True)
].reset_index()
if results.empty:
results = self.data.loc[
self.data[self.column].str.contains(self.phrase)
].reset_index()
return results
@property
def n_results(self):
return len(self.results)
@property
def n_samples(self):
if self.n_results < self.n:
return self.n_results
return self.n
@property
def phrase_info(self):
if self.info:
qry = f"{self.info[1]} == @self.phrase"
phrase_info = self.info[0].query(qry)
if not phrase_info.empty:
return phrase_info
return None
def __call__(self):
if self.n_results:
return self._yield_results()
else:
print(f"Phrase '{self.phrase}' not found")
return None
def _yield_results(self):
if self.n:
for sample, (idx, row) in enumerate(
self.results.sample(self.n_samples).iterrows()
):
html = self.get_html(sample + 1, idx, row)
yield row, html
else:
for idx, row in self.results.iterrows():
html = self.get_html(None, idx, row)
yield row, html
def get_html(self, sample, idx, row):
info_html = ''
if self.phrase_info is not None:
info_html = self.phrase_info.to_html(
index=False,
notebook=True
)
info_html = f"<div class='container'>{info_html}</div>"
sample_html = ''
if sample:
sample_html += f"SAMPLE: {sample} of {self.n_samples} | "
content = (
f"<h1>{self.name}</h1>"
f"<h2>PHRASE: {self.phrase}</h2>"
f"{info_html}"
f"<h3>"
f"{sample_html}"
f"SOURCE: {row.source} | "
f"RESULT: {idx + 1} of {self.n_results} | "
f"{row.id}"
f"</h3><hr>"
f"<h4>{row.title}</h4><hr>"
)
for p in row.body_:
if re.search(self.regex, p):
p = f"<p>{p}</p>"
content += re.sub(
self.regex,
f"<mark><b>{self.phrase}</b></mark>",
p,
)
return f"<style>{self.style}</style><div class='box'>{content}</div>"
def section_explorer(df, phrase=None):
if not phrase:
phrase = input("Input section to search for:\n")
def explore(phrase):
df_q = df.query("section.str.contains(@phrase)", engine='python')
results = len(df_q)
for idx, row in df_q.reset_index().iterrows():
html = f"<h1>SECTION EXPLORER</h1>"
html += (
f"<h2>"
f"SECTION: {row.section}"
f"</h2>"
)
html += (
f"<h3>"
f"RESULT: {idx + 1} of {results} | "
f"SOURCE: {row.source} | "
f"ID: {row.id}"
"</h3><hr>"
)
html += f"<h4>{row.title}</h4><hr>"
for p in row.body:
html += f"<p>{p}</p>"
html = (
f"<style>{PhraseSearch.style}</style>"
f"<div class='box'>{html}</div>"
)
display(HTML(html))
user = input(
"Press <enter> to go to next record "
"(you can also input a new section or '.' to exit)"
)
clear_output()
if user == '.':
break
if len(user) > 0:
return user
if df_q.empty:
clear_output()
print(f"Section '{phrase}' not found.")
return '.'
while phrase != '.':
phrase = explore(phrase)
return None