rules.py
3.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
'''
Created on 24 sty 2014
@author: mlenart
'''
from morfeuszbuilder.segrules.rulesNFA import RulesNFAState
class SegmentRule(object):
'''
classdocs
'''
def __init__(self):
'''
Constructor
'''
def addToNFA(self, fsa):
raise NotImplementedError()
def _doAddToNFA(self, startStates, endState):
raise NotImplementedError()
class TagRule(SegmentRule):
def __init__(self, segnum, segtype):
self.segnum = segnum
self.segtype = segtype
def addToNFA(self, fsa):
endState = RulesNFAState(final=True)
self._doAddToNFA(fsa.initialState, endState)
def _doAddToNFA(self, startState, endState):
startState.addTransition(self.segnum, endState)
def __str__(self):
return u'%s(%d)' % (self.segtype, self.segnum)
class UnaryRule(SegmentRule):
def __init__(self, child):
self.child = child
class ComplexRule(SegmentRule):
def __init__(self, children):
self.children = children
def addToNFA(self, fsa):
endState = RulesNFAState(final=True)
self._doAddToNFA(fsa.initialState, endState)
class ConcatRule(ComplexRule):
def __init__(self, children):
super(ConcatRule, self).__init__(children)
def _doAddToNFA(self, startState, endState):
currStartState = startState
for child in self.children[:-1]:
currEndState = RulesNFAState()
child._doAddToNFA(currStartState, currEndState)
nextStartState = RulesNFAState()
currEndState.addTransition(None, nextStartState)
currStartState = nextStartState
lastChild = self.children[-1]
lastChild._doAddToNFA(currStartState, endState)
def __str__(self):
return u' '.join(map(lambda c: str(c), self.children))
class OrRule(ComplexRule):
def __init__(self, children):
super(OrRule, self).__init__(children)
def _doAddToNFA(self, startState, endState):
for child in self.children:
intermStartState = RulesNFAState()
intermEndState = RulesNFAState()
startState.addTransition(None, intermStartState)
child._doAddToNFA(intermStartState, intermEndState)
intermEndState.addTransition(None, endState)
def __str__(self):
return u'|'.join(map(lambda c: str(c), self.children))
class ZeroOrMoreRule(UnaryRule):
def __init__(self, child):
super(ZeroOrMoreRule, self).__init__(child)
def addToNFA(self, fsa):
raise ValueError()
def _doAddToNFA(self, startState, endState):
intermStartState = RulesNFAState()
intermEndState = RulesNFAState()
startState.addTransition(None, intermStartState)
startState.addTransition(None, endState)
self.child._doAddToNFA(intermStartState, intermEndState)
intermEndState.addTransition(None, endState)
endState.addTransition(None, intermStartState)
def __str__(self):
return u'(' + str(self.child) + ')*'
class ShiftOrthRule(UnaryRule):
def __init__(self, child):
super(ShiftOrthRule, self).__init__(child)
def addToNFA(self, fsa):
raise ValueError()
def _doAddToNFA(self, startState, endState):
self.child._doAddToNFA(startState, endState)
startState.setTransitionData(self.child.segnum, 1)
def __str__(self):
return u'(' + str(self.child) + ')>'
class ShiftOrthSameTypeRule(UnaryRule):
def __init__(self, child):
super(ShiftOrthSameTypeRule, self).__init__(child)
def addToNFA(self, fsa):
raise ValueError()
def _doAddToNFA(self, startState, endState):
self.child._doAddToNFA(startState, endState)
startState.setTransitionData(self.child.segnum, 2)
def __str__(self):
return u'(' + str(self.child) + ')!>'