Blame view

fsabuilder/morfeuszbuilder/segrules/rulesManager.py 2.29 KB
Michał Lenart authored
1
2
3
4
5
'''
Created on 20 lut 2014

@author: mlenart
'''
Michał Lenart authored
6
import logging
Michał Lenart authored
7
from morfeuszbuilder.utils.serializationUtils import htons, htonl
Michał Lenart authored
8
Michał Lenart authored
9
10
class RulesManager(object):
Michał Lenart authored
11
    def __init__(self, segtypes):
Michał Lenart authored
12
        self.options2DFA = {}
Michał Lenart authored
13
        self.segtypes = segtypes
Michał Lenart authored
14
        self.defaultOptions = None
Michał Lenart authored
15
16
17
18

    def _options2Key(self, optionsMap):
        return frozenset(optionsMap.items())
Michał Lenart authored
19
20
21
    def _key2Options(self, optionsKey):
        return dict(optionsKey)
Michał Lenart authored
22
23
24
    def getDFA(self, optionsMap):
        return self.options2DFA[self._options2Key(optionsMap)]
Michał Lenart authored
25
26
27
    def setDefaultOptions(self, key2Def):
        self.defaultOptions = key2Def
Michał Lenart authored
28
    def addDFA(self, optionsMap, dfa):
Michał Lenart authored
29
30
        self.options2DFA[self._options2Key(optionsMap)] = dfa
Michał Lenart authored
31
32
    def lexeme2SegmentTypeNum(self, lemma, tagnum):
        res = self.segtypes.lexeme2Segnum(lemma, tagnum)
Michał Lenart authored
33
34
35
36
37
        if res is None:
            raise ValueError()
        else:
            return res
Michał Lenart authored
38
    def serialize(self):
Michał Lenart authored
39
40
41
42
43
44
45
46
        res = bytearray()
        dfasNum = len(self.options2DFA)
        assert dfasNum > 0 and dfasNum < 256
        res.append(dfasNum)
        for key, dfa in self.options2DFA.iteritems():
            optionsMap = self._key2Options(key)
            res.extend(self._serializeOptionsMap(optionsMap))
            res.extend(self._serializeDFA(dfa))
Michał Lenart authored
47
        res.extend(self._serializeOptionsMap(self.defaultOptions))
Michał Lenart authored
48
        logging.info('segmentation rules size: %s bytes', len(res))
Michał Lenart authored
49
50
51
52
53
        return res

    def _serializeOptionsMap(self, optionsMap):
        assert len(optionsMap) < 256
        res = bytearray()
Michał Lenart authored
54
55
        res.append(2)
        res.extend(self._serializeString('aggl'))
Michał Lenart authored
56
        res.extend(self._serializeString(optionsMap['aggl']))
Michał Lenart authored
57
        res.extend(self._serializeString('praet'))
Michał Lenart authored
58
        res.extend(self._serializeString(optionsMap['praet']))
Michał Lenart authored
59
60
61
62
        return res

    def _serializeDFA(self, dfa):
        res = bytearray()
Michał Lenart authored
63
64
65
#         serializer = SimpleSerializer(dfa, serializeTransitionsData=True)
        dfaBytearray = dfa.serialize()
        res.extend(htonl(len(dfaBytearray)))
Michał Lenart authored
66
67
68
69
70
        res.extend(dfaBytearray)
        return res

    def _serializeString(self, string):
        res = bytearray()
Michał Lenart authored
71
#         res.append(len(string))
Michał Lenart authored
72
        res.extend(string.encode('utf8'))
Michał Lenart authored
73
        res.append(0)
Michał Lenart authored
74
        return res