Blame view

fsabuilder/morfeuszbuilder/segrules/rules.py 3.27 KB
Michał Lenart authored
1
2
3
4
5
6
'''
Created on 24 sty 2014

@author: mlenart
'''
Michał Lenart authored
7
8
from morfeuszbuilder.segrules.rulesNFA import RulesNFAState
Michał Lenart authored
9
10
11
12
13
14
15
class SegmentRule(object):
    '''
    classdocs
    '''


    def __init__(self):
Michał Lenart authored
16
17
18
19
20
21

        self.weak = False

    def setWeak(self, weak):
        self.weak = weak
        return self
Michał Lenart authored
22
23
24
25
26
27

    def addToNFA(self, fsa):
        raise NotImplementedError()

    def _doAddToNFA(self, startStates, endState):
        raise NotImplementedError()
Michał Lenart authored
28
29
30

class TagRule(SegmentRule):
Michał Lenart authored
31
    def __init__(self, segnum, shiftOrth, segtype):
Michał Lenart authored
32
        self.segnum = segnum
Michał Lenart authored
33
        self.segtype = segtype
Michał Lenart authored
34
        self.shiftOrth = shiftOrth
Michał Lenart authored
35
36

    def addToNFA(self, fsa):
Michał Lenart authored
37
        endState = RulesNFAState(final=True, weak=self.weak)
Michał Lenart authored
38
39
40
        self._doAddToNFA(fsa.initialState, endState)

    def _doAddToNFA(self, startState, endState):
Michał Lenart authored
41
        startState.addTransition((self.segnum, self.shiftOrth), endState)
Michał Lenart authored
42
43

    def __str__(self):
Michał Lenart authored
44
        return u'%s(%d)' % (self.segtype, self.segnum)
Michał Lenart authored
45
46
47

class UnaryRule(SegmentRule):
Michał Lenart authored
48
    def __init__(self, child):
Michał Lenart authored
49
50
51
52
        self.child = child

class ComplexRule(SegmentRule):
Michał Lenart authored
53
    def __init__(self, children):
Michał Lenart authored
54
        self.children = children
Michał Lenart authored
55
56

    def addToNFA(self, fsa):
Michał Lenart authored
57
        endState = RulesNFAState(final=True, weak=self.weak)
Michał Lenart authored
58
        self._doAddToNFA(fsa.initialState, endState)
Michał Lenart authored
59
60
61

class ConcatRule(ComplexRule):
Michał Lenart authored
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)
Michał Lenart authored
75
Michał Lenart authored
76
77
78
    def __str__(self):
        return u' '.join(map(lambda c: str(c), self.children))
Michał Lenart authored
79
80
class OrRule(ComplexRule):
Michał Lenart authored
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)
Michał Lenart authored
91
Michał Lenart authored
92
93
94
    def __str__(self):
        return u'|'.join(map(lambda c: str(c), self.children))
Michał Lenart authored
95
96
class ZeroOrMoreRule(UnaryRule):
Michał Lenart authored
97
98
    def __init__(self, child):
        super(ZeroOrMoreRule, self).__init__(child)
Michał Lenart authored
99
        assert isinstance(child, SegmentRule)
Michał Lenart authored
100
101
102

    def addToNFA(self, fsa):
        raise ValueError()
Michał Lenart authored
103
Michał Lenart authored
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)
Michał Lenart authored
113
114
115

    def __str__(self):
        return u'(' + str(self.child) + ')*'