forked from munibanust/febrl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathname.py
More file actions
1309 lines (1088 loc) · 51.1 KB
/
name.py
File metadata and controls
1309 lines (1088 loc) · 51.1 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
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
# =============================================================================
# name.py - Routines for name cleaning and standardisation.
#
# Freely extensible biomedical record linkage (Febrl) Version 0.2.2
# See http://datamining.anu.edu.au/projects/linkage.html
#
# =============================================================================
# AUSTRALIAN NATIONAL UNIVERSITY OPEN SOURCE LICENSE (ANUOS LICENSE)
# VERSION 1.1
#
# The contents of this file are subject to the ANUOS License Version 1.1 (the
# "License"); you may not use this file except in compliance with the License.
# Software distributed under the License is distributed on an "AS IS" basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# The Original Software is "name.py".
# The Initial Developers of the Original Software are Dr Peter Christen
# (Department of Computer Science, Australian National University) and Dr Tim
# Churches (Centre for Epidemiology and Research, New South Wales Department
# of Health). Copyright (C) 2002, 2003 the Australian National University and
# others. All Rights Reserved.
# Contributors:
#
# =============================================================================
"""Module name.py - Routines for name cleaning and standardisation.
PUBLIC FUNCTIONS:
tag_name_component Tag a name input component string and make a list
get_gender_guess Extract a gender guess from tags and words
get_title Extract the title component of a name
get_name_component Parse the input word list and extract names and
alternative names for either givenname or surnames
get_name_rules Parse the input using rules and extract given- and
surnames
get_name_hmm Process the input word and tag lists using a Hidden
Markov Model (HMM) to extract name output fields
See doc strings of individual functions for detailed documentation.
TODO:
"""
# =============================================================================
# Imports go here
import string
import mymath
# =============================================================================
def tag_name_component(name_str, tag_lookup_table, record_id):
"""Tag a name input component string and make a list.
USAGE:
[word_list, tag_list] = tag_name_component(name_str, tag_lookup_table,
record_id)
ARGUMENTS:
name_str A string containing the name component
tag_lookup_table A tagging look-up table as defined in 'lookup.py'
record_id A string identifying the current record
DESCRIPTION:
This routine cleans the input string and extracts words, numbers and
separators into a list. Each element of this list is assigned one or more
tags. A 'greedy tagger' is applied, which cheques sequences of list
elements in the given lookup table (longer sequences first) and replaces
them with the string and tag from the lookup-table if found.
The routine returns two lists: words and their tags
"""
# First, split input string into elements at spaces - - - - - - - - - - - - -
#
org_list = name_str.split() # The original list from the input string
tag_list = [] # The initially empty list of tags
word_list = [] # The initially empty list of words
while (org_list != []): # As long as not all elements have been processed
tmp_list = org_list[:tag_lookup_table.max_key_length]
# Extract longest sub-list
tmp_val = [] # Start with empty value
tmp_key = tuple(tmp_list)
while (tmp_key != ()): # As long as key not empty and not found in lookup
if (tag_lookup_table.has_key(tmp_key)):
tmp_val = tag_lookup_table[tmp_key]
break
tmp_key = tmp_key[:-1] # Remove last element in key
if (tmp_val != []): # A value has been found in the dictionary
tmp_len = len(tmp_key) # Length of found sequence
if (tmp_val[0] != ''): # It's not an empty value
word_list.append(tmp_val[0]) # Append corrected word (or sequence)
tag_list.append(tmp_val[1]) # Append tag or tags
else: # No value has been found in the lookup dictionary, try other tags
tmp_val = org_list[0] # Value is first element in the original list
tmp_len = 1
if (len(tmp_val) == 1) and (tmp_val.isalpha()): # A 1-letter word
word_list.append(tmp_val)
tag_list.append('II')
elif (tmp_val.isdigit()): # Element is a number
word_list.append(tmp_val)
tag_list.append('NU')
elif (not tmp_val.isalpha()) and tmp_val.isalnum(): # Alpha-numeric
word_list.append(tmp_val)
tag_list.append('AN')
elif (tmp_val == '-'): # Element is a hyphen
if (tag_list != []) and (tag_list[-1] not in ['BO','SP']):
# Don't append hyphen at beginning or after BO/SP
word_list.append(tmp_val)
tag_list.append('HY')
elif (tmp_val == ','): # Element is a comma
word_list.append(tmp_val)
tag_list.append('CO')
elif (tmp_val == '|'): # Element is a vertical bar
word_list.append(tmp_val)
tag_list.append('VB')
else: # An unknown element
word_list.append(tmp_val)
tag_list.append('UN')
# Finally remove the processed elements from the original element list
#
org_list = org_list[tmp_len:] # Remove processed elements
# A log message for high volume log output (level 3) - - - - - - - - - - - -
#
print '3:%s Name string "%s"' % (record_id, name_str)
print '3:%s Split into word list: %s' % (record_id, str(word_list))
print '3:%s and tag list: %s' % (record_id, str(tag_list))
return [word_list, tag_list]
# =============================================================================
def get_gender_guess(word_list, tag_list, male_titles, female_titles,
record_id):
"""Extract a gender guess from tags and words.
USAGE:
gender_guess = get_gender_guess(word_list, tag_list, male_titles,
female_titles)
ARGUMENTS:
word_list List of words as produces with clean_tag_names()
tag_list Corresponding list of tags
male_title A list with male title words
female_titles A list with female title words
record_id A string identifying the current record
DESCRIPTION:
First, titles are checked and if a word is found in a gender list of
titles the gender is set.
If no gender could be extracted from title words, the remaining list of
tags is checked, and if a givenname gender is found, it is used.
A final gender value is only returned if it is un-ambiguous (i.e. if both
male and female givennames are found, no gender information is returned).
The returned string is either 'female', 'male' or '' (if no gender has been
found).
"""
male_count = 0
female_count = 0
# Check title words - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
for i in range(len(tag_list)):
if (tag_list[i] == 'TI'):
if (word_list[i] in male_titles):
male_count += 1
elif (word_list[i] in female_titles):
female_count += 1
# Check givenname tags only if no gender in title words found - - - - - - - -
#
if (male_count == 0) and (female_count == 0):
for t in tag_list:
if (t.find('GM') >= 0):
male_count += 1
elif (t.find('GF') >= 0):
female_count += 1
# Check if only male or only female gender found - - - - - - - - - - - - - -
#
if (male_count > 0) and (female_count == 0):
gender = 'male'
elif (male_count == 0) and (female_count > 0):
gender = 'female'
else:
gender = ''
# A log message for high volume log output (level 3) - - - - - - - - - - - -
#
print '3:%s Word list: %s and tag list: %s' % \
(record_id, str(word_list), str(tag_list))
print '3:%s Gender guess extracted: %s' % (record_id, gender)
return gender
# =============================================================================
def get_title(word_list, tag_list, record_id):
"""Extract the title component of a name.
USAGE:
[word_list, tag_list, title_list] = get_title(word_list, tag_list)
ARGUMENTS:
word_list List of words as produces with clean_tag_names()
tag_list Corresponding list of tags
record_id A string identifying the current record
DESCRIPTION:
This routine extracts all title words (and removes them from both the word
and tag list).
It returns a list with title words that were at the beginning of the word
list, but not after a non-title word.
Returns the modified word and tag list with all title words removed, and
the list of extracted title words sorted alphabetically.
"""
title_list = []
tmp_list = []
tmp_tags = []
list_len = len(word_list)
# Extract all title words (list elements with a TI tag) - - - - - - - - - - -
#
for i in range(list_len):
if (tag_list[i] == 'TI'):
title_list.append(word_list[i])
else:
title_list.append('')
tmp_list.append(word_list[i])
tmp_tags.append(tag_list[i])
# Make a dictionary of title words found at beggining of word list - - - - -
#
title_dict = {}
i = 0
while (i < list_len) and (title_list[i] == ''):
i+=1
while (i < list_len) and (title_list[i] != ''):
title_dict.update({title_list[i]:i})
i+=1
title_list = title_dict.keys()
title_list.sort()
# A log message for high volume log output (level 3) - - - - - - - - - - - -
#
print '3:%s Word list: %s and tag list: %s' % \
(record_id, str(word_list), str(tag_list))
print '3:%s Titles extracted: %s' % (record_id, str(title_list))
print '3:%s Modified word list: %s and tag list: %s' % \
(record_id, str(tmp_list), str(tmp_tags))
return [tmp_list, tmp_tags, title_list]
# =============================================================================
def get_name_component(word_list, tag_list, record_id, fields_str):
"""Parse the input word list and extracts names and alternative names.
USAGE:
[names, alt_names] = get_name_component(word_list, tag_list)
ARGUMENTS:
word_list List of words as produces with clean_tag_names()
tag_list Corresponding list of tags as produces with clean_tag_names()
record_id A string identifying the current record
fields_str A string representation of the input fields
DESCRIPTION:
This routine can be used to parse and process a name list that contains
either given names or surnames (but not both of them). It uses the tag list
to distinguish between names and alternative names (which occur after a
separator element or within brackets).
Two lists are returned, the first one with the names and the second one
with the alternative names.
"""
names_list=[[], []] # Output list with primary and alternative names
# Flags (indices into 'names_list') for different name modes
#
name_mode_prim = 0 # Flag for 'primary' name mode
name_mode_alt = 1 # Flag for 'alternative' name mode
curr_name_mode = name_mode_prim # Set current name mode to primary name mode
# Flags for vertical bars mode
#
vb_mode_outside = 0 # 'outside' vertical bars
vb_mode_inside = 1 # 'inside' vertical bars
curr_vb_mode = vb_mode_outside
sep_index = -1 # Gives the index of the last separator element in input list
list_len = len(word_list)
# Loop over all elements in the input list - - - - - - - - - - - - - - - - -
#
i = 0
while i < list_len:
w = word_list[i] # Process this word
t = tag_list[i] # Corresponding tag
if (t == 'ST'): # Process a saint word or name - - - - - - - - - - - - - -
if ('_' not in w): # A saint word only, like saint, brother, holy, etc.
names_list[curr_name_mode].append(w)
else: # A 'saint' plus name sequence, append name to alternative names
names_list[curr_name_mode].append(w)
unders_ind = w.find('_')
names_list[name_mode_alt].append(w[unders_ind+1:]) # Name word only
elif (t in ['RU', 'CO']): # A 'rubbuish' word or a comma - - - - - - - - -
pass # Just pass over it, don't append it to the names list
elif (t == 'BO'): # Process 'baby of' and similar sequences - - - - - - -
names_list[curr_name_mode].append(w)
elif (t == 'PR'): # Process name prefix - - - - - - - - - - - - - - - - -
names_list[curr_name_mode].append(w)
elif (t in ['NU', 'AN']): # A Number or alphanumeric - - - - - - - - - - -
names_list[curr_name_mode].append(w)
print 'warning:%s Number or alpha-numeric word in name: %s%s' % \
(record_id, str(word_list), fields_str)
elif (t == 'HY'): # Process hyphen - - - - - - - - - - - - - - - - - - - -
if (i > 0):
if (tag_list[i-1] not in ['HY','BO','CO','SP','VB']):
names_list[curr_name_mode].append(w) # Don't always append a hyphen
else:
print 'warning:%s Strange hyphen situation: %s%s' % \
(record_id, str(word_list), fields_str)
else:
print 'warning:%s Strange hyphen situation: %s%s' % \
(record_id, str(word_list), fields_str)
elif (t == 'VB'): # Vertical bar, switch vertical bar mode - - - - - - - -
if (i == 0):
print 'warning:%s Strange situation: Vertical bar at beginning: %s%s' \
% (record_id, str(word_list), fields_str)
elif (tag_list[i-1] in ['HY','BO','CO','SP']) or \
((tag_list[i-1] == 'ST') and ('_' not in word_list[i-1])):
print 'warning:%s Strange vertical bar situation: %s%s' % \
(record_id, str(word_list), fields_str)
else: # Switch vertical bar (from outside to inside or vice versa)
if (curr_vb_mode == vb_mode_outside):
curr_vb_mode = vb_mode_inside
curr_name_mode = name_mode_alt # Switch to alternative name mode
# All names between vertival bars (brackets) are in alternative mode
else:
curr_vb_mode = vb_mode_outside
curr_name_mode = name_mode_prim # Switch to primary name mode
elif (t == 'SP'): # A separator like 'known_as', 'and' or 'or' - - - - - -
sep_index = i # Store index of separator list element
# Switch to alternative name if it's 'known_as' not at beginning
#
if (i > 0) and (w == 'known_as'):
curr_name_mode = name_mode_alt # Following names are now alt. names
elif (t == 'NE'): # The word 'nee' (name or separator) - - - - - - - - - -
# 'nee' is a separator if it is not at the beginning and the previous
# word is neither a name prefix, saint name, nor a 'baby_of' sequence
#
if (i > 0) and ((tag_list[i-1] not in ['PR','BO']) or \
((tag_list[i-1] == 'ST') and \
('_' not in word_list[i-1]))):
sep_index = i # Get index of separator word
curr_name_mode = name_mode_alt # Switch to alternative name mode
elif (t == 'II'): # A single character (e.g. initial) - - - - - - - - - -
names_list[name_mode_alt].append(w) # Append single letter to alt. name
# Word must be a name word, tagged with: SN, GF, GM or UN - - - - - - - - -
#
else:
# Check alternative mode: If we're outside vertical bars ('curr_vb_mode'
# is 'vb_mode_outside') and the 'curr_name_mode' is 'name_mode_alt'
# check previous list element
#
if (curr_vb_mode == vb_mode_outside) and \
(curr_name_mode == name_mode_alt):
if (tag_list[i-1] not in ['SP', 'HY', 'PR']):
# Switch back to primary name mode if list element before is a
# 'normal' word (assuming we're behind a 'known_as' sequence)
#
curr_name_mode == name_mode_prim
names_list[curr_name_mode].append(w) # Append word to current name mode
i+=1
# Now check if words are stored in both name lists, and if so remove them
# from the alternative name list
tmp_name_list1 = []
for w in names_list[1]:
if (w not in names_list[0]):
tmp_name_list1.append(w)
# Now check if a word appears twice in the name lists, if so remove one
#
tmp_name_list0 = []
for w in names_list[0]:
if (w not in tmp_name_list0):
tmp_name_list0.append(w)
tmp_name_list11 = []
for w in tmp_name_list1:
if (w not in tmp_name_list11):
tmp_name_list11.append(w)
# A log message for high volume log output (level 3) - - - - - - - - - - - -
#
print '3:%s Word list: %s and tag list: %s' % \
(record_id, str(word_list), str(tag_list))
print '3:%s Primary name(s): %s' % (record_id, str(tmp_name_list0))
print '3:%s Alternative name(s): %s' % (record_id, str(tmp_name_list11))
return (tmp_name_list0, tmp_name_list11)
# =============================================================================
def get_name_rules(word_list, tag_list, first_name_comp, record_id,
fields_str):
"""Parse the input word list using rules and extracts given- and surnames.
USAGE:
[givenname_list, alt_givenname_list, surname_list, alt_surname_list] = \
get_name_rules(word_list, tag_list, first_name_comp)
ARGUMENTS:
word_list List of words as produces with clean_tag_names()
tag_list Corresponding list of tags as produces with
clean_tag_names()
first_name_comp Set to 'gname' if the input is most likely to start with
given names, or to 'sname' if it most likely starts with
surnames.
record_id A string identifying the current record
fields_str A string representation of the input fields
DESCRIPTION:
This routine can be used to parse and process a name list that contains
both given- and surnames. It uses the tag list to distinguish between
given- and surnames, and between names and alternative names (which occur
after a separator element or within brackets).
A list with four sub lists is returned containing
given names
alternative given names
surnames
alternative surnames
"""
if (first_name_comp not in ['gname', 'sname']):
print 'error:%s Illegal value for first_name_comp: %s' % \
(record_id, str(first_name_comp))
raise Exception
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Phase one: Split input word and tag list into up to five sub-lists
# (for words and for tags) at separator elements
#
# Sub-list 1: All words/tags until first separator
# Sub-list 2: All words/tags in first alternative given word area
# Sub-list 3: All words/tags until next separator or end
# Sub-list 4: All words/tags in second alternative given word area
# Sub-list 5: All words/tags after second alternative word area
#
word_sub_list = [[],[],[],[],[]]
tag_sub_list = [[],[],[],[],[]]
# Check if whe word 'nee' (with tag 'NE') is in the list, and decide if - - -
# it is a separator or a name word
#
if ('NE' in tag_list):
i = 0
for i in range(len(tag_list)):
if (tag_list[i] == 'NE'):
# 'nee' is a separator if it is not at the beginning and the word
# before 'nee' is neither a name prefix, a saint name, nor a 'baby_of'
# sequence
#
if (i > 0) and ((tag_list[i-1] not in ['PR','BO']) or \
((tag_list[i-1] == 'ST') and ('_' not in word_list[i-1]))):
tag_list[i] = 'SP'
else:
tag_list[i] == 'UN' # Make it an unknown word
# If no separator or vertical bar in the input then no splitting into - - - -
# sub-lists is needed
#
if ('SP' not in tag_list) and ('VB' not in tag_list):
word_sub_list[0] = word_list
tag_sub_list[0] = tag_list
else: # Perform splitting into sub-lists
list_len = len(word_list)
list_ptr = 0 # Pointer into five sub-lists
i = 0 # Loop index
while (i < list_len):
# Handle separators ('known_as' or 'and' or 'or') - - - - - - - - - - - -
#
if (tag_list[i] == 'SP'):
# Only switch to next sub-list if it's a 'known_as' or 'nee' separator
# not at the beginning, and the element before was not a vertical bar
#
if (i > 0) and (word_list[i] in ['known_as','nee']) and \
(tag_list[i-1] != 'VB'):
if (list_ptr == 4):
print 'warning:%s To many separators/vertical ' % (record_id) + \
'bars in input: %s%s' % (str(word_list), fields_str)
else:
list_ptr += 1 # Switch to next sub-list
if (i == list_len-1):
print 'warning:%s Last element in input is a separator: %s%s' % \
(record_id, str(word_list), fields_str)
else:
i += 1 # Go to next word/tag, i.e. skip over separator
if (tag_list[i] not in ['SP','VB','RU','CO']):
word_sub_list[list_ptr].append(word_list[i]) # Append next element
tag_sub_list[list_ptr].append(tag_list[i])
else: # A strange situation
print 'warning:%s Strange separator situation: %s%s' % \
(record_id, str(word_list), fields_str)
# If current word is a name prefix append following word(s) as well
#
if (tag_list[i] == 'PR') and (i < list_len-1):
i += 1
word_sub_list[list_ptr].append(word_list[i]) # Append name word
tag_sub_list[list_ptr].append(tag_list[i])
if (tag_list[i] == 'PR') and (i < list_len-1): # Check 2. prefix
i += 1
word_sub_list[list_ptr].append(word_list[i]) # Append name word
tag_sub_list[list_ptr].append(tag_list[i])
# Check if word is hyphened with another word, if so append
#
elif (i < list_len-2) and (tag_list[i+1] == 'HY'):
word_sub_list[list_ptr].append(word_list[i+1]) # Append hyphen
tag_sub_list[list_ptr].append(tag_list[i+1])
word_sub_list[list_ptr].append(word_list[i+2]) # Append word
tag_sub_list[list_ptr].append(tag_list[i+2])
i += 2 # Increase pointer in list to next element
# If the following element is not a separator switch to next sub-list
#
if (i < list_len-1) and (tag_list[i+1] != 'SP'):
if (list_ptr == 4):
print 'warning:%s To many separators/vertical ' % (record_id) + \
'bars in input: %s%s' % (str(word_list), fields_str)
else:
list_ptr += 1 # Switch to next sub-list
# If the following word is again 'known_as' change it to 'and' so in
# the next iteration no sub-list switching occurs
#
elif (i < list_len-1) and (word_list[i+1] == 'known_as'):
word_list[i+1] = 'and'
# Handle vertical bars - - - - - - - - - - - - - - - - - - - - - - - - -
#
elif (tag_list[i] == 'VB'):
# Only switch to next sub-list if vertical bar is not at the beginning
#
if (i > 0):
if (list_ptr == 4):
print 'warning:%s To many separators/vertical ' % (record_id) + \
'bars in input: %s%s' % (str(word_list), fields_str)
else:
list_ptr += 1 # Switch to next sub-list
if (i != list_len-1):
i += 1 # Go to next word/tag, i.e. skip vertical bar
# Process all elements until next vertical bar
#
while (i < list_len-1) and (tag_list[i] != 'VB'):
if (tag_list[i] != 'SP'): # Jump over separator elements
word_sub_list[list_ptr].append(word_list[i])
tag_sub_list[list_ptr].append(tag_list[i])
i += 1
# Only switch to next sub-list if the element following the vertical
# bar is not a separator 'known_as'
#
if (i < list_len-1) and (word_list[i+1] != 'known_as'):
if (list_ptr == 4):
print 'warning:%s To many separators/vertical ' % (record_id) + \
'bars in input: %s%s' % (str(word_list), fields_str)
else:
list_ptr += 1 # Switch to next sub-list
# Append all other words/tags to the current sub-lists - - - - - - - - -
#
else:
word_sub_list[list_ptr].append(word_list[i])
tag_sub_list[list_ptr].append(tag_list[i])
if (i < list_len):
i += 1 # Go to next word/tag
# A log message for high volume log output (level 3) - - - - - - - - - - - -
#
print '3:%s Extracted sub-lists:' % (record_id)
print '3:%s Potential given names: %s' % \
(record_id, str(word_sub_list[0]))
print '3:%s %s' % \
(record_id, str(tag_sub_list[0]))
print '3:%s Potential alternative given names: %s' % \
(record_id, str(word_sub_list[1]))
print '3:%s %s' % \
(record_id, str(tag_sub_list[1]))
print '3:%s Potential surnames: %s' % \
(record_id, str(word_sub_list[2]))
print '3:%s %s' % \
(record_id, str(tag_sub_list[2]))
print '3:%s Potential alternative surnames: %s' % \
(record_id, str(word_sub_list[3]))
print '3:%s %s' % \
(record_id, str(tag_sub_list[3]))
print '3:%s Potential second alternative surnames: %s' % \
(record_id, str(word_sub_list[4]))
print '3:%s %s' % \
(record_id, str(tag_sub_list[4]))
# Make sure there are no empty sub-lists between filled sub-lists
#
if (((word_sub_list[3] == []) and (word_sub_list[4] != [])) or \
((word_sub_list[2] == []) and (word_sub_list[3] != [])) or \
((word_sub_list[1] == []) and (word_sub_list[2] != [])) or \
((word_sub_list[0] == []) and (word_sub_list[1] != []))):
print 'warning:%s Empty sub-lists between filled sub-lists: ' % \
(record_id) + '%s, %s, %s, %s, %s%s' % \
(str(word_sub_list[0]), str(word_sub_list[1]), \
str(word_sub_list[2]), str(word_sub_list[3]), \
str(word_sub_list[4]), fields_str)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Phase two: Parse sub-lists and assign into four output name lists
#
givenname_list = []
alt_givenname_list = []
surname_list = []
alt_surname_list = []
# The second name component is the opposite of the first - - - - - - - - - -
# (givenname <-> surname)
#
if (first_name_comp == 'gname'):
second_name_comp = 'sname'
else:
second_name_comp = 'gname'
# Sub-list 4 not empty: Potential second alternative names - - - - - - - - -
#
if (word_sub_list[4] != []):
[n, an] = get_name_component(word_sub_list[4], tag_sub_list[4], record_id,
fields_str)
if (second_name_comp == 'sname'):
alt_surname_list += n + an
else:
alt_givenname_list += n + an
# Sub-list 3 not empty: Potential second alternative names - - - - - - - - -
#
if (word_sub_list[3] != []):
[n, an] = get_name_component(word_sub_list[3], tag_sub_list[3], record_id,
fields_str)
if (second_name_comp == 'sname'):
alt_surname_list = n + an + alt_surname_list
else:
alt_givenname_list = n + an + alt_givenname_list
# Sub-list 2 not empty: Potential second primary names - - - - - - - - - - -
#
if (word_sub_list[2] != []):
[n, an] = get_name_component(word_sub_list[2], tag_sub_list[2], record_id,
fields_str)
if (second_name_comp == 'sname'):
surname_list += n
alt_surname_list = an + alt_surname_list
else:
givenname_list += n
alt_givenname_list = an + alt_givenname_list
# Sub-list 1 not empty: Potential first or second alternative names - - - - -
# This list can now contain either first or second alternative names.
# - If the first name component (e.g. givenname) doesn't have an alternative
# name, but the second name component (e.g. surname) does, then alternative
# surnames are stored in this list.
# - But if the first name component has an alternative name, then sub-list 2
# was not empty, it contained the second name component.
#
if (word_sub_list[1] != []):
if (word_sub_list[2] == []):
# Sub-list 2 is empty, so this sub-list contain alternative names for the
# second name component
#
[n, an] = get_name_component(word_sub_list[1], tag_sub_list[1],
record_id, fields_str)
if (second_name_comp == 'sname'):
alt_surname_list = n + an + alt_surname_list
else:
alt_givenname_list = n + an + alt_givenname_list
else:
# Sub-list 2 not empty, so this sub-list contains alternative names for
# the first name component
#
[n, an] = get_name_component(word_sub_list[1], tag_sub_list[1],
record_id, fields_str)
if (first_name_comp == 'sname'):
alt_surname_list = n + an + alt_surname_list
else:
alt_givenname_list = n + an + alt_givenname_list
# Sub-list 0 should be non-empty all the time - - - - - - - - - - - - - - - -
# - If both sub-list 1 and 2 are not empty, then sub-list 0 (this list)
# contains only the first primary names.
# - But if sub-list two is empty, then for both cases sub-list 1 empty or not
# this sub-list 0 contains both primary name components.
#
if (word_sub_list[0] == []): # This should never happen!
print 'warning:%s Empty first sub-list (this should never happen): %s%s'% \
(record_id, str(word_sub_list), fields_str)
else:
if (word_sub_list[1] != []) and (word_sub_list[2] != []):
# Sub-list 1 and 2 not empty, so this first sub-list contains primary
# names for the first name component
#
[n, an] = get_name_component(word_sub_list[0], tag_sub_list[0],
record_id, fields_str)
if (first_name_comp == 'sname'):
surname_list += n
alt_surname_list = an + alt_surname_list
else:
givenname_list += n
alt_givenname_list = an + alt_givenname_list
else:
# Sub-list 0 contains both primary names for first and second name
# components. This is the most common case of names.
# Loop over all elements in sub-lists 0 - - - - - - - - - - - - - - - - -
#
list_len = len(word_sub_list[0])
# Set current name component where words will be assigned to first
#
name_comp_assign = first_name_comp
gname_ass_count = 0 # Counter for number of words assigned to givenname
sname_ass_count = 0 # Counter for number of words assigned to surname
i = 0
while i < list_len:
w = word_sub_list[0][i] # Process this word
t = tag_sub_list[0][i] # Corresponding tag
if (t == 'ST'): # Process a saint word or name - - - - - - - - - - - -
if ('_' not in w): # A saint word only, like saint, brother, etc.
if (name_comp_assign == 'gname'):
givenname_list.append(w)
gname_ass_count += 1
else:
surname_list.append(w)
sname_ass_count += 1
else: # A 'saint' plus name sequence, append name to altern. names
unders_ind = w.find('_')
if (name_comp_assign == 'gname'):
givenname_list.append(w)
alt_givenname_list.append(w[unders_ind+1:])
gname_ass_count += 1
else:
surname_list.append(w)
alt_surname_list.append(w[unders_ind+1:])
sname_ass_count += 1
elif (t in ['RU','CO']): # A 'rubbuish' word or a comma - - - - - - -
pass # Just pass over it, don't append it to the names list
elif (t == 'BO'): # Process 'baby of' and similar sequences - - - - -
if (name_comp_assign == 'gname'):
givenname_list.append(w)
else:
surname_list.append(w)
elif (t == 'PR'): # Process name prefix - - - - - - - - - - - - - - -
if (name_comp_assign == 'gname'):
givenname_list.append(w)
gname_ass_count += 1
else:
surname_list.append(w)
sname_ass_count += 1
elif (t in ['NU','AN']): # A Number or alphanumeric - - - - - - - - -
if (name_comp_assign == 'gname'):
givenname_list.append(w)
gname_ass_count += 1
else:
surname_list.append(w)
sname_ass_count += 1
print 'warning:%s Number or alpha-numeric word in name: %s%s' % \
(record_id, str(word_list), fields_str)
elif (t == 'HY'): # Process hyphen - - - - - - - - - - - - - - - - - -
if (i > 0):
if (tag_list[i-1] not in ['HY','BO','CO','SP','VB']):
if (name_comp_assign == 'gname'):
givenname_list.append(w)
else:
surname_list.append(w)
else:
print 'warning:%s Strange hyphen situation: %s%s' % \
(record_id, str(word_list), fields_str)
elif (t == 'II'): # A single character (initial) - - - - - - - - - - -
# Append single letter to alternative givennames and switch
# component to surnames (assuming surname follows after an initial)
alt_givenname_list += [w]
name_comp_assign = 'sname'
# Word must be a name word, tagged with: SN, GF, GM, or UN - - - - - -
#
else:
# First check if a hyphen is following followed by another name - - -
# (in which case make sure they get all assigned to the same
# component)
#
if (i < list_len-2):
if (tag_list[i+1] == 'HY') and \
(tag_list[i+2] not in ['SP','HY','CO','VB','RU']):
tmp_w = word_list[i:i+3]
tmp_len = 3
else:
tmp_w = [word_list[i]]
tmp_len = 1
else:
tmp_w = [word_list[i]]
tmp_len = 1
# If the current word (or hyphened words) is the last and no
# givenname has been assigned yet, then assign it to givenname
#
if (gname_ass_count == 0) and \
(((tmp_len == 1) and (i == list_len-1)) or \
((tmp_len == 3) and (i == list_len-3))):
givenname_list += tmp_w
gname_ass_count += 1
# If the current word (or hyphened words) is the last and no surname
# has been assigned yet, then assign it to surname
#
elif (sname_ass_count == 0) and \
(((tmp_len == 1) and (i == list_len-1)) or \
((tmp_len == 3) and (i == list_len-3))):
surname_list += tmp_w
sname_ass_count += 1
# If the current name component is givenname and no givenname has
# been assigned yet
#
elif (name_comp_assign == 'gname') and (gname_ass_count == 0):
givenname_list += tmp_w
gname_ass_count += tmp_len
# If the current name component is surname and no surname has
# been assigned yet
#
elif (name_comp_assign == 'sname') and (sname_ass_count == 0):
surname_list += tmp_w
sname_ass_count += tmp_len
# If it no hyphened name and the tag of the current word is
# givenname (only) then assign to givenname
#
elif (tmp_len == 1) and ((t == 'GF') or (t == 'GM')):
givenname_list += tmp_w
gname_ass_count += 1
# If it no hyphened name and the tag of the current word is
# surname (only) then assign to surname
#
elif (tmp_len == 1) and (t == 'SN'):
surname_list += tmp_w
sname_ass_count += 1
# If the current name component is givenname and the current word is
# (not only) a surname, then assign current name to givenname
#
elif (name_comp_assign == 'gname') and (tmp_len == 1) and \
(t != 'SN'):
givenname_list += tmp_w
gname_ass_count += 1
# If the current name component is surname and the current word is
# (not only) a givenname, then assign current name to surname
#
elif (name_comp_assign == 'sname') and (tmp_len == 1) and \
(t != 'GF') and (t != 'GM'):
surname_list += tmp_w
sname_ass_count += 1
else: # Append to current name component
if (name_comp_assign == 'gname'):
givenname_list += tmp_w
gname_ass_count += 1
else:
surname_list += tmp_w
sname_ass_count += 1
if (tmp_len == 3):
i += 2
i += 1
# Now do some post-processing steps - - - - - - - - - - - - - - - - - - - - -
#
# First, if a hyphened name is in a primary name, or a name containing an
# underscore, add the components (if they are not name prefixes) into the
# corresponding alternative name.
#
#for n in givenname_list:
# if ('-' in n):
# tmp_gname_list = n.split('-')
# elif ('_' in n):
# tmp_gname_list = n.split('_')
# else:
# tmp_gname_list = []
# Check each word in the list if they are in a givenname dictionary, not
# a name prefix, not in ('baby', 'son', 'daughter', 'of') or 'saint'
#
# for gn in tmp_gname_list: # Now check each component in this list
# if (not config.nameprefix_dict.has_key(gn)) and \
# (gn not in ['baby','son','daughter','of']):
# if (config.givenname_f_dict.has_key(gn)):
# alt_givenname_list.append(config.givenname_f_dict[gn])
# if (config.givenname_m_dict.has_key(gn)):
# alt_givenname_list.append(config.givenname_m_dict[gn])
# if (not config.givenname_f_dict.has_key(gn)) and \
# (not config.givenname_m_dict.has_key(gn)):
# alt_givenname_list.append(gn)
# Now the same for surnames
#
#for n in surname_list:
# if ('-' in n):
# tmp_sname_list = n.split('-')
# elif ('_' in n):
# tmp_sname_list = n.split('_')
# else:
# tmp_sname_list = []
# Check each word in the list if they are in surname dictionary, not
# a name prefix, not in ('baby', 'son', 'daughter', 'of') or 'saint'
#