|
1
2
3
4
5
6
7
8
9
10
11
12
13
|
'''
Created on Oct 8, 2013
@author: mlenart
'''
class State(object):
'''
A state in an automaton
'''
def __init__(self):
self.transitionsMap = {}
|
|
14
|
self.freq = 0
|
|
15
16
17
|
self.encodedData = None
self.reverseOffset = None
self.offset = None
|
|
18
19
|
self.label2Freq = {}
self.serializeAsArray = False
|
|
20
|
|
|
21
22
23
24
|
@property
def transitionsNum(self):
return len(self.transitionsMap)
|
|
25
26
27
28
29
30
|
def setTransition(self, byte, nextState):
self.transitionsMap[byte] = nextState
def hasNext(self, byte):
return byte in self.transitionsMap
|
|
31
32
33
|
def getNext(self, byte, addFreq=False):
if addFreq:
self.freq += 1
|
|
34
|
self.label2Freq[byte] = self.label2Freq.get(byte, 0) + 1
|
|
35
36
37
38
39
40
41
42
|
return self.transitionsMap.get(byte, None)
def getRegisterKey(self):
return ( frozenset(self.transitionsMap.iteritems()), tuple(self.encodedData) if self.encodedData else None )
def isAccepting(self):
return self.encodedData is not None
|
|
43
|
def tryToRecognize(self, word, addFreq=False):
|
|
44
|
if word:
|
|
45
|
nextState = self.getNext(word[0], addFreq)
|
|
46
|
if nextState:
|
|
47
|
return nextState.tryToRecognize(word[1:], addFreq)
|
|
48
49
50
51
52
|
else:
return False
else:
return self.encodedData
|
|
53
|
def dfs(self, alreadyVisited=set(), sortKey=lambda (_, state): -state.freq):
|
|
54
|
if not self in alreadyVisited:
|
|
55
|
for _, state in sorted(self.transitionsMap.iteritems(), key=sortKey):
|
|
56
57
58
59
|
for state1 in state.dfs(alreadyVisited):
yield state1
alreadyVisited.add(self)
yield self
|