-
Notifications
You must be signed in to change notification settings - Fork 2
/
interpretiveContext.py
executable file
·831 lines (643 loc) · 22.6 KB
/
interpretiveContext.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
#!/usr/bin/python
# PyElly - scripting tool for analyzing natural language
#
# interpretiveContext.py : 24mar2018 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.
# -----------------------------------------------------------------------------
"""
data structures for communicating between generative procedures and collecting
their output into a single stream
"""
import sys
import conceptualWeighting
deletion = '_d_' # name to save deletions under in local stack
class InterpretiveContext(object):
"""
for context object shared by all semantic procedures for a grammar,
both generative and cognitive
attributes:
lbstk - for tracking binding of local variables
locls - for storing string values of defined local variables
glbls - for storing string values of defined global variables
buffs - buffers currently defined
buffn - index of currently active buffer
tokns - token list by position
procs - for storing standalone procedures under name
wghtg - weighting of semantic concepts by current importance
syms - symbol table for grammar
cncp - saved concept
"""
def __init__ ( self , syms , procs , glbls , hiery ):
"""
initialization
arguments:
self -
syms - symbol table
procs - standalone procedure dictionary
glbls - global variables dictionary
hiery - conceptual hierarchy
"""
self.syms = syms
self.procs = procs
self.glbls = glbls
self.wghtg = conceptualWeighting.ConceptualWeighting(hiery)
self.cncp = None
self.reset()
def reset ( self ):
"""
start with empty buffers and global variables with default values
arguments:
self -
"""
self.lbstk = [ ] # stack for procedures
self.clearLocalStack() # for defining local variables later
self.clearBuffers() # for output buffers
self.tokns = [ ] # empty out token list for parse
def getProcedure ( self , name ):
"""
get code for named procedure
arguments:
self -
name - procedure name
returns:
GenerativeProcedure
"""
return None if not name in self.procs else self.procs[name]
def setProcedure ( self , name , proc ):
"""
set code for named procedure
arguments:
self -
name - procedure name
proc - generative semantic procedure
"""
self.procs[name] = proc
def getGlobalVariable ( self , name ):
"""
get string for specified global
arguments:
self -
name - of global
returns:
string if successful, None otherwise
"""
return '' if not name in self.glbls else self.glbls[name]
def setGlobalVariable ( self , name , s ):
"""
set string for specified global
arguments:
self -
name - of global
s - new value to be assigned
"""
self.glbls[name] = s
def defineLocalVariable ( self , var , val='' ):
"""
define new local variable in current stack frame
arguments:
self -
var - variable name
val - option initial value
"""
# print 'local stack' , self.lbstk
ls = self.lbstk[-1] # get locals defined already for procedure
if not var in ls: # already in list?
ls.append(var) # can define only once per procedure
if not var in self.locls: # already defined elsewhere?
self.locls[var] = [ ] # if not, need to initialize dictionary
self.locls[var].append(val) # set to initial value
def getLocalVariable ( self , var ):
"""
get current value of a given local variable
arguments:
self -
var - variable name
"""
if not var in self.locls: return '' # if not locally defined anywhere, return ''
h = self.locls[var] # list of bindings for variable
return '' if len(h) == 0 else h[-1] # if not empty, return last binding
def setLocalVariable ( self , var , val='' ):
"""
set given local variable to value
arguments:
self -
var - variable name
val - value
"""
# print 'var=' , var
# print self.locls
if not var in self.locls: # locally defined anywhere?
self.defineLocalVariable(var) # if not, define locally for current procedure
self.locls[var][-1] = val # reset last binding to new value
def getDeletion ( self ):
"""
get sequence last deleted
arguments:
self
returns:
string of last deleted chars
"""
h = self.locls[deletion] # find deletion saver
return '' if len(h) == 0 else h[-1] # get last deletion if there was one
def putDeletion ( self , ds ):
"""
save last deleted sequence
arguments:
self -
ds - deletion sequence
"""
h = self.locls[ deletion ] # find deletion saver
h[-1] = ''.join(ds) # save last deletion as string
def clearLocalStack ( self ):
"""
empty out local stack
arguments:
self
"""
self.locls = { }
self.locls[ deletion ] = [ [ ] ] # for saving deletions
def getCurrentBuffer ( self ):
"""
index of current buffer
arguments:
self
"""
return self.buffn
def splitBuffer ( self ):
"""
create new output buffer and move to it
arguments:
self
"""
self.buffn += 1
self.buffs.insert(self.buffn,[ ])
def backBuffer ( self ):
"""
return to preceding buffer
arguments:
self
"""
if self.buffn > 0:
self.buffn -= 1
def mergeBuffer ( self ):
"""
merge current buffer with next buffer
arguments:
self
"""
bnext = self.buffn + 1
if bnext < len(self.buffs):
b = self.buffs.pop(bnext)
self.buffs[self.buffn].extend(b)
def mergeBuffers ( self , bn=0 ):
"""
merge merge buffers, all by default
arguments:
self
bn - buffer index for merged results
"""
if bn < 0 or bn >= len(self.buffs): return
bnext = bn + 1
while bnext < len(self.buffs):
b = self.buffs.pop(bnext)
self.buffs[bn].extend(b)
self.buffn = bn
def clearBuffers ( self ):
"""
revert to single empty buffer
arguments:
self
"""
self.buffn = 0
self.buffs = [ [ ] ]
def findCharsInBuffer ( self , ts , sns=True ):
"""
find string ts in current or next buffer and transfer chars to other
buffer up to and including first occurrence of ts or until entire
source buffer is exhausted
arguments:
self -
ts - string to look for
sns - direction of search:i
True = new buffer to current
False= current buffer to new
"""
n = self.buffn + 1
if n >= len(self.buffs): return # new buffer must exist!
t = list(ts) # list of chars to look for
ln = len(t) # length of list
bn = self.buffs[n] # next buffer
bc = self.buffs[n-1] # current one
if sns: # forward scanning
while len(bn) >= ln:
if t == bn[:ln]:
bc += t
self.buffs[n] = bn[ln:]
return # match
bc.append(bn.pop(0))
bc.extend(bn)
self.buffs[n] = [ ]
else: # reverse scanning
tmp = [ ] # buffer scanned chars for reverse scan
while len(bc) >= ln:
if t == bc[-ln:]:
# print 'find < ln=' , ln , bc
self.buffs[n] = t + tmp + bn
self.buffs[n-1] = bc[:-ln]
return # match
tmp.insert(0,bc.pop())
self.buffs[n] = bc + tmp + bn
self.buffs[n-1] = [ ]
def mergeBufferWithReplacement ( self , ts , ss ):
"""
merge current and next buffers, replacing occurrences of ts with ss
arguments:
self -
ts - string to look for
ss - its replacement
"""
n = self.buffn + 1
if n >= len(self.buffs): return
bt = self.buffs.pop(n)
t = list(ts)
s = list(ss)
ln = len(t)
bc = self.buffs[self.buffn] # chars in current buffer
while len(bt) >= ln: # match is still possible?
if t == bt[:ln]: # compare chars
bt = bt[ln:] # on match, remove those chars
bc.extend(s) # and put replacement in current buffer
else:
bc.append(bt.pop(0)) # no match; move 1 char from next to current buffer
bc.extend(bt) # move remaining chars from next to current one
def deleteCharsFromBuffer ( self , n=1 ):
"""
delete up to n chars from next or current buffer
arguments:
self -
n - maximum char count to delete
- n < 0 means current buffer
- n > 0 next buffer
"""
if n == 0:
return
k = self.buffn
if n > 0:
k += 1
if k >= len(self.buffs): return
b = self.buffs[k] # buffer to delete from
l = len(b) # its current char count
if l <= abs(n): # enough chars to delete
self.buffs[k] = [ ] # if not, empty out buffer
self.putDeletion(b)
elif n > 0: # delete from next buffer?
self.buffs[k] = b[n:] # remove n chars from front
self.putDeletion(b[:n])
else:
self.buffs[k] = b[:n] # remove n chars from back of current buffer
self.putDeletion(b[n:])
def deleteCharsInBufferTo ( self , s ):
"""
delete chars in next buffer up to first occurrence of specified sequence
arguments:
self -
s - specified sequence as string
"""
# print ' deleting to' , s
k = self.buffn + 1
if k >= len(self.buffs): return
ls = list(s) # convert string to list for comparisons
l = len(ls) # char count
bs = self.buffs[k] # saved buffer
b = list(bs) # copy of buffer for scanning
while l <= len(b): # enough chars to match?
if ls == b[:l]: break # if so, check match
b.pop(0)
else:
b = [ ] # no match, empty out scan
l = 0 # take zero length match
nd = len(bs) - len(b) + l # must delete matched sequence also
# print ' delete' , nd , k
self.putDeletion(bs[:nd]) # save deletion
self.buffs[k] = bs[nd:] # shorten next buffer
def deleteCharsInBufferFrom ( self , s ):
"""
delete chars in current buffer from last occurrence of specified sequence
arguments:
self -
s - specified sequence as string
"""
# print ' deleting from' , s
k = self.buffn
ls = list(s) # convert string to list for comparisons
l = len(ls) # char count
bs = self.buffs[k] # saved buffer
b = list(bs) # copy of buffer for scanning
while l <= len(b): # enough chars to match?
if ls == b[-l:]: break # if so, check match
b.pop()
else:
b = [ ] # no match, empty out scan
l = 0 # take zero length match
nd = len(bs) - len(b) + l # must delete matched sequence also
# print ' delete' , nd , k
self.putDeletion(bs[-nd:]) # save deletion
self.buffs[k] = bs[:-nd] # shorten current buffer
def prependCharsInCurrentBuffer ( self , s ):
"""
put specified chars at start of current buffer
arguments:
self -
s - chars in string
"""
self.buffn -= 1 # make current buffer into next
self.insertCharsIntoBuffer(s,1) # insert into next
self.buffn += 1 # next becomes current again
def checkBufferForChars ( self , s ):
"""
check start of next buffer against string
arguments:
self -
s - comparison string
returns:
True on match, False otherwise
"""
if self.buffn + 1 == len(self.buffs): return False
l = len(s)
b = self.buffs[self.buffn]
return False if l > len(b) or s != b[:l] else True
def insertCharsIntoBuffer ( self , s , m=0 ):
"""
insert specified chars in current or next buffer
arguments:
self -
s - chars in string
m - mode
- m == 0 means into current buffer
- m == 1 next buffer
"""
# print 'in m=' , m
# print 'in buf=' , self.buffn
if m == 0:
b = self.buffs[self.buffn]
b.extend(list(s))
else:
k = self.buffn + 1
# print 'in k=' , k
if k >= len(self.buffs):
return
b = self.buffs[k]
self.buffs[k] = list(s) + b
def extractCharsFromBuffer ( self , n=1 , m=0 ):
"""
get chars from start of next or current buffer
arguments:
self -
n - character count
m - mode
- m == 0 means from current buffer
- m == 1 next buffer
returns:
string of chars if successful, '' otherwise
"""
# print 'ex no of buf=' , len(self.buffs)
# print 'ex at buf=', self.buffn , len(self.buffs[self.buffn]) , len(self.buffs[self.buffn+1])
# print 'ex m=' , m
# print 'ex buf=' , (self.buffn + 1)
if n <= 0:
return ''
elif m == 1:
k = self.buffn + 1
if k == len(self.buffs): return ''
b = self.buffs[k]
l = len(b)
if n > l: return ''
s = b[:n]
self.buffs[k] = b[n:]
else:
b = self.buffs[self.buffn]
l = len(b)
if n > l: return ''
s = b[-n:]
self.buffs[self.buffn] = b[:-n]
return u''.join(s)
def moveCharsBufferToBuffer ( self , n ):
"""
move up to n chars between adjacent buffers
arguments:
self -
n - maximum char count
- n > 0 means from next to current buffer
- n < 0 means current to next
"""
k = self.buffn + 1
if n == 0:
return
if n < 0:
n = -n
if k == len(self.buffs):
self.buffs.append([ ])
bs = self.buffs[k-1]
bd = self.buffs[k]
l = len(bs)
if n > l: n = l
self.buffs[k] = bs[-n:] + bd
self.buffs[k-1] = bs[:-n]
else:
if k == len(self.buffs):
return
bs = self.buffs[k]
bd = self.buffs[k-1]
l = len(bs)
if n > l: n = l
self.buffs[k-1].extend(bs[:n])
self.buffs[k] = bs[n:]
def peekIntoBuffer ( self , nxtb=True ):
"""
look at last char in current or first char in next buffer without disturbing it
arguments:
self -
nxtb - selects next buffer
returns:
peeked char if it exists, otherwise ''
"""
c = ''
if nxtb:
k = self.buffn + 1
if k < len(self.buffs):
b = self.buffs[k]
if len(b) > 0:
c = b[0]
else:
b = self.buffs[self.buffn]
if len(b) > 0:
c = b[-1]
return c
def getBufferContent ( self ):
"""
get content of all buffers as single char list
arguments:
self
returns:
list of chars from buffers
"""
self.mergeBuffers() # combine all current buffers into first one
# print 'merged=' , self.buffs[0]
return self.buffs[0] # list of combined chars
def getBufferContents ( self ):
"""
get list of the lists of chars from all active buffers
arguments:
self
returns:
list of lists of chars in each buffer
"""
b = [ ]
for bs in self.buffs:
b.append(bs) # append buffer content, but keep lists separate
return b
def viewBufferDivide ( self , n ):
"""
show last n chars of current buffer and
first n chars of next buffer
arguments:
self -
n - char count
returns:
list of two char lists
"""
k = self.buffn
buf = self.buffs[k]
nbc = len(buf)
if nbc == 0:
curs = [ ]
else:
curs = buf[-n:] if nbc > n else buf[-nbc:]
k += 1
if k == len(self.buffs):
nxts = [ ]
else:
buf = self.buffs[k]
nbc = len(buf)
if nbc == 0:
nxts = [ ]
else:
nxts = buf[:n] if nbc > n else buf[:nbc]
return [ curs , nxts ]
def addTokenToListing ( self , tok ):
"""
put token into list for current sentence being parsed
arguments:
self -
tok - token to save
"""
self.tokns.append(tok)
def getNthTokenInListing ( self , n ):
"""
get specified token in parsing
arguments:
self -
n - which token
returns:
token if successful, None otherwise
"""
return None if n >= len(self.tokns) else self.tokns[n]
def countTokensInListing ( self ):
"""
get length of token listing
arguments:
self -
returns:
count
"""
return len(self.tokns)
def interpretConcept ( self , cn ):
"""
get importance score for specified concept
arguments:
self -
cn - concept as name string
returns:
integer importance score
"""
return self.wghtg.interpretConcept(cn)
def relateConceptPair ( self , cna , cnb ):
"""
get relatedness of two specified concepts
arguments:
self -
cna - first concept as name string
cnb - second
returns:
integer relatedness score
"""
return self.wghtg.relateConceptPair(cna,cnb)
def pushStack ( self ):
"""
make stack frame for local variables of generative procedure
arguments:
self
"""
# print 'pushStack' , len(self.lbstk)
self.lbstk.append([ deletion ]) # to hold bindings for procedures
self.locls[ deletion ].append([ ]) # set deletion to empty char list
def popStack ( self ):
"""
free up stack for generative procedure
arguments:
self
"""
# print 'popStack' , len(self.lbstk)
r = self.lbstk.pop() # remove procedure bindings from binding stack
for v in r:
stk = self.locls[v]
stk.pop() # remove their values from local stack
if len(stk) == 0:
del self.locls[v] # if all values gone, remove empty list
def printStatus ( self , pnam='' ):
"""
show where current stack and buffer are
arguments:
self -
pnam - procedure name to report
"""
sys.stderr.write(' stk=' + str(len(self.lbstk)))
if pnam != '':
sys.stderr.write(' in (' + pnam + ')')
if self.buffs == None:
sys.stderr.write(' buf= None ERROR!\n')
else:
sys.stderr.write(' buf=' + str(len(self.buffs)))
sys.stderr.write(' (' + str(len(self.buffs[self.buffn])) + ' chars)\n')
sys.stderr.flush()
def echoTokensToOutput ( self ):
"""
copy all tokens to output buffer
arguments:
self
"""
toks = map((lambda x: 'NONE' if x == None else x.orig),self.tokns)
self.insertCharsIntoBuffer(u''.join(toks))