-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnameTable.py
executable file
·450 lines (371 loc) · 13.7 KB
/
nameTable.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
#!/usr/bin/python3
# PyElly - rule-based tool for analyzing natural language (Python v3.8)
#
# nameTable.py : 14nov2019 CPM
# ------------------------------------------------------------------------------
# Copyright (c) 2019, Clinton Prentiss Mah
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
"""
a table for identifying personal name components as a given internal type (see TYP)
a single-word component can also be specified in one of four special forms,
where X is a substring of at least TWO literal chars
(1) X+ X is a prefix, and the rest of a lookup string must be a known component
(2) X- X is a prefix, and the rest of a lookup string does not matter
(3) +X X is a suffix, and the rest of a lookup string must be a known component
(4) -X X is a suffix, and the rest of a lookup string does not matter
the CND (=0) type allows for handling of multi-word components in tables
to put all name-related rules in a single persistent object, a name table will
also include phonetic patterns of common names for name component inference
"""
import sys
import ellyChar
import ellyException
import phondexEN
# component type encodings
REJ = -3 # reject match immediately
STP = -2 # not name component
NON = -1 # no match
CND = 0 # conditional match
TTL = 1 # title
HON = 2 # honorific
PNM = 3 # personal name
SNM = 4 # surname
XNM = 5 # either personal or surname
SNG = 6 # single component name
INI = 7 # initial
REL = 8 # relational qualifier
CNJ = 9 # AND
GEN = 10 # generation indicator
TYP = { 'rej':REJ , 'stp':STP , 'cnd':CND ,
'pnm':PNM , 'snm':SNM , 'xnm':XNM , 'sng':SNG ,
'gen':GEN , 'ttl':TTL , 'hon':HON , 'rel':REL , 'ini':INI ,
'cnj':CNJ } # internal name component types (NOT syntactic types!)
START = [ TTL ] # these types must start a name
END = [ GEN ] # these types must end a name
NOTEND = [ REL , GEN ] # these must not end a name
# restrictions on sequencing of component types
def starts ( typ ):
"""
does type have to start a name?
arguments:
typ - encoded type
returns:
True if yes, False otherwise
"""
return (typ in START)
def ends ( typ ):
"""
does type have to end a name?
arguments:
typ - encoded type
returns:
True if yes, False otherwise
"""
return (typ in END)
def mustNOTend ( typ ):
"""
type cannot end a name?
arguments:
typ - encoded type
returns:
True if yes, False otherwise
"""
return (typ in NOTEND)
class NameTable(object):
"""
support for name entity extraction
attributes:
pres - name starting patterns
posts - name ending patterns
dictn - dictionary for looking up components
phone - list of common name phonetics
compn - compound component being matched
_nerr - error count in table definition
"""
def __init__ ( self , inpr ):
"""
define table from text input
arguments:
self -
inpr - EllyDefinitionReader
throws:
TableFailure on table definition failure
"""
self.pres = { }
self.posts = { }
self.dictn = { }
self.phone = [ ]
self.compn = ''
self._nerr = 0
# print ( 'TYP=' , TYP )
while True:
lin = inpr.readline().lower() # ignore capitalization
if len(lin) == 0: break
if lin[0] == '=': # phonetic entry?
lin = lin[1:] # if so, remove marker
first = ''
if lin[0] == 'a': # vowel is first?
first = 'a' # if so, remove it
lin = lin[1:]
pho = first + lin.upper() # combine any vowel with uppercase rest
self.phone.append(pho) # save in phonetic list
continue
lins = lin.strip().split(':')
if len(lins) != 2: # type definition must have two parts
self._err(lne=lin)
continue
typ = lins[1].strip() # get component type
if not typ in TYP:
self._err('bad name component type',lin)
continue
cod = TYP[typ]
els = lins[0].strip().split(' ') # name component
# print ( 'type=' , '"' + typ + '"' , els )
lim = len(els)
if lim == 1:
cmpo = els[0]
chf = cmpo[0] # first char of component
chl = cmpo[-1] # last char
if chf == '-' or chf == '+':
if not ellyChar.isLetter(chl) or len(cmpo) < 3:
self._err('bad end of name',lin)
continue
dky = cmpo[-2:] # dictionary key is 2 chars only
if not dky in self.posts:
self.posts[dky] = [ ]
self.posts[dky].append([ cmpo[1:] , cod , (chf == '+') ])
elif chl == '-' or chl == '+':
if not ellyChar.isLetter(chf) or len(cmpo) < 3:
self._err('bad start of name',lin)
continue
dky = cmpo[:2] # dictionary key is 2 chars only
if not dky in self.pres:
self.pres[dky] = [ ]
self.pres[dky].append([ cmpo[:-1] , cod , (chl == '+') ])
else:
self.dictn[cmpo] = cod
if cmpo[-1] == '.': # if ending with '.' , also save without
self.dictn[cmpo[:-1]] = cod
continue
Nix = 1
while Nix <= lim: # process elements of name component
cmpo = ' '.join(els[0:Nix])
Nix += 1
if cmpo not in self.dictn: # first Nix elements
self.dictn[cmpo] = CND
if self.dictn[cmpo] != CND:
self._err('name component redefined',lin)
continue
self.dictn[cmpo] = TYP[typ] # put into table
if self._nerr > 0:
print ( '**' , self._nerr , 'name errors in all' , file=sys.stderr )
print ( 'name table definition FAILed', file=sys.stderr )
raise ellyException.TableFailure
def filled ( self ):
"""
check if name table is non-empty
arguments:
self
returns:
True if non-empty, False otherwise
"""
return (len(self.dictn.keys()) > 0)
def lookUp ( self , itm ):
"""
look up substring in name table
arguments:
self -
itm - string to look up
returns:
-1 if not found, component type code >= 0 otherwise
"""
self.compn = ''
itm = itm.lower()
status = self._find(itm)
if status >= 0:
self.compn = itm
return status
def lookUpMore ( self , itm ):
"""
look up compounding of substring in name table
arguments:
self -
itm - string to look up
returns:
-1 if not found, component type code >= 0 otherwise
"""
if self.compn == '':
return NON
else:
cat = self.compn + ' ' + itm.lower()
status = self._find(cat,False)
if status >= 0:
self.compn = cat
return status
def _find ( self , cmpo , smpl=True ):
"""
lookup method with recursion
arguments:
self -
cmpo - simple or compound component
smpl - simple flag
returns:
-1 or -2 if not found, component type code >= 0 otherwise
"""
# print ( '_find:' , cmpo )
lcmp = len(cmpo)
if lcmp == 0:
return NON
if cmpo in self.dictn: # full name component known?
return self.dictn[cmpo]
if lcmp == 1:
return INI if ellyChar.isLetter(cmpo[0]) else NON
if cmpo[-1] == '.': # component ends in '.'?
if lcmp == 2:
if ellyChar.isLetter(cmpo[0]):
return INI
return NON
if smpl and lcmp > 4: # check component by parts?
pre = cmpo[:2]
suf = cmpo[-2:]
# print ( 'pre=' , pre , 'suf=' , suf )
if pre in self.pres: # if not known, check for prefix match
for p in self.pres[pre]:
x = p[0]
n = len(x)
# print ( 'recursion=' , p[2] )
if (n < lcmp and
cmpo[:n] == x): # prefix match found?
if not p[2] or self._find(cmpo[n:]) > 0:
return p[1]
elif suf in self.posts: # last resort is check for suffix match
for p in self.posts[suf]:
x = p[0]
n = len(x)
# print ( 'recursion=' , p[2] )
if (n < lcmp and
cmpo[-n:] == x): # suffix match found?
if not p[2] or self._find(cmpo[:-n]) > 0:
return p[1]
return NON
def _err ( self , msg='bad component definition' , lne='' ):
"""
for error handling
arguments:
self -
msg - error message
lne - problem line
"""
self._nerr += 1
print ( '** name error:' , msg , file=sys.stderr )
if lne != '':
print ( '* at [' , lne , ']' , file=sys.stderr )
def checkPhonetic ( self , tokn ):
"""
look for phonetic in table listing
arguments:
self -
tokn - token as list of chars
returns:
True if found, False otherwise
"""
pho = phondexEN.phondex(tokn)
return (pho in self.phone)
def dump ( self ):
"""
show contents of name table
arguments:
self
"""
stp = [ ]
print ( '------------' )
nix = 0
kys = sorted(self.dictn.keys())
for ky in kys:
cod = self.dictn[ky]
if cod == STP:
stp.append(ky)
elif cod == REJ:
stp.append(ky.upper())
else:
print ( ' {0:<11.11s}:{1:2d}'.format(ky,cod) , end=' ' )
nix += 1
if nix%6 == 0: print ()
print ()
print ( '------------' )
kys = sorted(self.pres.keys())
for ky in kys:
lp = self.pres[ky]
for p in lp:
x = '+' if p[2] else '-'
print ( ky , '/' , p[0] + x , ':' , p[1] )
print ( '------------' )
kys = sorted(self.posts.keys())
for ky in kys:
lp = self.posts[ky]
for p in lp:
x = '+' if p[2] else '-'
print ( ky , '/' , x + p[0] , ':' , p[1] )
print ( '------- stop' )
nix = 0
for st in stp:
print ( ' {0:<10.10s}'.format(st) , end=' ' )
nix += 1
if nix%8 == 0: print ()
print ()
print ( '------- phonetic' )
nix = 0
for ph in self.phone:
print ( '{0:<8.8s}'.format(ph) , end=' ' )
nix += 1
if nix%9 == 0: print ()
print ()
#
# unit test
#
if __name__ == '__main__':
import ellyConfiguration
import ellyDefinitionReader
inp = 'test' if len(sys.argv) == 1 else sys.argv[1]
pnm = ellyConfiguration.baseSource + inp + '.n.elly'
rdr = ellyDefinitionReader.EllyDefinitionReader(pnm)
if rdr.error != None:
print ( rdr.error )
sys.exit(1)
try:
ntb = NameTable(rdr)
except ellyException.TableFailure:
sys.exit(1)
print ( 'name test with' , '<' + pnm + '>' )
print ( 'table filled=' , ntb.filled() )
ntb.dump()
while True: # test examples from standard input
sys.stdout.write('> ')
sys.stdout.flush()
tos = sys.stdin.readline().strip()
if len(tos) == 0: break
idx = ntb.lookUp(tos)
print ( 'lookUp' , '(' , tos , ')= ' , end=' ' )
print ( idx if idx >= 0 else 'NONE' )