-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpatternTable.py
More file actions
executable file
·546 lines (443 loc) · 18.9 KB
/
patternTable.py
File metadata and controls
executable file
·546 lines (443 loc) · 18.9 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
#!/usr/bin/python
# PyElly - scripting tool for analyzing natural language
#
# patternTable.py : 20dec2018 CPM
# ------------------------------------------------------------------------------
# Copyright (c) 2013, 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.
# -----------------------------------------------------------------------------
"""
finite-state automaton (FSA) for inferring the syntactic type of simplei or x
hyphenated tokens
"""
import sys
import ellyChar
import ellyWildcard
import ellyException
import syntaxSpecification
import featureSpecification
class Link(object):
"""
for defining FSA transitions with Elly pattern matching
attributes:
patn - Elly pattern to match
nxts - next state on match
catg - syntactic category
synf - syntactic features
semf - semantic features
bias - initial plausibility score
"""
def __init__ ( self , syms , dfls ):
"""
initialization
arguments:
self -
syms - Elly grammatical symbol table
dfls - definition elements in list
exceptions:
FormatFailure on error
"""
# print 'dfls=' , dfls
ne = len(dfls)
# print 'ne=' , ne
if 3 > ne or ne > 5: # must have 3 to 5 elements
raise ellyException.FormatFailure
else:
if dfls[0] == '\\0':
self.patn = u'\x00' # special nul pattern
elif ellyWildcard.numSpaces(list(dfls[0])) > 0:
print >> sys.stderr , '** link pattern includes space:' , dfls[0]
raise ellyException.FormatFailure
else:
# print 'do conversion'
self.patn = ellyWildcard.convert(dfls[0]) # encode Elly pattern
# print 'patn=' , self.patn
if dfls[0] != '$':
if self.patn == None or ellyWildcard.minMatch(self.patn) == 0:
print >> sys.stderr , '** bad link pattern:' , dfls[0]
raise ellyException.FormatFailure
# print 'appended patn=' , list(self.patn) , '=' , len(self.patn)
lastat = dfls[-1]
self.catg = None # defaults
self.synf = None #
self.semf = None #
self.bias = 0 #
sss = dfls[1].lower() # assumed not to be Unicode
# print 'sss=' , sss
if sss != '-': # allow for no category
syx = syntaxSpecification.SyntaxSpecification(syms,sss)
if syx != None:
if not lastat in ['-1','-2']: # not a stop state for matching
raise ellyException.FormatFailure # cannot have syntax here
self.catg = syx.catg # syntactic category
self.synf = syx.synf.positive # syntactic features
if ne > 3:
if lastat != '-1': # not a stop state for matching
raise ellyException.FormatFailure # cannot have semantics here
sss = None if dfls[2] == '-' else dfls[2].lower()
else:
sss = None
# print 'semantic features=' , sss
sem = featureSpecification.FeatureSpecification(syms,sss,True)
self.semf = sem.positive # get semantic features
# print 'semf=' , self.semf
if ne > 4:
try:
self.bias = int(dfls[3])
except ValueError:
raise ellyException.FormatFailure # unrecognizable bias
try:
n = int(lastat) # next state for link
except ValueError:
raise ellyException.FormatFailure # unrecognizable number
# print 'transition=' , n
if n < 0: # final transition?
if self.patn == u'\x00':
raise ellyException.FormatFailure # final state not allowed here
if n == -1:
pe = self.patn[-1] # if so, get last pattern element
if ( pe != ellyWildcard.cALL and # final pattern must end with * or $
pe != ellyWildcard.cEND ):
self.patn += ellyWildcard.cEND # default is $
print >> sys.stderr , '** final $ added to pattern' , list(self.patn)
self.nxts = n # specify next state
def __unicode__ ( self ):
"""
string representation of link
arguments:
self
returns:
string for printing out
"""
if self.patn == None:
return u'None'
else:
p = u'{0:<24}'.format(ellyWildcard.deconvert(self.patn))
c = unicode(self.catg)
x = u'' if self.synf == None else self.synf.hexadecimal(False)
m = u'' if self.semf == None else self.semf.hexadecimal(False)
b = str(self.bias)
n = ' next=' + unicode(self.nxts) if self.nxts >= 0 else ' stop'
if self.nxts == -2: n += '+breaking'
return p + ' ' + ' ' + c + ' ' + x + ' ' + m + ' ' + b + n
def shortcode ( self ):
"""
short description of FSA link
arguments:
self
returns:
state and link pattern as a string
"""
return str(self.nxts) + ': ' + ellyWildcard.deconvert(self.patn)
#
#### main code section
#
Trmls = ellyWildcard.Trmls
def bound ( segm ):
"""
get maximum limit on string for pattern matching
(override this method if necessary)
arguments:
segm - text segment to match against
returns:
char count
"""
# print 'segm=' , segm
lm = len(segm) # limit can be up to total length of text for matching
ll = 0
while ll < lm: # look for first space in text segment
if segm[ll] == ' ': break
ll += 1
# print 'll=' , ll , ', lm=' , lm
ll -= 1
while ll > 0: # exclude trailing non-alphanumeric from matching
# except for '.', Unicode prime, or '*' and bracketing
c = segm[ll]
# print 'bound c=' , c , '{:x}'.format(ord(c))
if (c in Trmls or c in [ u'*' , u'\u2032' , u'+' ]
or ellyChar.isLetterOrDigit(c)): break
ll -= 1
# print 'limit=' , ll + 1
return ll + 1
class PatternTable(object):
"""
FSA with Elly patterns to determine syntax types
attributes:
indx - for FSA states and their links
_errcount - running input error count
"""
def __init__ ( self , syms=None , fsa=None ):
"""
initialize
arguments:
self -
syms - symbol table for syntax categories and features
fsa - EllyDefinitionReader for FSA patterns plus syntax specifications
exceptions:
TableFailure on error
"""
self.indx = [ ] # start empty
self._errcount = 0 # no errors yet
if fsa != None and syms != None:
self.load(syms,fsa)
def _err ( self , s='malformed FSA transition' , l='' ):
"""
for error handling
arguments:
self -
s - error message
l - problem line
"""
self._errcount += 1
print >> sys.stderr , '** pattern error:' , s
if l != '':
print >> sys.stderr , '* at [' , l , ']'
def load ( self , syms , fsa ):
"""
convert text input to get FSA with encoded syntax
arguments:
self -
syms - symbol table for syntax categories and features
fsa - FSA link input from Elly definition reader
exceptions:
TableFailure on error
"""
# for error checking
ins = [ 0 ] # states with incoming links
sss = [ ] # starting states
lss = [ 0 ] # all defined states
nm = 0 # states producing matches
# print 'FSA definition line count=' , fsa.linecount()
while True: # read all input from ellyDefinitionReader
line = fsa.readline()
# print 'input line=[' , line , '], len=' , len(line)
if len(line) == 0: break # EOF check
ls = line.strip().split(' ') # get link definition as list
# print 'ls=' , ls
sts = ls.pop(0) # starting state for FSA link
try:
stn = int(sts) # numerical starting state
except ValueError:
self._err('bad start state',line)
continue
n = len(self.indx)
while stn >= n: # make sure state index has enough slots
self.indx.append([ ])
n += 1
try:
lk = Link(syms,ls) # allocate new link
except ellyException.FormatFailure:
self._err('bad FSA option',line)
continue
# print 'load lk=' , lk
if lk.nxts < 0: # final state?
if lk.catg != None:
nm += 1 # count link category for match
else:
self._err('missing category for final state',line)
continue
elif lk.catg != None:
self._err('unexpected category for non-final state' , line)
if not stn in lss: lss.append(stn)
if not stn in sss: sss.append(stn)
if lk.nxts >= 0: # -1 is stop, not state
if not lk.nxts in lss: lss.append(lk.nxts)
if not lk.nxts in ins: ins.append(lk.nxts)
elif lk.nxts < -2:
self._err('bad final state' , line)
continue
self.indx[stn].append(lk) # add to its slot in FSA state index
# print '=' , self.indx[stn]
if len(self.indx) == 0 and self._errcount == 0:
return # in case of empty definition file
if nm == 0: # FSA must have at least one match state
self._err('no match states')
# print 'ins=' , ins
# print 'lss=' , lss
ns = len(lss) # total number of states
if len(ins) != ns:
self._err('some states unreachable')
if len(sss) != ns:
self._err('some non-stop states are dead ends')
if self._errcount > 0:
print >> sys.stderr , '**' , self._errcount , 'pattern errors in all'
print >> sys.stderr , 'pattern table definition FAILed'
raise ellyException.TableFailure
def match ( self , segm , tree ):
"""
compare text segment against all FSA patterns from state 0
arguments:
self -
segm - segment to match against
tree - parse tree in which to put leaf nodes for final matches
returns:
text length matched by FSA
"""
# print 'comparing' , segm
if len(self.indx) == 0: return 0 # no matches if FSA is empty
if len(segm) == 0: return 0 # string is empty
lim = bound(segm) # get text limit for matching
mtl = 0 # accumulated match length
mtls = 0 # saved final match length
state = 0 # set to mandatory initial state for FSA
stk = [ ] # for tracking possible multiple matches
ls = self.indx[state] # for state 0!
ix = 0 # index into current possible transitions
sg = segm[:lim] # text subsegment for matching
# print 'initial sg=' , sg
# print len(ls) , 'transitions from state 0'
capd = False if len(sg) == 0 else ellyChar.isUpperCaseLetter(sg[0])
while True: # run FSA to find all possible matches
# print 'state=' , state
# print 'count=' , mtl , 'matched so far'
# print 'links=' , len(ls) , 'ix=' , ix
nls = len(ls) # how many links from current state
if ix == nls: # if none, then must back up
if len(stk) == 0: break
r = stk.pop() # restore match status
# print 'pop r= [' , r[0] , r[1][0].shortcode() , ']'
state = r[0] # FSA state
ls = r[1] # remaining links to check
sg = r[2] # input string
mtl = r[3] # total match length
ix = 0
# print 'pop sg=' , sg
continue
# print 'substring to match, sg=' , sg , 'nls=' , nls
m = 0
while ix < nls:
lk = ls[ix] # get next link at current state
ix += 1 # and increment link index
# print '@' , state , 'lk= [' , unicode(lk), ']' , 'ix=' , ix
# print 'patn=' , lk.patn
po = lk.patn[0]
if po == u'\x00': # do state change without matching?
m = 0 # no match length
elif po != ellyWildcard.cEND:
# print 'po=' , po
bds = ellyWildcard.match(lk.patn,sg)
# print 'bds=' , bds
if bds == None: continue
m = bds[0] # get match length, ignore wildcard bindings
elif (len(sg) > 0 and
(ellyChar.isLetterOrDigit(sg[0]) or sg[0] == ellyChar.PRME)):
# print 'unmatched solitary $'
continue
else:
# print 'matched solitary $, state=' , state
m = 0
# print 'm=' , m
if lk.nxts < 0: # final state?
if lk.nxts == -2: m = 0 # last part of match not counted
# print 'state=' , state , unicode(lk)
# print 'flags=' , lk.synf , '/' , lk.semf
if tree.addLiteralPhraseWithSemantics(
lk.catg,lk.synf,lk.semf,lk.bias,cap=capd): # make phrase
ml = mtl + m
if mtls < ml: mtls = ml
# print 'success!' , 'mtls=' , mtls
tree.lastph.lens = mtls # save its length
# print 'match state=' , state , 'length=' , mtls
# print 'ix=' , ix , 'nls=' , nls
if ix < nls: # any links not yet checked?
r = [ state , ls[ix:] , sg , mtl ]
# print 'saved r= ' , state ,
# print [ x.shortcode() for x in ls[ix:] ]
stk.append(r) # if not, save info for later continuation
mtl += m # update match length
break # leave loop at this state, go to next state
else:
# print 'no matches'
continue # all patterns exhausted for state
ix = 0
sg = sg[m:] # move up in text input
state = lk.nxts # next state
if state < 0:
ls = [ ]
else:
ls = self.indx[state]
# print 'sg=' , sg
# print 'state=' , state
# print 'len(ls)=' , len(ls)
# print 'mtls=' , mtls
return mtls
def dump ( self ):
"""
show contents of pattern table
arguments:
self
"""
nn = 0 # nul pattern count
for k in range(len(self.indx)):
lks = self.indx[k]
if lks == None or lks == [ ]:
continue
print '[state ' + str(k) + ']'
for lk in lks:
print u' ' + unicode(lk)
if lk.patn == u'\x00': nn += 1
print nn , 'nul pattern(s)'
print ''
#
# unit test
#
if __name__ == '__main__':
import ellyConfiguration
import ellyDefinitionReader
import dumpEllyGrammar
import parseTest
import stat
import os
mode = os.fstat(0).st_mode # to check for redirection of stdin (=0)
interact = not ( stat.S_ISFIFO(mode) or stat.S_ISREG(mode) )
ctx = parseTest.Context() # dummy interpretive context for testing
tre = parseTest.Tree(ctx.syms) # dummy parse tree for testing
print ''
basn = ellyConfiguration.baseSource + '/'
filn = sys.argv[1] if len(sys.argv) > 1 else 'test' # which FSA definition to use
inp = ellyDefinitionReader.EllyDefinitionReader(basn + filn + '.p.elly')
print 'pattern test with' , '<' + filn + '>'
if inp.error != None:
print inp.error
sys.exit(1)
patn = None
try:
patn = PatternTable(ctx.syms,inp) # try to define FSA
except ellyException.TableFailure:
print 'no pattern table generated'
sys.exit(1)
print len(patn.indx) , 'distinct FSA state(s)'
print ''
dumpEllyGrammar.dumpCategories(ctx.syms)
print ''
patn.dump()
print 'enter tokens in context to recognize'
while True: # try FSA with test examples
if interact: sys.stdout.write('> ')
t = sys.stdin.readline().decode('utf8').strip()
if len(t) == 0: break
print 'text=' , '[' , t , ']'
nma = patn.match(list(t),tre)
if interact: sys.stdout.write('\n')
tre.showQueue()