Blame view

fsabuilder/morfeuszbuilder/fsa/common.py 3.15 KB
Michał Lenart authored
1
2
3
4
5
6
7
'''
Created on Nov 7, 2013

@author: mlenart
'''

import codecs
Michał Lenart authored
8
import logging
Michał Lenart authored
9
Michał Lenart authored
10
class EncodedForm(object):
Michał Lenart authored
11
Michał Lenart authored
12
13
14
    def __init__(self, fromWord, targetWord):
        assert type(fromWord) == unicode
        assert type(targetWord) == unicode
Michał Lenart authored
15
        root = u''
Michał Lenart authored
16
        for o, b in zip(fromWord, targetWord):
Michał Lenart authored
17
            if o.lower() == b.lower():
Michał Lenart authored
18
                root += b
Michał Lenart authored
19
20
            else:
                break
Michał Lenart authored
21
22
23
24
        self.cutLength = len(fromWord) - len(root)
        self.suffixToAdd = targetWord[len(root):]
        self.casePattern = [c == c.upper() for c in root]
Michał Lenart authored
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class EncodedFormWithPrefix(object):

    def __init__(self, fromWord, targetWord):
        assert type(fromWord) == unicode
        assert type(targetWord) == unicode
        bestEncodedForm = None
        bestPrefixLength = -1
        for prefixLength in range(min(len(targetWord), 5)):
            encodedForm = EncodedForm(fromWord, targetWord[prefixLength:])
            if not bestEncodedForm \
            or len(encodedForm.suffixToAdd) + prefixLength < len(bestEncodedForm.suffixToAdd) + bestPrefixLength:
                bestEncodedForm = encodedForm
                bestPrefixLength = prefixLength
        assert bestPrefixLength >= 0
        self.cutLength = bestEncodedForm.cutLength
        self.suffixToAdd = bestEncodedForm.suffixToAdd
        self.prefixToAdd = targetWord[:bestPrefixLength]
Michał Lenart authored
43
class Interpretation4Analyzer(object):
Michał Lenart authored
44
45

    def __init__(self, orth, base, tagnum, namenum, typenum):
Michał Lenart authored
46
        self.encodedForm = EncodedForm(orth, base)
Michał Lenart authored
47
48
        self.tagnum = tagnum
        self.namenum = namenum
Michał Lenart authored
49
        self.typenum = typenum
Michał Lenart authored
50
51

    def getSortKey(self):
Michał Lenart authored
52
        return (
Michał Lenart authored
53
54
55
                self.encodedForm.cutLength, 
                tuple(self.encodedForm.suffixToAdd), 
                tuple(self.encodedForm.casePattern), 
Michał Lenart authored
56
57
                self.tagnum, 
                self.namenum)
Michał Lenart authored
58
59

    def __eq__(self, other):
Michał Lenart authored
60
        if isinstance(other, Interpretation4Analyzer):
Michał Lenart authored
61
62
63
64
65
66
67
            return self.getSortKey() == other.getSortKey()
        else:
            return False

    def __hash__(self):
        return hash(self.getSortKey())
Michał Lenart authored
68
69
class Interpretation4Generator(object):
Michał Lenart authored
70
    def __init__(self, orth, base, tagnum, namenum, typenum):
Michał Lenart authored
71
72
        self.encodedForm = base
        self.encodedForm = EncodedFormWithPrefix(base, orth)
Michał Lenart authored
73
74
        self.tagnum = tagnum
        self.namenum = namenum
Michał Lenart authored
75
        self.typenum = typenum
Michał Lenart authored
76
77
78

    def getSortKey(self):
        return (
Michał Lenart authored
79
                self.tagnum,
Michał Lenart authored
80
81
82
                self.encodedForm.cutLength, 
                tuple(self.encodedForm.suffixToAdd), 
#                 tuple(self.encodedForm.casePattern), 
Michał Lenart authored
83
84
85
                self.namenum)

    def __eq__(self, other):
Michał Lenart authored
86
        if isinstance(other, Interpretation4Generator):
Michał Lenart authored
87
88
89
90
91
92
            return self.getSortKey() == other.getSortKey()
        else:
            return False

    def __hash__(self):
        return hash(self.getSortKey())
Michał Lenart authored
93
94

    def __unicode__(self):
Michał Lenart authored
95
        return u'<%s,(%d %s),%d,%d>' % (self.encodedForm.decode('utf8'), self.encodedForm.cutLength, self.encodedForm.suffixToAdd.decode('utf8'), self.tagnum, self.namenum)
Michał Lenart authored
96
97
98

    def __repr__(self):
        return unicode(self)