serializer.py
6.94 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
'''
Created on Oct 20, 2013
@author: mlenart
'''
import logging
class Serializer(object):
def __init__(self, fsa):
self.fsa = fsa
self.label2Count = {}
for state in self.fsa.initialState.dfs():
for label, _ in state.transitionsMap.iteritems():
self.label2Count[label] = self.label2Count.get(label, 0) + 1
def serialize2CppFile(self, fname):
res = []
self.fsa.calculateOffsets(sizeCounter=lambda state: self.getStateSize(state))
res.append('const unsigned char DEFAULT_FSA[] = {')
for idx, state in enumerate(sorted(self.fsa.initialState.dfs(set()), key=lambda state: state.offset)):
res.append('// state '+str(idx))
partRes = []
for byte in self.state2bytearray(state):
partRes.append(hex(byte))
partRes.append(',')
res.append(' '.join(partRes))
res.append('}')
with open(fname, 'w') as f:
f.write('\n'.join(res))
def serialize2BinaryFile(self, fname):
res = bytearray()
self.fsa.calculateOffsets(sizeCounter=lambda state: self.getStateSize(state))
for state in sorted(self.fsa.initialState.dfs(set()), key=lambda state: state.offset):
# res.append('// state '+str(idx))
res.extend(self.state2bytearray(state))
with open(fname, 'wb') as f:
f.write(res)
def getStateSize(self, state):
raise NotImplementedError('Not implemented')
def fsa2bytearray(self, fsa):
res = bytearray()
fsa.calculateOffsets(sizeCounter=lambda state: self.getStateSize(state))
for state in sorted(fsa.initialState.dfs(set()), key=state.offset):
res.extend(self.state2bytearray(state))
return res
def state2bytearray(self, state):
raise NotImplementedError('Not implemented')
class SimpleSerializer(Serializer):
ACCEPTING_FLAG = 128
def getStateSize(self, state):
return 1 + 4 * len(state.transitionsMap.keys()) + self.getDataSize(state)
def getDataSize(self, state):
assert type(state.encodedData) == bytearray or not state.isAccepting()
return len(state.encodedData) if state.isAccepting() else 0
def state2bytearray(self, state):
res = bytearray()
res.extend(self._stateData2bytearray(state))
res.extend(self._transitionsData2bytearray(state))
return res
def _stateData2bytearray(self, state):
res = bytearray()
firstByte = 0
if state.isAccepting():
firstByte |= SimpleSerializer.ACCEPTING_FLAG
firstByte |= len(state.transitionsMap)
assert firstByte < 256 and firstByte > 0
res.append(firstByte)
if state.isAccepting():
res.extend(state.encodedData)
return res
def _transitionsData2bytearray(self, state):
res = bytearray()
# logging.debug('next')
for byte, nextState in sorted(state.transitionsMap.iteritems(), key=lambda (label, next): (-next.freq, -self.label2Count[label])):
# logging.debug(byte)
res.append(byte)
offset = nextState.offset
res.append(offset & 0x0000FF)
res.append((offset & 0x00FF00) >> 8)
res.append((offset & 0xFF0000) >> 16)
return res
class VLengthSerializer(Serializer):
LAST_FLAG = 128
def __init__(self, fsa):
super(VLengthSerializer, self).__init__(fsa)
self.statesTable = list(reversed(fsa.dfs(set())))
self.state2Index = dict([(state, idx) for (idx, state) in enumerate(self.statesTable)])
def getStateSize(self, state):
return len(self.state2bytearray(state))
def getDataSize(self, state):
assert type(state.encodedData) == bytearray or not state.isAccepting()
return len(state.encodedData) if state.isAccepting() else 0
def state2bytearray(self, state):
res = bytearray()
res.extend(self._stateData2bytearray(state))
res.extend(self._transitionsData2bytearray(state))
return res
def _stateData2bytearray(self, state):
res = bytearray()
if state.isAccepting():
res.extend(state.encodedData)
return res
def _transitionsData2bytearray(self, state):
res = bytearray()
sortedLabels = list(sorted(self.fsa.label2Freq.iteritems(), key=lambda label, freq: (-freq, label)))
label2Index = dict([(label, sortedLabels.index(label)) for label in sortedLabels][:30])
transitions = sorted(state.transitionsMap.iteritems(), key=lambda (label, _): (-next.freq, -self.label2Count[label]))
thisIdx = self.state2Index[state]
if len(transitions) == 0:
assert state.isAccepting()
return bytearray()
else:
offsetToStateAfterThis = 0
stateAfterThis = self.statesTable[thisIdx + 1]
for reversedN, (label, nextState) in enumerate(reversed(transitions)):
assert nextState.reverseOffset is not None
n = len(transitions) - reversedN
popularLabel = label2Index[label] < 31
firstByte = (label2Index[label] + 1) if popularLabel else 0
# if state.isAccepting():
# firstByte |= VLengthSerializer.ACCEPTING_FLAG
last = len(transitions) == n
next = last and stateAfterThis == nextState
if last:
firstByte |= VLengthSerializer.LAST_FLAG
offsetSize = 0
offset = 0
if not next:
offsetSize = 1
offset = (stateAfterThis.reverseOffset - nextState.reverseOffset) + offsetSize
if offset >= 256:
offset += 1
offsetSize += 1
if offset >= 256 * 256:
offset += 1
offsetSize += 1
assert offset < 256 * 256 * 256 #TODO - przerobić na jakiś porządny wyjątek
firstByte |= (32 * offsetSize)
res.append(firstByte)
if not popularLabel:
res.append(label)
if offsetSize >= 1:
res.append(offset & 0x0000FF)
if offsetSize >= 2:
res.append((offset & 0x00FF00) >> 8)
if offsetSize == 3:
res.append((offset & 0xFF0000) >> 16)
return res
# currReverseOffset = nextState.reverseOffset
# res.append(byte)
# offset = nextState.offset
# res.append(offset & 0x0000FF)
# res.append((offset & 0x00FF00) >> 8)
# res.append((offset & 0xFF0000) >> 16)
# return res