-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathredone_expr.py
465 lines (346 loc) · 11.6 KB
/
redone_expr.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
# -*- coding: utf-8 -*-
import re
import logging
import lex as lex
import yacc as yacc
class ExpressionError(ValueError):
pass
class BaseNode():
node_id = ''
level = 0
negs_above = False
neg = False
deprel = False
parent_node = None
extra_comments = []
#def __init__(self):
# self.extra_comments = []
class SetNode_Token(BaseNode):
#Makes a single operation, and returns a single set
def __init__(self, token_restriction):
self.node_id = ''
self.extra_comments = []
self.token_restriction = token_restriction.rstrip('"').lstrip('"')
if self.token_restriction.startswith('L=') and not token_restriction.startswith('"'):
self.token_restriction = self.token_restriction[2:]
self.proplabel = '@CGBASE'
else:
self.proplabel = ''
def set_proplabel(self, label):
if self.proplabel == '':
self.proplabel = label
def get_kid_nodes(self):
return []
def to_unicode(self):
return u'SetNode(Token:' + self.proplabel + self.token_restriction + u')' + str(self.extra_comments)
class SetNode_Eq(BaseNode):
def __init__(self, setnode1, setnode2):
self.node_id = ''
self.setnode1 = setnode1
self.setnode2 = setnode2
self.proplabel = ''
self.extra_comments = []
def get_kid_nodes(self):
return [self.setnode1, self.setnode2]
def to_unicode(self):
return u'Node(' + self.setnode1.to_unicode() + ' == ' + self.setnode2.to_unicode() + ')'
class SetNode_SubEq(BaseNode):
def __init__(self, setnode1, setnode2):
self.node_id = ''
self.setnode1 = setnode1
self.setnode2 = setnode2
self.proplabel = ''
self.extra_comments = []
def get_kid_nodes(self):
return [self.setnode1, self.setnode2]
def to_unicode(self):
return u'Node(' + self.setnode1.to_unicode() + ' -> ' + self.setnode2.to_unicode() + ')'
class SetNode_And(BaseNode):
def __init__(self, setnode1, setnode2):
self.node_id = ''
self.setnode1 = setnode1
self.setnode2 = setnode2
self.proplabel = ''
self.extra_comments = []
def get_kid_nodes(self):
return [self.setnode1, self.setnode2]
def to_unicode(self):
return u'Node(' + self.setnode1.to_unicode() + ' AND ' + self.setnode2.to_unicode() + ')'
class SetNode_Plus(BaseNode):
def __init__(self, setnode1, setnode2):
self.node_id = ''
self.setnode1 = setnode1
self.setnode2 = setnode2
self.proplabel = ''
self.extra_comments = []
def get_kid_nodes(self):
return [self.setnode1, self.setnode2]
def to_unicode(self):
return u'Node(' + self.setnode1.to_unicode() + ' PLUS ' + self.setnode2.to_unicode() + ')'
class SetNode_Or(BaseNode):
def __init__(self, setnode1, setnode2):
self.node_id = ''
self.setnode1 = setnode1
self.setnode2 = setnode2
self.proplabel = ''
self.extra_comments = []
def get_kid_nodes(self):
return [self.setnode1, self.setnode2]
def to_unicode(self):
return u'Node(' + self.setnode1.to_unicode() + ' OR ' + self.setnode2.to_unicode() + ')'
class SetNode_Dep(BaseNode):
def __init__(self, setnode1, setnode2, deprel):
self.node_id = ''
self.index_node = setnode1
self.deprels = [(deprel, setnode2)]
self.proplabel = ''
self.extra_comments = []
def get_kid_nodes(self):
to_return = [self.index_node,]
for dr_tuple in self.deprels:
to_return.append(dr_tuple[0])
to_return.append(dr_tuple[1])
return to_return
def to_unicode(self):
deprel_list = []
for drt in self.deprels:
deprel_list.append(drt[0].to_unicode() + ':' + drt[1].to_unicode())
return u'Node(index_node:' + self.index_node.to_unicode() + ' << ' + ','.join(deprel_list) +' >> )' + str(self.extra_comments)
class SetNode_Not(BaseNode):
def __init__(self, setnode1):
self.node_id = ''
self.setnode1 = setnode1
self.neg = True
self.extra_comments = []
def get_kid_nodes(self):
return [self.setnode1,]
def to_unicode(self):
return u'Node(NOT ' + self.setnode1.to_unicode() +')'
class DeprelNode(BaseNode):
#Makes a single operation, and returns a single set
def __init__(self, dep_restriction):
self.node_id = ''
self.dep_restriction = dep_restriction
self.deprel = True
def get_kid_nodes(self):
return []
def to_unicode(self):
return u'DeprelNode(' + self.dep_restriction + u')'
class DeprelNode_And(BaseNode):
def __init__(self, dnode1, dnode2):
self.node_id = ''
self.dnode1 = dnode1
self.dnode2 = dnode2
self.deprel = True
def get_kid_nodes(self):
return [self.dnode1, self.dnode2]
def to_unicode(self):
return u'DeprelNode(' + self.dnode1.to_unicode() + ' AND ' + self.dnode2.to_unicode() + ')'
class DeprelNode_Or(BaseNode):
def __init__(self, dnode1, dnode2):
self.node_id = ''
self.dnode1 = dnode1
self.dnode2 = dnode2
self.deprel = True
def get_kid_nodes(self):
return [self.dnode1, self.dnode2]
def to_unicode(self):
return u'DeprelNode(' + self.dnode1.to_unicode() + ' OR ' + self.dnode2.to_unicode() + ')'
class DeprelNode_Not(BaseNode):
def __init__(self, dnode1):
self.node_id = ''
self.dnode1 = dnode1
self.neg = True
self.deprel = True
def get_kid_nodes(self):
return [self.dnode1]
def to_unicode(self):
return u'DeprelNode(NOT ' + self.dnode1.to_unicode() +')'
#########################
### The expression parser bit starts here
#Lexer - tokens
tokens=('TEXT', #/..../
'WORD', #@...
'DEPOP', #</nsubj/
'LPAR', #(
'RPAR', #)
'NEG', #!
'AND', #&
'OR', #|
'PLUS',
'EQ',
'SE',
'ANYTOKEN',
'ECOM',
'XDOT',
'BGN',
'END') #_
#Here's regular expressions for the tokens defined above
t_TEXT = ur'((?!(->|\+|&|\(|\)|\||==|<|>|"|\s)).)+'
t_DEPOP = ur'(<|>)([^="<>()&|\s]+)?'
def t_ECOM(t):
ur'{[^}]+}'
return t
def t_WORD(t):
ur'"[^"]+"'
return t
def t_LPAR(t):
ur"\("
return t
def t_RPAR(t):
ur"\)"
return t
def t_NEG(t):
ur"\!"
return t
def t_AND(t):
ur"\&"
return t
def t_OR(t):
ur"\|"
return t
def t_EQ(t):
ur"=="
return t
def t_SE(t):
ur"->"
return t
def t_PLUS(t):
ur"\+"
return t
def t_XDOT(t):
ur"[.]{1}"
return t
def t_BGN(t):
ur"\^"
return t
def t_END(t):
ur"\$"
return t
def t_ANYTOKEN(t):
ur"_"
return t
t_ignore=u" \t"
def t_error(t):
raise ExpressionError(u"%s:::Unexpected character '%s'\nHERE: '%s'..."%(t, t.value[0],t.value[:5]))
#......and here's where starts the CFG describing the expressions
# the grammar rules are the comment strings of the functions
#Main
precedence = (('left','PLUS'),('left','EQ'),('left','SE'),('left','DEPOP'),('left','OR'),('left','AND'),('left','NEG'), ('left', 'ECOM'), ('left','TEXT'), ('right', 'XDOT'), ('left', 'BGN'), ('left', 'END'))
def p_error(t):
if t==None:
raise ExpressionError(u"Syntax error at the end of the expression. Perhaps you forgot to specify a target token? Forgot to close parentheses?")
else:
raise ExpressionError(u"Syntax error at the token '%s'\nHERE: '%s'..."%(t.value,t.lexer.lexdata[t.lexpos:t.lexpos+5]))
#TOP search -> expression
def p_top(t):
u'''search : setnode'''
t[0]=t[1] #t[0] is the result (left-hand side) and t[1] is the result of "expr"
# expression can be:
# ( expression )
# _
def p_bgn(t):
u'''setnode : BGN setnode'''
t[0] = SetNode_Dep(t[2], SetNode_Token('_'), DeprelNode_Not(DeprelNode('<lin@L')))
def p_end(t):
u'''setnode : setnode END'''
t[0] = SetNode_Dep(t[1], SetNode_Token('_'), DeprelNode_Not(DeprelNode('<lin@R')))
def p_dot(t):
u'''setnode : tokendef XDOT setnode'''
t[0] = SetNode_Dep(t[1], t[3], DeprelNode('<lin@R'))
def p_ecomxpr(t):
u'''setnode : setnode ECOM'''
t[1].extra_comments.append(t[2][1:-1])
t[0] = t[1]
def p_expr2(t):
u'''setnode : setnode depres'''
if isinstance(t[1], SetNode_Dep):
t[1].deprels.append(t[2])
t[0] = t[1]
else:
t[0] = SetNode_Dep(t[1], t[2][1], t[2][0])
def p_sn_depres_a(t):
u'''depres : depnode tokendef'''
t[0] = (t[1], t[2])
def p_exprp(t):
u'''tokendef : LPAR setnode RPAR'''
# | ANYTOKEN'''
if len(t)==4: #the upper rule
t[0]=t[2] #...just return the embedded expression
#elif len(t)==2:
# t[0]=SetNode_Token(t[1])
def p_exprp_d(t):
u'''depdef : LPAR depnode RPAR
| DEPOP'''
if len(t)==4: #the upper rule
t[0]=t[2] #...just return the embedded expression
elif len(t)==2:
t[0]=DeprelNode(t[1])
def p_exprp2(t):
u'''setnode : tokendef'''
#if isinstance(BaseNode):
t[0]=t[1] #if tokendef returns a Node(), this will also return a Node()
#else:
# p_exprp3(t)
def p_exprp5(t):
u'''depnode : depdef'''
t[0]=t[1] #if tokendef returns a Node(), this will also return a Node()
def p_exprp3(t):
u'''tokendef : WORD
| TEXT
| ANYTOKEN'''
if len(t) == 2:
if t[1].startswith('"') and t[1].endswith('"'):
t[0] = SetNode_Token(t[1])
elif t[1]=='_':
t[0]=SetNode_Token(t[1])
else:
t[0] = SetNode_Token(t[1])
t[0].set_proplabel('@CGTAG')
def p_dn_and(t):
u'''depnode : depnode AND depnode'''
if not isinstance(t[1], DeprelNode_Not) and not isinstance(t[3], DeprelNode_Not):
t[0] = DeprelNode_And(t[1], t[3])
else:
raise ExpressionError(u"Negated depency restrictions are not allowed inside AND operators, maybe try to include negation outside the AND operator.")
def p_sn_and(t):
u'''tokendef : tokendef AND tokendef'''
t[0] = SetNode_And(t[1], t[3])
def p_sn_or(t):
u'''tokendef : tokendef OR tokendef'''
t[0] = SetNode_Or(t[1], t[3])
def p_sn_plus(t):
u'''setnode : setnode PLUS setnode'''
t[0] = SetNode_Plus(t[1], t[3])
def p_sn_eq(t):
u'''setnode : setnode EQ setnode'''
t[0] = SetNode_Eq(t[1], t[3])
def p_sn_seq(t):
u'''setnode : setnode SE setnode'''
t[0] = SetNode_SubEq(t[1], t[3])
def p_dn_or(t):
u'''depnode : depnode OR depnode'''
if not isinstance(t[1], DeprelNode_Not) and not isinstance(t[3], DeprelNode_Not):
t[0] = DeprelNode_Or(t[1], t[3])
else:
raise ExpressionError(u"Negated depency restrictions are not allowed inside OR operators, maybe try to include negation outside the OR operator.")
def p_dn_not(t):
u'''depdef : NEG depdef'''
t[0] = DeprelNode_Not(t[2])
def p_sn_not(t):
u'''tokendef : NEG tokendef'''
t[0] = SetNode_Not(t[2])
lex.lex(reflags=re.UNICODE)
yacc.yacc(write_tables=0,debug=1,method='SLR')
if __name__=="__main__":
import argparse
parser = argparse.ArgumentParser(description='Expression parser')
parser.add_argument('expression', nargs='+', help='Training file name, or nothing for training on stdin')
args = parser.parse_args()
e_parser=yacc.yacc(write_tables=0,debug=1,method='LALR')
for expression in args.expression:
import logging
logging.basicConfig(filename='myapp.log', level=logging.INFO)
log = logging.getLogger()
ebin = e_parser.parse(expression.decode('utf8'), debug=0)
print ebin.to_unicode()