-
Notifications
You must be signed in to change notification settings - Fork 0
/
lookup.py
657 lines (491 loc) · 23.4 KB
/
lookup.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
# =============================================================================
# AUSTRALIAN NATIONAL UNIVERSITY OPEN SOURCE LICENSE (ANUOS LICENSE)
# VERSION 1.3
#
# The contents of this file are subject to the ANUOS License Version 1.3
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at:
#
# https://sourceforge.net/projects/febrl/
#
# 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: "lookup.py"
#
# The Initial Developer of the Original Software is:
# Dr Peter Christen (Research School of Computer Science, The Australian
# National University)
#
# Copyright (C) 2002 - 2011 the Australian National University and
# others. All Rights Reserved.
#
# Contributors:
#
# Alternatively, the contents of this file may be used under the terms
# of the GNU General Public License Version 2 or later (the "GPL"), in
# which case the provisions of the GPL are applicable instead of those
# above. The GPL is available at the following URL: http://www.gnu.org/
# If you wish to allow use of your version of this file only under the
# terms of the GPL, and not to allow others to use your version of this
# file under the terms of the ANUOS License, indicate your decision by
# deleting the provisions above and replace them with the notice and
# other provisions required by the GPL. If you do not delete the
# provisions above, a recipient may use your version of this file under
# the terms of any one of the ANUOS License or the GPL.
# =============================================================================
#
# Freely extensible biomedical record linkage (Febrl) - Version 0.4.2
#
# See: http://datamining.anu.edu.au/linkage.html
#
# =============================================================================
"""Module lookup.py - Classes for various types of look-up tables.
This module contains classes for look-up table and correction lists.
"""
# =============================================================================
# Import necessary modules (Python standard modules first, then Febrl modules)
import logging
import auxiliary
# =============================================================================
class LookupTable(dict):
"""class LookupTable - Based on dictionary type.
"""
# ---------------------------------------------------------------------------
def __init__(self, **kwargs):
"""Constructor, set general attributes.
"""
dict.__init__(self) # Initialise dictionary base type
self.description = ''
self.created = ''
self.modified = ''
self.file_names = []
self.default = None # Default return value for non existing keys
self.length = None # Number of entries in the look-up table
for (keyword, value) in kwargs.items():
if (keyword.startswith('desc')):
auxiliary.check_is_string('description', value)
self.description = value
elif (keyword.startswith('defau')):
self.default = value
elif (keyword.startswith('creat')):
self.created = value
elif (keyword.startswith('modif')):
self.modified = value
else:
logging.exception('Illegal constructor argument keyword: "%s"' % \
(str(keyword)))
raise Exception
# ---------------------------------------------------------------------------
def __getitem__(self, key):
"""Return an item in the look-up table with the given key. If not found,
return the default value.
"""
try:
return dict.__getitem__(self, key)
except KeyError:
return self.default
# ---------------------------------------------------------------------------
def get(self, key, *args):
"""Return an item in the look-up table with the given key. If not found,
return the default value.
"""
if (not args):
args = (self.default,)
return dict.get(self, key, *args)
# ---------------------------------------------------------------------------
def load(self, file_names):
"""Load one or more files into the look-up table.
See implementations in derived classes for details.
"""
logging.exception('Override abstract method in derived class')
raise Exception
# =============================================================================
class TagLookupTable(LookupTable):
"""A look-up table class for look-up tables with word corrections and tags.
"""
# ---------------------------------------------------------------------------
def __init__(self, **kwargs):
LookupTable.__init__(self, **kwargs) # Initialise base class
self.max_key_length = None # The maximum length of a key in words
# A log message - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
logging.info('Initialised tag look-up table "%s"' %(self.description))
logging.info(' With default: "%s"' % (str(self.default)))
# ---------------------------------------------------------------------------
def load(self, file_names):
"""Load one or more files with word corrections and tags into the look-up
table.
See Febrl manual for details on the file format.
"""
# Check input argument type - - - - - - - - - - - - - - - - - - - - - - - -
#
if (isinstance(file_names, str)):
file_names = [file_names] # Make a list out of a single file name
auxiliary.check_is_list('file_names', file_names)
i = 0
for file_name in file_names:
auxiliary.check_is_string('file_name[%d]' % (i), file_name[i])
i += 1
self.file_names = file_names
self.clear() # Remove all items from the look-up table
self.max_key_length = 0
# Loop over file names - - - - - - - - - - - - - - - - - - - - - - - - - -
#
for fn in self.file_names:
try: # Open file and read all lines into a list
f = open(fn,'r')
except:
logging.exception('Cannot read from file "%s"' % (fn))
raise IOError
file_data = f.readlines() # Read complete file
f.close()
tag = '' # Start with no tag
key = '' # Start with an empty key
# Now process all lines - - - - - - - - - - - - - - - - - - - - - - - - -
#
for line in file_data:
l = line.strip() # Remove line separators
if (len(l) > 0) and (l[0] != '#'): # Not empty line and not comment
if (l[:5] == 'tag=<'): # It's a line with a new tag
tag = l[5:7]
else: # A line with an entry
# Make sure a tag is set
#
if (tag == ''):
logging.exception('Missing tag specification in file "%s"' % \
(fn))
raise Exception
line_list = l.split(':') # Separate key from values
if (len(line_list) > 2):
logging.exception('Illegal format in file "%s" in line: %s' % \
(fn, l))
raise Exception
if (len(line_list) == 2): # Line contains a key - - - - - - - - -
key = line_list[0].strip().lower() # Get and clean key
key_list = key.split(' ') # Make a list of key words
if (len(key_list) > self.max_key_length):
self.max_key_length = len(key_list) # Update maximal key length
# Insert key itself into lookup table
#
dict_val = '_'.join(key_list)
dict_key = tuple(key_list)
this_tag = tag
if (self.__contains__(dict_key)): # Already in lookup table
test_item = self.__getitem__(dict_key)
test_val = test_item[0] # Value without tag
test_tag = test_item[1]
if (dict_val != test_val):
logging.warn('Key "%s" already in dictionary with ' % \
(str(dict_val)) + 'different value (old value ' + \
'will be over written with "%s")' % (str(test_val)))
if (test_tag.find(this_tag) < 0): # This tag is new
this_tag = test_tag+'/'+this_tag # Tag for this entry
else:
this_tag = test_tag
this_val = (dict_val, this_tag)
self.__setitem__(dict_key,this_val) # Insert key itself
vals = line_list[1].lower() # Get values in this line in a string
elif (len(line_list) == 1): # Line contains only values - - - - -
vals = line_list[0].lower() # Get values in this line in a string
# Porcess all values right of ':' in this line
val_list = vals.split(',') # Split values into a list
for val in val_list: # Loop over all values - - - - - - - - - - -
val_strip = val.strip()
if (val_strip != ''): # Only append non-empty values
key_list = val_strip.split(' ') # Make a list of key words
if (len(key_list) > self.max_key_length):
self.max_key_length = len(key_list) # Update maximal key len
dict_key = tuple(key_list)
this_tag = tag
if (self.__contains__(dict_key)):
test_item = self.__getitem__(dict_key)
test_val = test_item[0] # Value without tag
test_tag = test_item[1]
if (dict_val != test_val):
logging.warn('Key "%s" already in dictionary with ' % \
(str(dict_val)) + 'different value (old value ' + \
'will be over written with "%s")' % (str(test_val)))
if (test_tag.find(this_tag) < 0): # This tag is new
this_tag = test_tag+'/'+this_tag # Tag for this entry
else:
this_tag = test_tag
this_val = (dict_val, this_tag)
self.__setitem__(dict_key,this_val)
self.length = self.__len__() # Get number of elements in the look-up table
# A log message - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
logging.info('Loaded tag look-up table "%s"' % (self.description))
logging.info(' From files: %s' % (str(self.file_names)))
logging.info(' Number of entries: %i' % (self.length))
logging.info(' Maximal key length: %i' % (self.max_key_length))
# =============================================================================
class FrequencyLookupTable(LookupTable):
"""A look-up table class for look-up tables with words and frequencies.
"""
# ---------------------------------------------------------------------------
def __init__(self, **kwargs):
LookupTable.__init__(self, **kwargs) # Initialise base class
self.sum = None # The sum of all frequency counts
self.default = 1
# A log message - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
logging.info('Initialised frequency look-up table "%s"' % \
(self.description))
logging.info(' With default: %s' % (str(self.default)))
# ---------------------------------------------------------------------------
def load(self, file_names):
"""Load one or more files with words and their frequency counts into the
look-up table.
See Febrl manual for details on the file format.
"""
# Check input argument type - - - - - - - - - - - - - - - - - - - - - - - -
#
if (isinstance(file_names, str)):
file_names = [file_names] # Make a list out of a single file name
auxiliary.check_is_list('file_names', file_names)
i = 0
for file_name in file_names:
auxiliary.check_is_string('file_name[%d]' % (i), file_name[i])
i += 1
self.file_names = file_names
self.clear() # Remove all items from the look-up table
self.sum = 0
# Loop over file names - - - - - - - - - - - - - - - - - - - - - - - - - -
#
for fn in self.file_names:
try: # Open file and read all lines into a list
f = open(fn,'r')
except:
logging.exception('Cannot read from file "%s"' % (fn))
raise IOError
file_data = f.readlines() # Read complete file
f.close()
# Now process all lines - - - - - - - - - - - - - - - - - - - - - - - - -
#
for line in file_data:
l = line.strip()
if (len(l) > 0) and (l[0] != '#'): # Not empty line and not comment
ll = l.split(',') # Get fields from a line
# Check for two columns
#
if (len(ll) != 2):
logging.exception('Illegal file format (not 2 columns) in file' + \
': "%s" in line: %s"' % (fn, l))
raise Exception
key = ll[0].strip().lower() # Make sure it's lower case
val = ll[1].strip().lower()
try:
val = int(val) # Convert the value into an integer
except:
logging.exception('Illegal value for frequency count: "%s"' % \
(str(val)) + ' in line: "%s"' % (l))
raise Exception
if (self.__contains__(key)):
val += self.__getitem__(key) # Sum up counts
self.__setitem__(key, val)
self.sum += val
self.length = self.__len__() # Get number of elements in the look-up table
# A log message - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
logging.info('Loaded frequency look-up table "%s"' % \
(self.description))
logging.info(' From files: %s' % (str(self.file_names)))
logging.info(' Number of entries: %i' % (self.length))
logging.info(' Sum of all value: %i' % (self.sum))
# =============================================================================
class GeocodeLookupTable(LookupTable):
"""A look-up table class for look-up tables with entires and their locations.
For each entry in the look-up table, its longitude and latitude are given.
The file format is three columns comma separated text file (CSV).
For each entry, the key is the name of the locality, and the value is a
list with the two entries [longitude,latitude].
"""
# ---------------------------------------------------------------------------
def __init__(self, **kwargs):
LookupTable.__init__(self, **kwargs) # Initialise base class
self.default = []
# A log message - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
logging.info('Initialised geocode look-up table "%s"' % \
(self.description))
logging.info(' With default: %s' % (str(self.default)))
# ---------------------------------------------------------------------------
def load(self, file_names):
"""Load one or more files with entries and their localities into the
table.
See Febrl manual for details on the file format.
"""
# Check input argument type - - - - - - - - - - - - - - - - - - - - - - - -
#
if (isinstance(file_names, str)):
file_names = [file_names] # Make a list out of a single file name
auxiliary.check_is_list('file_names', file_names)
i = 0
for file_name in file_names:
auxiliary.check_is_string('file_name[%d]' % (i), file_name[i])
i += 1
self.file_names = file_names
self.clear() # Remove all items from the look-up table
# Loop over file names - - - - - - - - - - - - - - - - - - - - - - - - - -
#
for fn in self.file_names:
try: # Open file and read all lines into a list
f = open(fn,'r')
except:
logging.exception('Cannot read from file "%s"' % (fn))
raise IOError
file_data = f.readlines() # Read complete file
f.close()
# Now process all lines - - - - - - - - - - - - - - - - - - - - - - - - -
#
for line in file_data:
l = line.strip()
if (len(l) > 0) and (l[0] != '#'): # Not empty line and not comment
ll = l.split(',') # Get fields from a line
# Check for three columns
#
if (len(ll) != 3):
logging.exception('Illegal file format (not 3 columns) in file' + \
': "%s" in line: %s' % (fn, l))
raise Exception
key = ll[0].strip().lower() # Make sure it's lower case
long = ll[1].strip()
lati = ll[2].strip()
# Try to convert into numerical (float) values
#
try:
long = float(long)
except:
logging.exception('Longitude: "%s" is not a number in line: "%s"' \
% (str(long), l))
raise Exception
try:
lati = float(lati)
except:
logging.exception('Lattitude: "%s" is not a number in line: "%s"' \
% (str(lati), l))
raise Exception
# And check their values
#
if (long < -180.0) or (long > 180.0):
logging.exception('Illegal value for longitude: '+str(long))
raise Exception
if (lati < -90.0) or (lati > 90.0):
logging.exception('Illegal value for latitude: '+str(lati))
raise Exception
val = [long,lati] # Value for dictionary
if (self.__contains__(key)) and (self.__getitem__(key) != val):
logging.exception('Key "%s" already in look-up table with ' % \
(str(key)) + 'different value')
raise Exception
self.__setitem__(key, val)
self.length = self.__len__() # Get number of elements in the look-up table
# A log message - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
logging.info('Loaded geocode look-up table "%s"' % (self.description))
logging.info(' From files: %s' % (str(self.file_names)))
logging.info(' Number of entries: %i' % (self.length))
# =============================================================================
class CorrectionList(list):
"""A class for correction lists (containing original and replacement
strings).
"""
# ---------------------------------------------------------------------------
def __init__(self, **kwargs):
"""Constructor, set general attributes.
"""
list.__init__(self) # Initialise list base type
self.description = ''
self.created = ''
self.modified = ''
self.file_name = ''
self.length = None # Number of entries in the correction list
for (keyword, value) in kwargs.items():
if (keyword.startswith('desc')):
auxiliary.check_is_string('description', value)
self.description = value
elif (keyword.startswith('creat')):
self.created = value
elif (keyword.startswith('modif')):
self.modified = value
else:
logging.exception('Illegal constructor argument keyword: "%s"' % \
(str(keyword)))
raise Exception
# A log message - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
logging.info('Initialised correction list "%s"' % (self.description))
# ---------------------------------------------------------------------------
def load(self, file_name):
"""Load one correction list file into a sorted (decreasing length) list.
See Febrl manual for details on the file format.
"""
# Check input argument type and open file - - - - - - - - - - - - - - - - -
#
auxiliary.check_is_string('file_name', file_name)
self.file_name = file_name
# Make sure the list is empty, remove all items from the correction list
#
while (self.__len__() > 0):
self.pop()
try: # Open file and read all lines into a list
f = open(self.file_name, 'r')
except:
logging.exception('Cannot read from file "%s"' % (str(self.file_name)))
raise IOError
file_data = f.readlines() # Read complete file
f.close()
org_list = [] # List of original strings (the ones to be replaced)
repl_list = [] # List of replacement strings
len_list = [] # List of original string lengths
repl = '' # Set inital replacement to nothing
# Now process all lines - - - - - - - - - - - - - - - - - - - - - - - - - -
#
for line in file_data:
l = line.strip() # Remove line separators at the end
if (len(l) > 0) and (l[0] != '#'): # Not an empty line and not comment
ll = l.split(':=') # Separate replacement from values
if (len(ll) == 2): # Line contains a replacement - - - - - - - - - - -
repl = ll[0].strip().lower() # Make replacement lower and strip
if (not ((repl[0] == '"') and (repl[-1] == '"') or \
(repl[0] == "'") and (repl[-1] == "'"))):
logging.exception('Replacement string is not properly quoted: '+ \
'"%s" in file: "%s"' % (repl, str(self.file_name)))
raise Exception
repl = repl[1:-1] # Remove quotes from replacement string
v = ll[1].lower() # Get values in a string and make lowercase
elif (len(ll) == 1): # Line contains only values - - - - - - - - - - -
v = ll[0].lower() # Get values in a string and make lowercase
else: # More than one ':=' separator in the line - - - - - - - - - - -
logging.exception('Too many ":=" separators in line: "%s"' % (l))
raise Exception
# Now process the values and append them to the list - - - - - - - - -
vv = v.split(',') # Split values into a list
for v in vv: # Loop over all values - - - - - - - - - - - - - - - -
org = v.strip() # Get the original string
if (org != ''): # Only process non-empty values
if (not ((org[0] == '"') and (org[-1] == '"') or \
(org[0] == "'") and (org[-1] == "'"))):
logging.exception('Original string is not properly quoted: '+ \
'"%s" in file: "%s"' % (org, str(self.file_name)))
raise Exception
org = org[1:-1] # Remove quotes from original string
if (org != ''): # Only append non-empty values
org_list.append(org)
repl_list.append(repl)
len_list.append(len(org))
tmp_list = map(None,len_list,org_list,repl_list)
tmp_list.sort()
tmp_list.reverse()
for (i,org,repl) in tmp_list:
self.append((org,repl))
self.length = self.__len__() # Get number of elements in the look-up table
# A log message - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
#
logging.info('Loaded correction list "%s"' % (self.description))
logging.info(' From file: %s' % (str(self.file_name)))
logging.info(' Number of entries: %i' % (self.length))
# =============================================================================