-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathextract_display_quotes.py
1025 lines (827 loc) · 39.6 KB
/
extract_display_quotes.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
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
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
'''
Created on Mon Jul 4 14:55:46 2022
@author: sjufri
This code has been adapted (with permission) from the GenderGapTracker GitHub page
(https://github.com/sfu-discourse-lab/GenderGapTracker/tree/master/NLP/main)
and modified to run on a Jupyter Notebook.
The quotation tool’s accuracy rate is evaluated in the below article:
The Gender Gap Tracker: Using Natural Language Processing to measure gender bias in media
(https://doi.org/10.1371/journal.pone.0245533)
'''
# import required packages
import os
import io
import sys
import codecs
import logging
import traceback
import warnings
from collections import Counter
import hashlib
from tqdm import tqdm
from zipfile import ZipFile
from pathlib import Path
# matplotlib: visualization tool
from matplotlib import pyplot as plt
# pandas: tools for data processing
import pandas as pd
# spaCy and NLTK: natural language processing tools for working with language/text data
import spacy
from spacy import displacy
from spacy.tokens import Span
import nltk
nltk.download('punkt')
from nltk.tokenize import sent_tokenize
# ipywidgets: tools for interactive browser controls in Jupyter notebooks
import ipywidgets as widgets
from ipywidgets import Layout
from IPython.display import display, clear_output, FileLink
# clone the GenderGapTracker GitHub page
path = './'
clone = 'git clone https://github.com/sfu-discourse-lab/GenderGapTracker'
os.chdir(path)
#os.system(clone)
# import the quote extractor tool
from config import config
sys.path.insert(0,'./GenderGapTracker/nlp/english')
from quote_extractor import QuoteExtractor
import utils
warnings.filterwarnings("ignore")
class DownloadFileLink(FileLink):
'''
Create link to download files in Jupyter Notebook
'''
html_link_str = "<a href='{link}' download={file_name}>{link_text}</a>"
def __init__(self, path, file_name=None, link_text=None, *args, **kwargs):
super(DownloadFileLink, self).__init__(path, *args, **kwargs)
self.file_name = file_name or os.path.split(path)[1]
self.link_text = link_text or self.file_name
def _format_path(self):
from html import escape
fp = "".join([self.url_prefix, escape(self.path)])
return "".join(
[
self.result_html_prefix,
self.html_link_str.format(
link=fp, file_name=self.file_name, link_text=self.link_text
),
self.result_html_suffix,
]
)
class QuotationTool():
'''
Interactive tool for extracting and displaying quotes in a text
'''
def __init__(self):
'''
Initiate the QuotationTool
'''
# initiate the QuoteExtractor
self.qt = QuoteExtractor(config)
# initiate the app_logger
self.app_logger = utils.create_logger('quote_extractor', log_dir='logs',
logger_level=logging.INFO,
file_log_level=logging.INFO)
# download spaCy's en_core_web_lg, the pre-trained English language tool from spaCy
print('Loading spaCy language model...')
print('This may take a while...')
self.nlp = spacy.load('en_core_web_lg')
print('Finished loading.')
# initiate variables to hold texts and quotes in pandas dataframes
self.text_df = None
self.quotes_df = None
self.large_texts = []
self.large_file_size = 1000000
# initiate the variables for file uploading
self.file_uploader = widgets.FileUpload(
description='Upload your files (txt, csv, xlsx or zip)',
accept='.txt, .xlsx, .csv, .zip', # accepted file extension
multiple=True, # True to accept multiple files
error='File upload unsuccessful. Please try again!',
layout = widgets.Layout(width='320px')
)
self.upload_out = widgets.Output()
# give notification when file is uploaded
def _cb(change):
with self.upload_out:
if self.file_uploader.value!=():
# clear output and give notification that file is being uploaded
clear_output()
# check file size
self.check_file_size(self.file_uploader)
# reading uploaded files
self.process_upload()
# give notification when uploading is finished
print('Finished uploading files.')
print('{} text documents are loaded for tagging.'.format(self.text_df.shape[0]))
# clear saved value in cache and reset counter
self.file_uploader.value = ()
# observe when file is uploaded and display output
self.file_uploader.observe(_cb, names='value')
self.upload_box = widgets.VBox([self.file_uploader, self.upload_out])
# initiate other required variables
self.html = None
self.figs = None
self.current_text = None
# create an output folder if not already exist
os.makedirs('output', exist_ok=True)
def check_file_size(self, uploaded_file):
'''
Function to check the uploaded file size
Args:
uploaded_file: the uploaded file containing the text data
'''
# check total uploaded file size
total_file_size = sum([file['size'] for file in uploaded_file.value])
print('The total size of the upload is {:.2f} MB.'.format(total_file_size/1000000))
# display warning for individual large files (>1MB)
large_text = [file['name'] for file in uploaded_file.value \
if file['size']>self.large_file_size and \
file['name'].endswith('.txt')]
if len(large_text)>0:
print('The following file(s) are larger than 1MB:', large_text)
def load_txt(self, file, n) -> list:
'''
Load individual txt file content and return a dictionary object,
wrapped in a list so it can be merged with list of pervious file contents.
Args:
file: the file containing the text data
n: index of the uploaded file (value='unzip' if the file is extracted form a zip file
'''
# read the unzip text file
if n=='unzip':
# read the unzip text file
with open(file) as f:
temp = {'text_name': file.name[:-4],
'text': f.read()
}
os.remove(file)
else:
file = self.file_uploader.value[n]
# read and decode uploaded text
temp = {'text_name': file['name'][:-4],
'text': codecs.decode(file['content'], encoding='utf-8', errors='replace')
}
# check for unknown characters and display warning if any
unknown_count = temp['text'].count('�')
if unknown_count>0:
print('We identified {} unknown character(s) in the following text: {}'.format(unknown_count, file['name'][:-4]))
return [temp]
def load_table(self, file, n) -> list:
'''
Load csv or xlsx file
Args:
file: the file containing the excel or csv data
n: index of the uploaded file (value='unzip' if the file is extracted form a zip file
'''
if n!='unzip':
file = io.BytesIO(self.file_uploader.value[n]['content'])
# read the file based on the file format
try:
temp_df = pd.read_csv(file)
except:
temp_df = pd.read_excel(file)
# check if the column text and text_name present in the table, if not, skip the current spreadsheet
if ('text' not in temp_df.columns) or ('text_name' not in temp_df.columns):
print('File {} does not contain the required header "text" and "text_name"'.format(self.file_uploader.value[n]['name']))
return []
# return a list of dict objects
temp = temp_df[['text_name', 'text']].to_dict(orient='index').values()
return temp
def extract_zip(self, zip_file):
'''
Load zip file
Args:
zip_file: the file containing the zipped data
'''
# create an input folder if not already exist
os.makedirs('input', exist_ok=True)
# read and decode the zip file
temp = io.BytesIO(zip_file['content'])
# open and extract the zip file
with ZipFile(temp, 'r') as zip:
# extract files
print('Extracting {}...'.format(zip_file['name']))
zip.extractall('./input/')
# clear up temp
temp = None
def hash_gen(self, temp_df: pd.DataFrame) -> pd.DataFrame:
'''
Create column text_id by md5 hash of the text in text_df
Args:
temp_df: the temporary pandas dataframe containing the text data
'''
temp_df['text_id'] = temp_df['text'].apply(lambda t: hashlib.md5(t.encode('utf-8')).hexdigest())
return temp_df
def nlp_preprocess(self, text):
'''
Pre-process text and fit it with Spacy language model into the column "spacy_text"
Args:
temp_df: the temporary pandas dataframe containing the text data
'''
text = sent_tokenize(text)
text = ' '.join(text)
text = utils.preprocess_text(text)
text = self.nlp(text)
return text
def process_upload(self, deduplication: bool = True):
'''
Pre-process uploaded .txt files into pandas dataframe
Args:
deduplication: option to deduplicate text_df by text_id
'''
# create placeholders to store all texts and zipped file names
all_data = []; files = []
# read and store the uploaded files
uploaded_files = self.file_uploader.value
# extract zip files (if any)
for n, file in enumerate(uploaded_files):
files.append([file.name, n])
if file.name.lower().endswith('zip'):
self.extract_zip(self.file_uploader.value[n])
files.pop()
# add extracted files to files
for file_type in ['*.txt', '*.xlsx', '*.csv']:
files += [[file, 'unzip'] for file in Path('./input').rglob(file_type) if 'MACOSX' not in str(file)]
print('Reading uploaded files...')
print('This may take a while...')
# process and upload files
for file, n in tqdm(files):
# process text files
if str(file).lower().endswith('txt'):
text_dic = self.load_txt(file, n)
# process xlsx or csv files
else:
text_dic = self.load_table(file, n)
all_data.extend(text_dic)
# remove files and directory once finished
os.system('rm -r ./input')
# convert them into a pandas dataframe format and add unique id
self.text_df = pd.DataFrame.from_dict(all_data)
self.text_df = self.hash_gen(self.text_df)
# clear up all_data
all_data = []; files = []
# deduplicate the text_df by text_id
if deduplication:
self.text_df.drop_duplicates(subset='text_id', keep='first', inplace=True)
def extract_inc_ent(
self,
list_of_string: list,
spacy_doc: spacy.tokens.doc.Doc,
inc_ent: list
) -> list:
'''
Extract included named entities from a list of string
Args:
list_of_string: a list of string from which to extract the named entities
spacy_doc: spaCy's processed text for the above list of string
inc_ent: a list containing the named entities to be extracted from the text,
e.g., ['ORG','PERSON','GPE','NORP','FAC','LOC']
'''
return [
[(str(ent), ent.label_) for ent in spacy_doc.ents \
if (str(ent) in string) & (ent.label_ in inc_ent)]\
for string in list_of_string
]
def get_quotes(self, inc_ent: list) -> pd.DataFrame:
'''
Extract quotes and their meta-data (quote_id, quote_index, etc.) from the text
and return as a pandas dataframe
Args:
inc_ent: a list containing the named entities to be extracted from the text,
e.g., ['ORG','PERSON','GPE','NORP','FAC','LOC']
'''
print('Extracting quotes...')
print('This may take a while...')
# create an empty list to store all detected quotes
all_quotes = []
# go through all the texts and start extracting quotes
for row in tqdm(self.text_df.itertuples(), total=len(self.text_df)):
text_id = row.text_id
text_name = row.text_name
try:
# process text using spacy
doc = self.nlp_preprocess(row.text)
# extract the quotes
quotes = self.qt.extract_quotes(doc=doc)
# extract the named entities
speaks, qts = [quote['speaker'] for quote in quotes], [quote['quote'] for quote in quotes]
speak_ents = self.extract_inc_ent(speaks, doc, inc_ent)
quote_ents = self.extract_inc_ent(qts, doc, inc_ent)
# add text_id, quote_id and named entities to each quote
for n, quote in enumerate(quotes):
quote['text_id'] = text_id
quote['text_name'] = text_name
quote['quote_id'] = str(n)
quote['speaker_entities'] = list(set(speak_ents[n]))
quote['quote_entities'] = list(set(quote_ents[n]))
# store them in all_quotes
all_quotes.extend(quotes)
except:
# this will provide some information in the case of an error
print('{} is too large. Consider breaking it down into smaller texts (< 1MB each file).'.format(text_name))
self.large_texts.append(text_name)
# convert the outcome into a pandas dataframe
self.quotes_df = pd.DataFrame.from_dict(all_quotes)
# convert the string format quote spans in the index columns to a tuple of integers
for column in self.quotes_df.columns:
if column.endswith('_index'):
self.quotes_df[column].replace('','(0,0)', inplace=True)
self.quotes_df[column] = self.quotes_df[column].apply(eval)
# re-arrange the columns
new_index = ['text_id', 'text_name', 'quote_id', 'quote', 'quote_index', 'quote_entities',
'speaker', 'speaker_index', 'speaker_entities',
'verb', 'verb_index', 'quote_token_count', 'quote_type', 'is_floating_quote']
self.quotes_df = self.quotes_df.reindex(columns=new_index)
return self.quotes_df
def add_entities(
self,
spacy_doc: spacy.tokens.doc.Doc,
selTokens: list,
inc_ent: list
) -> list:
'''
Add included named entities to displaCy code
Args:
spacy_doc: spaCy's processed text for the above list of string
selTokens: options to display speakers, quotes or named entities
inc_ent: a list containing the named entities to be extracted from the text,
e.g., ['ORG','PERSON','GPE','NORP','FAC','LOC']
'''
# empty list to hold entities code
ent_code_list = []
# create span code for entities
for ent in spacy_doc.ents:
if (ent.start in selTokens) & (ent.label_ in inc_ent):
span_code = "Span(doc, {}, {}, '{}'),".format(ent.start,
ent.end,
ent.label_)
ent_code_list.append(span_code)
# combine codes for all entities
ent_code = ''.join(ent_code_list)
return ent_code
def show_quotes(
self,
text_name: str,
show_what: list,
inc_ent: list
):
'''
Display speakers, quotes and named entities inside the text using displaCy
Args:
text_name: the text_name of the text you wish to display
show_what: options to display speakers, quotes or named entities
inc_ent: a list containing the named entities to be extracted from the text,
e.g., ['ORG','PERSON','GPE','NORP','FAC','LOC']
'''
# formatting options
TPL_SPAN = '''
<span style="font-weight: bold; display: inline-block; position: relative;
line-height: 55px">
{text}
{span_slices}
{span_starts}
</span>
'''
TPL_SPAN_SLICE = '''
<span style="background: {bg}; top: {top_offset}px; height: 4px; left: -1px; width: calc(100% + 2px); position: absolute;">
</span>
'''
TPL_SPAN_START = '''
<span style="background: {bg}; top: {top_offset}px; height: 4px; border-top-left-radius: 3px; border-bottom-left-radius: 3px; left: -1px; width: calc(100% + 2px); position: absolute;">
<span style="background: {bg}; z-index: 10; color: #000; top: -0.5em; padding: 2px 3px; position: absolute; font-size: 0.6em; font-weight: bold; line-height: 1; border-radius: 3px">
{label}{kb_link}
</span>
</span>
'''
colors = {'QUOTE': '#66ccff', 'SPEAKER': '#66ff99'}
options = {'ents': ['QUOTE', 'SPEAKER'],
'colors': colors,
'top_offset': 42,
'template': {'span':TPL_SPAN,
'slice':TPL_SPAN_SLICE,
'start':TPL_SPAN_START},
'span_label_offset': 14,
'top_offset_step':14}
# get the spaCy text
current_text = self.text_df[self.text_df['text_name']==text_name]['text'].to_list()[0]
doc = self.nlp_preprocess(current_text)
# create a mapping dataframe between the character index and token index from the spacy text.
loc2tok_df = pd.DataFrame([(t.idx, t.i) for t in doc], columns = ['loc', 'token'])
# get the quotes and speakers indexes
locs = {
'QUOTE': self.quotes_df[self.quotes_df['text_name']==text_name]['quote_index'].tolist(),
'SPEAKER': set(self.quotes_df[self.quotes_df['text_name']==text_name]['speaker_index'].tolist())
}
# create the displaCy code to visualise quotes and speakers
my_code_list = ['doc.spans["sc"] = [', ']']
for key in locs.keys():
for loc in locs[key]:
if loc!=(0,0):
# Find out all token indices that falls within the given span (variable loc)
selTokens = loc2tok_df.loc[(loc[0]<=loc2tok_df['loc']) & (loc2tok_df['loc']<loc[1]), 'token'].tolist()
# option to display named entities only
if show_what==['NAMED ENTITIES']:
ent_code = self.add_entities(doc, selTokens, inc_ent)
my_code_list.insert(1,ent_code)
# option to display speaker and/or quotes and/or named entities
elif key in show_what:
if 'NAMED ENTITIES' in show_what:
ent_code = self.add_entities(doc, selTokens, inc_ent)
my_code_list.insert(1,ent_code)
start_token, end_token = selTokens[0], selTokens[-1]
span_code = "Span(doc, {}, {}, '{}'),".format(start_token, end_token+1, key)
my_code_list.insert(1,span_code)
# combine all codes
my_code = ''.join(my_code_list)
# execute the code
exec(my_code)
# display the preview in this notebook
if len(locs['QUOTE'])==0 and len(locs['SPEAKER'])==0:
print('No speakers or quotes identified in the text. Select another text.')
displacy.render(doc, style='span', options=options, jupyter=True)
self.html = displacy.render(doc, style='span', options=options, jupyter=False, page=True)
warnings.filterwarnings("default")
def analyse_quotes(self, inc_ent: list):
'''
Interactive tool to display and analyse speakers, quotes and named entities inside the text
Args:
inc_ent: a list containing the named entities to be extracted from the text,
e.g., ['ORG','PERSON','GPE','NORP','FAC','LOC']
'''
# widgets to select text_name to preview
enter_text, text = self.select_text_widget(entity=False)
# widgets to select which entities to preview, i.e., speakers and/or quotes and/or named entities
entity_options, speaker_box, quote_box, ne_box = self.select_entity_widget(entity=True)
# widgets to show the preview
preview_button, preview_out = self.click_button_widget(desc='Preview',
margin='10px 0px 0px 10px',
width='200px')
# function to define what happens when the preview button is clicked
def on_preview_button_clicked(_):
# what happens when we click the preview_button
with save_out:
clear_output()
with preview_out:
clear_output()
text_name = text.value
# add the selected entities to display
show_what = []
if speaker_box.value:
show_what.append('SPEAKER')
if quote_box.value:
show_what.append('QUOTE')
if ne_box.value:
show_what.append('NAMED ENTITIES')
# provide information in the case no entity is selected
if show_what==[]:
print('Please select the entities to display!')
else:
#self.show_quotes(text_name, show_what, inc_ent)
try:
# display the text and the selected entities
self.show_quotes(text_name, show_what, inc_ent)
except:
if text_name in self.large_texts:
print('{} is too large. Consider breaking it down to smaller texts (< 1 MB).'.format(text_name))
print('Please select another text to analyse')
else:
# provide information in the case no text is selected
print('Please select the text to preview')
# link the preview_button with the function
preview_button.on_click(on_preview_button_clicked)
# widget to save the above preview
save_button, save_out = self.click_button_widget(desc='Save Preview',
margin='10px 0px 0px 10px',
width='200px')
# function to define what happens when the save button is clicked
def on_save_button_clicked(_):
with save_out:
try:
# set the output folder for saving
out_dir='./output/'
text_name = text.value
file_name = '-'.join(text_name.split()) +'.html'
# save the preview as an html file
file = open(out_dir+str(text_name)+'.html', 'w')
file.write(self.html)
file.close()
clear_output()
print('Preview saved! Click below to download:')
display(DownloadFileLink(out_dir+str(text_name)+'.html', file_name))
except:
print('You need to generate a preview before you can save it!')
# link the save_button with the function
save_button.on_click(on_save_button_clicked)
# widgets for displaying inputs, buttons and outputs
vbox2 = widgets.VBox([enter_text, text],
layout = widgets.Layout(width='300px'))
vbox1 = widgets.VBox([entity_options, speaker_box, quote_box, ne_box],
layout = widgets.Layout(width='300px'))
hbox = widgets.HBox([vbox1, vbox2])
vbox = widgets.VBox([hbox, preview_button, save_button, save_out, preview_out])
return vbox
def analyse_entities(self, inc_ent: list):
'''
Interactive tool to display and analyse named entities inside the text
Args:
inc_ent: a list containing the named entities to be extracted from the text,
e.g., ['ORG','PERSON','GPE','NORP','FAC','LOC']
'''
# widgets for selecting text_name to analyse
enter_text, text = self.select_text_widget(entity=True)
# widgets for selecting whether to display entities in speakers and/or quotes
entity_options, speaker_box, quote_box, ne_box = self.select_entity_widget()
# widgets for selecting whether to display entity names and/or types
label_options, name_box, entity_box = self.name_or_type_widget()
# widgets for selecting the number of entities to display
enter_n, top_n_option = self.select_n_widget()
# widget to show top entities
top_button, top_out = self.click_button_widget(desc='Show Top Entities',
margin='10px 0px 0px 30px',
width='200px')
# function to define what happens when the top button is clicked
def on_top_button_clicked(_):
with save_out:
clear_output()
with top_out:
clear_output()
text_name = text.value
top_n = top_n_option.value
# process the selected quote and/or speaker entities
which_ents=[]; ent_types=[]
if quote_box.value:
which_ents.append('quote_entities')
if speaker_box.value:
which_ents.append('speaker_entities')
if which_ents==[]:
print('Please select whether to display entities in the speakers and/or quotes!')
# process the selected entity names and/or types
if name_box.value:
ent_types.append('name')
if entity_box.value:
ent_types.append('label')
if ent_types==[]:
print('Please select whether to display entity names and/or types!')
self.figs = []
# display the selections
for ent_type in ent_types:
for which_ent in which_ents:
try:
fig, bar_title = self.top_entities(text_name, which_ent, ent_type, top_n)
self.figs.append([fig, bar_title])
except:
if text_name=='':
print('Please select the text to analyse')
# link the top_button with the function
top_button.on_click(on_top_button_clicked)
# widget to save the above preview
save_button, save_out = self.click_button_widget(desc='Save Top Entities',
margin='10px 0px 0px 30px',
width='200px')
# function to define what happens when the save button is clicked
def on_save_button_clicked(_):
with save_out:
if self.figs!=[]:
# set the output folder for saving
out_dir='./output/'
print('Top entities saved! Click below to download:')
# save the top entities as jpg files
for fig, bar_title in self.figs:
file_name = '-'.join(bar_title.split()) + '.jpg'
fig.savefig(out_dir+file_name, bbox_inches='tight')
display(DownloadFileLink(out_dir+file_name, file_name))
else:
print('You need to generate the bar charts before you can save them!')
warnings.filterwarnings("default")
# link the save_button with the function
save_button.on_click(on_save_button_clicked)
# displaying inputs, buttons and their outputs
vbox1 = widgets.VBox([enter_text, text],
layout = widgets.Layout(width='290px'))
vbox2 = widgets.VBox([entity_options, speaker_box, quote_box],
layout = widgets.Layout(width='300px', height='100px'))
vbox3 = widgets.VBox([label_options, name_box, entity_box],
layout = widgets.Layout(width='350px'))
vbox4 = widgets.VBox([enter_n, top_n_option],
layout = widgets.Layout(width='250px', height='80px'))
vbox5 = widgets.VBox([top_button, save_button],
layout = widgets.Layout(width='250px', height='80px'))
hbox1 = widgets.HBox([vbox2, vbox3, vbox4])
hbox2 = widgets.HBox([vbox1, vbox5],
layout=Layout(margin='5px 0px 20px 0px'))
vbox = widgets.VBox([hbox1, hbox2, save_out, top_out])
return vbox
def top_entities(
self,
text_name: str,
which_ent: str,
ent_type: list,
top_n: int=5
):
'''
Display top n named entities identified in the speakrs and/or quotes
Args:
text_name: the text_name of the text you wish to display
which_ent: option to display named entities in speakers and/or quotes
ent_type: choose whether to display entity names or types
top_n: the number of top entities to display
'''
# specify the text to analyse ('all texts' or each text individually)
if text_name=='all texts':
most_ent = self.quotes_df[which_ent].to_list()
else:
most_ent = self.quotes_df[self.quotes_df['text_name']==text_name][which_ent].tolist()
# get the top n entities from the selected text
most_ent = list(filter(None,most_ent))
most_ent = [ent for most in most_ent for ent in most]
if ent_type=='name':
most_ent = Counter([ent_name for ent_name, ent_label in most_ent])
if ent_type=='label':
most_ent = Counter([ent_label for ent_name, ent_label in most_ent])
top_ent = dict(sorted(most_ent.items(), key=lambda x: x[1], reverse=False)[-top_n:])
# visualize them
fig, bar_title = self.visualize_entities(text_name, which_ent, ent_type, top_n, top_ent)
return fig, bar_title
def visualize_entities(
self,
text_name: str,
which_ent: str,
ent_type: str,
top_n: int,
top_ent: dict
):
'''
Create a horizontal bar plot for displaying top n named entities in the speakrs and/or quotes
Args:
text_name: the text_name of the text you wish to display
which_ent: option to display named entities in speakers and/or quotes
ent_type: choose whether to display entity names or types
top_n: the number of top entities to display
top_ent: the top entities to display
'''
if top_ent!={}:
# define color formatting and option for entity names/types
bar_colors = {'speaker_entities':'#2eb82e',
'quote_entities':'#008ae6'}
ent_types = {'name': 'entity names',
'label': 'entity types'}
# specify the width, height and tick range for the plot
display_height = top_n/2
range_tick = max(1,round(max(top_ent.values())/5))
# visualize the entities using horizontal bar plot
fig = plt.figure(figsize=(10, max(5,display_height)))
plt.barh(list(top_ent.keys()), list(top_ent.values()), color=bar_colors[which_ent])
# display the values on the bars
for i, v in enumerate(list(top_ent.values())):
plt.text(v+(len(str(v))*0.05), i, str(v), fontsize=12)
# specify xticks, yticks and title
plt.xticks(range(0, max(top_ent.values())+range_tick, range_tick), fontsize=12)
plt.yticks(fontsize=12)
bar_title = 'Top {} {} entities ({}) in {}'.format(min(top_n,len(top_ent.keys())),
which_ent[:-9],
ent_types[ent_type],
text_name)
plt.title(bar_title, fontsize=14)
plt.show()
return fig, bar_title
if text_name in self.large_texts:
print('{} is too large. Consider breaking it down to smaller texts (< 1 MB).'.format(text_name))
print('Please select another text to analyse')
else:
print('No entities identified in the {}s.'.format(which_ent[:-9]))
def select_text_widget(self, entity: bool=False):
'''
Create widgets for selecting text_name to analyse
Args:
entity: option to include 'all texts' for analysing top entities
'''
# widget to display instruction
enter_text = widgets.HTML(
value='<b>Select the text to analyse:</b>',
placeholder='',
description=''
)
# use text_name for text_options
text_options = self.text_df.text_name.to_list() # get the list of text_names
# the option to select 'all texts' for analysing top entities
if entity:
text_options.insert(0, 'all texts')
# widget to display text_options
text = widgets.Combobox(
placeholder='Choose text to analyse...',
options=text_options,
description='',
ensure_option=True,
disabled=False,
layout = widgets.Layout(width='195px')
)
return enter_text, text
def select_entity_widget(self, entity: bool=False):
'''
Create widgets for selecting which entities to preview,
i.e., speakers and/or quotes and/or named entities
Args:
entity: option to include a check box for displaying named entities
'''
ne_box = None
# widget to display instruction
entity_options = widgets.HTML(
value="<b>Select which entity to show:</b>",
placeholder='',
description='',
)
# widget to display speaker check box
speaker_box = widgets.Checkbox(
value=False,
description='Speaker',
disabled=False,
indent=False,
layout=Layout(margin='0px 0px 0px 0px')
)
# widget to display speaker quote box
quote_box = widgets.Checkbox(
value=False,
description='Quote',
disabled=False,
indent=False,
layout=Layout(margin='0px 0px 0px 0px')
)
# widget to display named entity check box
if entity:
ne_box = widgets.Checkbox(
value=False,
description='Named Entities',
disabled=False,
indent=False,
layout=Layout(margin='0px 0px 0px 0px')
)
return entity_options, speaker_box, quote_box, ne_box
def click_button_widget(
self,
desc: str,
margin: str='10px 0px 0px 10px',
width='320px'
):
'''
Create a widget to show the button to click
Args:
desc: description to display on the button widget
margin: top, right, bottom and left margins for the button widget
'''
# widget to show the button to click
button = widgets.Button(description=desc,
layout=Layout(margin=margin, width=width),
style=dict(font_weight='bold'))
# the output after clicking the button
out = widgets.Output()
return button, out
def name_or_type_widget(self):
'''
Create widgets for selecting whether to display entity names and/or types
'''
# widget to display instruction
label_options = widgets.HTML(
value="<b>Display entity names and/or types:</b>",
placeholder='',
description='',
)
# widget to display entity names box
name_box = widgets.Checkbox(
value=False,
description='Entity names (e.g., John Doe, Sydney, etc.)',
disabled=False,
indent=False,
layout=Layout(margin='0px 0px 0px 0px')
)
# widget to display entity types box
entity_box = widgets.Checkbox(
value=False,
description='Entity types (e.g., PERSON, LOC, etc.)',
disabled=False,
indent=False,
layout=Layout(margin='0px 0px 0px 0px')
)