|
1
2
3
4
5
6
|
'''
Created on 24 sty 2014
@author: mlenart
'''
|
|
7
8
|
from morfeuszbuilder.segrules.rulesNFA import RulesNFAState
|
|
9
10
11
12
13
14
15
|
class SegmentRule(object):
'''
classdocs
'''
def __init__(self):
|
|
16
17
18
19
20
21
|
self.weak = False
def setWeak(self, weak):
self.weak = weak
return self
|
|
22
23
24
25
26
27
|
def addToNFA(self, fsa):
raise NotImplementedError()
def _doAddToNFA(self, startStates, endState):
raise NotImplementedError()
|
|
28
29
30
|
class TagRule(SegmentRule):
|
|
31
|
def __init__(self, segnum, shiftOrth, segtype):
|
|
32
|
self.segnum = segnum
|
|
33
|
self.segtype = segtype
|
|
34
|
self.shiftOrth = shiftOrth
|
|
35
36
|
def addToNFA(self, fsa):
|
|
37
|
endState = RulesNFAState(final=True, weak=self.weak)
|
|
38
39
40
|
self._doAddToNFA(fsa.initialState, endState)
def _doAddToNFA(self, startState, endState):
|
|
41
|
startState.addTransition((self.segnum, self.shiftOrth), endState)
|
|
42
43
|
def __str__(self):
|
|
44
|
return u'%s(%d)' % (self.segtype, self.segnum)
|
|
45
46
47
|
class UnaryRule(SegmentRule):
|
|
48
|
def __init__(self, child):
|
|
49
50
51
52
|
self.child = child
class ComplexRule(SegmentRule):
|
|
53
|
def __init__(self, children):
|
|
54
|
self.children = children
|
|
55
56
|
def addToNFA(self, fsa):
|
|
57
|
endState = RulesNFAState(final=True, weak=self.weak)
|
|
58
|
self._doAddToNFA(fsa.initialState, endState)
|
|
59
60
61
|
class ConcatRule(ComplexRule):
|
|
62
63
64
65
66
67
68
69
70
71
72
73
74
|
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)
|
|
75
|
|
|
76
77
78
|
def __str__(self):
return u' '.join(map(lambda c: str(c), self.children))
|
|
79
80
|
class OrRule(ComplexRule):
|
|
81
82
83
84
85
86
87
88
89
90
|
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)
|
|
91
|
|
|
92
93
94
|
def __str__(self):
return u'|'.join(map(lambda c: str(c), self.children))
|
|
95
96
|
class ZeroOrMoreRule(UnaryRule):
|
|
97
98
|
def __init__(self, child):
super(ZeroOrMoreRule, self).__init__(child)
|
|
99
|
assert isinstance(child, SegmentRule)
|
|
100
101
102
|
def addToNFA(self, fsa):
raise ValueError()
|
|
103
|
|
|
104
105
106
107
108
109
110
111
112
|
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)
|
|
113
114
115
|
def __str__(self):
return u'(' + str(self.child) + ')*'
|