-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrule_engine.py
404 lines (325 loc) · 12.6 KB
/
rule_engine.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
from Plex import *
from debug import *
import re
import pprint
from semantic_rules import semantic_rules
from grammar_fsm import ND_FSM
### type l:tag(-|+) r:tag(-|+)
### - = left of
### + = right of
### RELATIONSHIPS:
### subject l:Wd-[n] r:Ss-[n+1];
### todo l:TO-[n] r:TO+[n];
### object l:O-[n] r:O+[n];
### imperative l:Wi-[n] r:Wi+[n];
### adj1 l:(A.*|DT.*)+[n] r:(A.*|DT.*)-[n];
### adj2 l:(Mp.*|MVp.*|Ma.*)-[n] r:(Mp.*|MVp.*)+[n];
### adv1 l:(EB.*|MVa.*)-[n] r:(EB.*|MVa.*)+[n] !(EBx.*)
### adv2 l:(E.*|EA.*)+[n] r:(E.*|EA.*)-[n] !(EA(m|y).*)
### adv3
def test_rules(sentence):
r = rule_engine()
r.parse_text(sentence)
class rule_engine:
def __init__(self):
self.rules = []
self.feature_path = feature_path()
def semanticRules(self):
for k, v in semantic_rules.items():
if 'regex' in v:
for x in v['regex']:
yield k, x
def setup_rules(self):
for k, x in self.semanticRules():
match_rules = {}
set_rules = {}
if x[:2] == '= ':
## Also "\." is replaced with "[a-z\*]"
current_rule = semantic_rules[k]
for m in current_rule['match']:
if not match_rules.has_key(k):
match_rules[k] = []
match_rules[k].append(self.feature_path.build_path(m))
for n in current_rule['set']:
if not set_rules.has_key(k):
set_rules[k] = []
set_rules[k].append(self.feature_path.build_path(n))
self.ndpda.add_transition(x[2:], match_rules, name=k, next_state=set_rules)
def parse_text(self, sentence):
if not sentence:
return
self.ndpda = NDPDA_FSM('INIT', sentence)
self.setup_rules()
processed = self.ndpda.process_list(sentence.tags)
#self.tokens.fsm_setup()
pprint.pprint(processed)
return processed
class feature_path:
def __init__(self, fsm=None):
if fsm:
self.fsm = fsm
literals = [
'%', ## signifies null
'str', ## is the literal for the current string
]
## a feature path is made up of
## left side, action, right side
def build_path(self, string):
idx, action = self.resolve_action(string)
left, right = self.words_before_and_after(string, idx)
## now match the <...>
left_m = self.match_to_path(left)
right_m = self.match_to_path(right)
return (left_m, action, right_m)
def match_to_path(self, to_match):
if '<' in to_match:
## it may not be the first character
L_idx = to_match.index('<')
if '>' in to_match:
R_idx = to_match.index('>')
substring = to_match[L_idx+1:R_idx]
return substring.split(' ')
else:
## it should really have a matching bracket
## but dump the whole thing anyway to play
## it safe
if to_match[0] == ' ':
return [to_match[1:]]
else:
return [to_match]
else:
## it doesnt like to pick up that first space
if to_match[0] == ' ':
return [to_match[1:]]
else:
return [to_match]
def dict_set(self, object, dictlist, value, action):
for x in dictlist:
if object.has_key(x):
pass
else:
if len(dictlist) > 1:
object[x] = {}
else:
if action == 'eq':
if value == ['%']:
try:
del object[x]
except KeyError:
pass
else:
object[x] = value
elif action == 'ap':
if isinstance(object[x], list):
object[x].append(value)
else:
object[x] = []
object[x].append(value)
dictlist.pop(0)
if isinstance(object, dict):
if not object.has_key(x):
object[x] = {}
self.dict_set(object[x], dictlist, value, action)
def _has(self, has, sentence):
if has in sentence:
has = sentence.index(has)
return (True, has)
else:
return (False, 0)
def resolve_words(self, s_tuple, memory):
LEFT_TAG = 'F_L'
RIGHT_TAG = 'F_R'
fsm_memory = self.fsm.memory
left_wall = self._has('LEFT-WALL', fsm_memory.words)
## start the indexer at 0 if we dont have the left wall
## or with 1 if we do, overloaded logic because of no
## ternary operator
i = (left_wall[0] != True and 0 or 1)
if self.fsm.counter == 0:
current_span = fsm_memory.spans[self.fsm.counter]
current_word = fsm_memory.words[self.fsm.counter]
else:
current_span = fsm_memory.spans[self.fsm.counter-i]
current_word = fsm_memory.words[self.fsm.counter-i]
word_set_len = len(fsm_memory.words)
if word_set_len > (self.fsm.counter + current_span):
right_word = fsm_memory.words[self.fsm.counter-i + current_span]
else:
## the right word is probably buried in a wall or
## actually i have no idea
right_word = fsm_memory.words[-1]
left_side = s_tuple[0]
right_side = s_tuple[2]
## handle left side
if LEFT_TAG in left_side:
idx = left_side.index(LEFT_TAG)
left_side[idx] = current_word
if RIGHT_TAG in left_side:
idx = left_side.index(RIGHT_TAG)
left_side[idx] = right_word
## handle the right side
if LEFT_TAG in right_side:
idx = right_side.index(LEFT_TAG)
right_side[idx] = current_word
if RIGHT_TAG in right_side:
idx = right_side.index(RIGHT_TAG)
right_side[idx] = right_word
def resolve_action(self, parsed):
for idx, x in enumerate(parsed):
if '!=' == x:
return (idx, 'ne')
if '+=' == x:
return (idx, 'ap')
if '=' == x:
return (idx, 'eq')
def words_before_and_after(self, sentence, idx):
before = sentence[:idx]
after = sentence[idx+1:]
return (before, after)
def match_action(self, s_tuple, memory):
self.resolve_words(s_tuple, memory)
if s_tuple[1] == 'eq':
## probing the dictionary wildly
## good idea to catch non-existant items
try:
entry = reduce(getattr, s_tuple[0], object)
debug(entry)
if entry == s_tuple[2]:
return True
else:
if s_tuple[2] == ['%']:
return True
else:
return False
except Exception, E:
if s_tuple[2] == ['%']:
return True
else:
return False
if s_tuple[1] == 'ne':
## probing the dictionary wildly
## good idea to catch non-existant items
try:
entry = reduce(getattr, s_tuple[0], object)
debug(entry)
if entry == s_tuple[2]:
return False
else:
if s_tuple[2] == ['%']:
return False
else:
return True
except Exception, E:
if s_tuple[2] == ['%']:
return True
else:
return False
def do_action(self, s_tuple, memory):
## memory has to be a dictionary
self.resolve_words(s_tuple, memory)
if s_tuple[1] == 'eq':
self.doActionEqual(s_tuple, memory)
if s_tuple[1] == 'ap':
self.doActionAppend(s_tuple, memory)
def doActionEqual(self, s_tuple, memory):
left = s_tuple[0]
right = s_tuple[2]
self.dict_set(memory, left, right, 'eq')
#debug(memory)
def doActionAppend(self, s_tuple, memory):
left = s_tuple[0]
right = s_tuple[2]
self.dict_set(memory, left, right, 'ap')
#debug(memory)
class NDPDA_FSM:
def __init__(self, initial_state, memory=[]):
self.feature_path = feature_path(fsm=self)
self.state_transitions = {}
self.input_symbol = None
self.initial_state = initial_state
self.current_state = self.initial_state
self.next_state = None
self.action = None
self.memory = memory
self.output = []
self.registers = {}
self.counter = 0
self.words = {}
self.frame_memory = {}
self.frames = []
def reset (self):
self.current_state = self.initial_state
self.input_symbol = None
def match_register_state(self, in_state):
if len(in_state.items()) < 1:
return True
head = in_state.keys()[0]
state = in_state[head]
output = None
match_value = None
for cur_state in state:
if 'str' in cur_state[0]:
self.feature_path.resolve_words(cur_state, self.words)
words_to_match = cur_state[2][0].split('|')
debug(words_to_match)
if cur_state[0] in words_to_match:
return True
match = self.feature_path.match_action(cur_state, self.words)
if not match_value:
match_value = match
else:
if match_value:
match_value = match
return match_value
def set_register_state(self, in_state):
if len(in_state.items()) < 1:
return
head = in_state.keys()[0]
state = in_state[head]
#debug(state)
for cur_state in state:
self.feature_path.do_action(cur_state, self.words)
def add_transition(self, input_symbol, state, next_state=None, name=None, action=None):
if next_state is None:
next_state = state
self.state_transitions[input_symbol] = (state, action, next_state, name)
def get_transition(self, input_symbol):
for regex_transitions in self.state_transitions:
regex = regex_transitions
regex.replace('\.', '[a-z]')
if regex[0] == ' ':
regex = regex[1:]
if regex[0] != '(':
to_compile = '(%s)' % regex
else:
to_compile = regex
re_to_match = re.compile(to_compile)
re_search = re_to_match.match(input_symbol)
if re_search:
yield self.state_transitions[regex_transitions]
def process(self, input_symbol):
output = None
self.input_symbol = input_symbol
for transitions in self.get_transition(self.input_symbol):
self.state, self.action, self.next_state, self.name = transitions
if self.match_register_state(self.state):
self.set_register_state(self.next_state)
break
#output = {self.name:{'set_state':self.next_state}}
self.frames.append((self.counter, self.frame_memory))
self.frame_memory = {}
self.current_state = self.next_state
self.next_state = None
self.counter += 1
if output:
return output
def process_list (self, input_symbols):
debug(input_symbols)
output = []
current_item = 0
for s in input_symbols:
#debug(s)
runner = self.process(s)
output.append((current_item, runner))
current_item += 1
return self.words