rules.py
10.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
'''
Created on 24 sty 2014
@author: mlenart
'''
import copy
from morfeuszbuilder.segrules.rulesNFA import RulesNFAState
from morfeuszbuilder.utils.exceptions import ConfigFileException
class SegmentRule(object):
def __init__(self, linenum):
self.weak = False
self.linenum = linenum
self.autogenerated = False
def setWeak(self, weak):
self.weak = weak
return self
def addToNFA(self, fsa):
raise NotImplementedError()
def allowsEmptySequence(self):
raise NotImplementedError()
def _doAddToNFA(self, startStates, endState):
raise NotImplementedError()
def transformToGeneratorVersion(self):
raise NotImplementedError()
def isSinkRule(self):
return False
def isShiftOrthRule(self):
raise NotImplementedError()
def getAtomicRules(self):
raise NotImplementedError()
def getAdditionalAtomicRules4Generator(self):
raise NotImplementedError()
def makeShiftOrthRule(self):
pass
def __repr__(self):
return str(self)
def validate(self, filename):
pass
class TagRule(SegmentRule):
def __init__(self, segnum, shiftOrth, segtype, linenum, weak=False):
self.segnum = segnum
self.segtype = segtype
self.shiftOrth = shiftOrth
self.linenum = linenum
self.weak = weak
self.autogenerated = False
def addToNFA(self, fsa):
endState = RulesNFAState(self, final=True, weak=self.weak, autogenerated=self.autogenerated)
self._doAddToNFA(fsa.initialState, endState)
def _doAddToNFA(self, startState, endState):
startState.addTransition((self.segnum, self.shiftOrth), endState)
def allowsEmptySequence(self):
return False
def __str__(self):
res = self.segtype
# res += '(' + str(self.segnum) + ')'
if self.shiftOrth:
res += '>'
return res
# return u'%s(%d)' % (self.segtype, self.segnum)
def transformToGeneratorVersion(self):
return copy.deepcopy(self)
def isShiftOrthRule(self):
return self.shiftOrth
def makeShiftOrthRule(self):
self.shiftOrth = True
def getAtomicRules(self):
yield self
def getAdditionalAtomicRules4Generator(self):
res = [ copy.deepcopy(self) ]
res[0].autogenerated = True
return res
class UnaryRule(SegmentRule):
def __init__(self, child, linenum):
super(UnaryRule, self).__init__(linenum)
self.child = child
assert not child.isSinkRule()
def isShiftOrthRule(self):
return self.child.isShiftOrthRule()
def makeShiftOrthRule(self):
self.child.makeShiftOrthRule()
def getAtomicRules(self):
for leaf in self.child.getAtomicRules():
yield leaf
def getAdditionalAtomicRules4Generator(self):
return self.child.getAdditionalAtomicRules4Generator()
def validate(self, filename):
self.child.validate(filename)
class ComplexRule(SegmentRule):
def __init__(self, children, linenum):
super(ComplexRule, self).__init__(linenum)
self.children = children
assert not any([c.isSinkRule() for c in children])
def addToNFA(self, fsa):
endState = RulesNFAState(self, final=True, weak=self.weak, autogenerated=self.autogenerated)
self._doAddToNFA(fsa.initialState, endState)
def getAtomicRules(self):
for child in self.children:
for leaf in child.getAtomicRules():
yield leaf
def makeShiftOrthRule(self):
for child in self.children:
child.makeShiftOrthRule()
class ConcatRule(ComplexRule):
def __init__(self, children, linenum):
super(ConcatRule, self).__init__(children, linenum)
assert type(children) == list
def _doAddToNFA(self, startState, endState):
currStartState = startState
for child in self.children[:-1]:
currEndState = RulesNFAState(self)
child._doAddToNFA(currStartState, currEndState)
nextStartState = RulesNFAState(self)
currEndState.addTransition(None, nextStartState)
currStartState = nextStartState
lastChild = self.children[-1]
lastChild._doAddToNFA(currStartState, endState)
def allowsEmptySequence(self):
return all([rule.allowsEmptySequence() for rule in self.children])
def __str__(self):
return ' '.join([str(c) for c in self.children])
def isShiftOrthRule(self):
return all([c.isShiftOrthRule() for c in self.children])
def transformToGeneratorVersion(self):
newChildren = [child.transformToGeneratorVersion() for child in self.children if not child.allowsEmptySequence() or child.isShiftOrthRule()]
if newChildren == []:
return SinkRule()
hasNonOptionalNonShiftingRule = False
for child in newChildren:
# print 'child=', child
if child.isSinkRule() or hasNonOptionalNonShiftingRule:
return SinkRule()
elif not child.isShiftOrthRule():
hasNonOptionalNonShiftingRule = True
# print 'got nonshifting'
res = ConcatRule(newChildren, self.linenum)
res.setWeak(self.weak)
return res
def getAdditionalAtomicRules4Generator(self):
res = []
currShiftOrthRule = None
for rule in list(self.children):
if rule.isShiftOrthRule():
if currShiftOrthRule:
currShiftOrthRule = ConcatRule([currShiftOrthRule, rule], rule.linenum)
else:
currShiftOrthRule = rule
else:
for atomicRule in rule.getAdditionalAtomicRules4Generator():
if currShiftOrthRule:
res.append(ConcatRule([currShiftOrthRule, atomicRule], atomicRule.linenum))
else:
res.append(atomicRule)
currShiftOrthRule = None
for rule in res:
rule.autogenerated = True
return res
def validate(self, filename):
for rule in self.children:
rule.validate(filename)
if self.children[-1].isShiftOrthRule() \
and not all([c.isShiftOrthRule() for c in self.children]):
raise ConfigFileException(
filename,
self.linenum,
'If the rightmost subrule of concatenation "%s" is with ">", than all subrules must be with ">"' % str(self))
class OrRule(ComplexRule):
def __init__(self, children, linenum):
super(OrRule, self).__init__(children, linenum)
def _doAddToNFA(self, startState, endState):
for child in self.children:
intermStartState = RulesNFAState(self)
intermEndState = RulesNFAState(self)
startState.addTransition(None, intermStartState)
child._doAddToNFA(intermStartState, intermEndState)
intermEndState.addTransition(None, endState)
def allowsEmptySequence(self):
return any([rule.allowsEmptySequence() for rule in self.children])
def __str__(self):
return ' | '.join([str(c) for c in self.children])
def isShiftOrthRule(self):
return all([c.isShiftOrthRule() for c in self.children])
def transformToGeneratorVersion(self):
newChildren = [child.transformToGeneratorVersion() for child in self.children if not child.allowsEmptySequence() or child.isShiftOrthRule()]
newChildren = [c for c in newChildren if not c.isSinkRule()]
if newChildren == []:
return SinkRule()
else:
res = OrRule(newChildren, self.linenum)
res.setWeak(self.weak)
return res
def getAdditionalAtomicRules4Generator(self):
res = []
for rule in self.children:
res.extend(rule.getAdditionalAtomicRules4Generator())
return res
def validate(self, filename):
for rule in self.children:
rule.validate(filename)
if not (
all([c.isShiftOrthRule() for c in self.children])
or not any([c.isShiftOrthRule() for c in self.children])):
raise ConfigFileException(
filename,
self.linenum,
'All subrules of alternative "%s" must be either with or without ">"' % str(self))
class ZeroOrMoreRule(UnaryRule):
def __init__(self, child, linenum):
super(ZeroOrMoreRule, self).__init__(child, linenum)
assert isinstance(child, SegmentRule)
def addToNFA(self, fsa):
raise ValueError()
def _doAddToNFA(self, startState, endState):
intermStartState = RulesNFAState(self)
intermEndState = RulesNFAState(self)
startState.addTransition(None, intermStartState)
startState.addTransition(None, endState)
self.child._doAddToNFA(intermStartState, intermEndState)
intermEndState.addTransition(None, endState)
intermEndState.addTransition(None, intermStartState)
def allowsEmptySequence(self):
return True
def transformToGeneratorVersion(self):
if self.isShiftOrthRule():
return copy.deepcopy(self)
else:
return SinkRule()
def __str__(self):
return '(' + str(self.child) + ')*'
class OptionalRule(UnaryRule):
def __init__(self, child, linenum):
super(OptionalRule, self).__init__(child, linenum)
assert isinstance(child, SegmentRule)
def addToNFA(self, fsa):
raise ValueError()
def _doAddToNFA(self, startState, endState):
intermStartState = RulesNFAState(self)
intermEndState = RulesNFAState(self)
startState.addTransition(None, intermStartState)
startState.addTransition(None, endState)
self.child._doAddToNFA(intermStartState, intermEndState)
intermEndState.addTransition(None, endState)
def allowsEmptySequence(self):
return True
def transformToGeneratorVersion(self):
if self.isShiftOrthRule():
return copy.deepcopy(self)
else:
return self.child.transformToGeneratorVersion()
def __str__(self):
return '(' + str(self.child) + ')?'
class SinkRule(SegmentRule):
def __init__(self):
super(SinkRule, self).__init__(None)
def addToNFA(self, fsa):
return
def allowsEmptySequence(self):
return False
def _doAddToNFA(self, startStates, endState):
return
def transformToGeneratorVersion(self):
return self
def isSinkRule(self):
return True
def __str__(self):
return '<<REMOVED>>'
def getAdditionalAtomicRules4Generator(self):
return []