|
1
2
3
4
5
6
7
|
'''
Created on Nov 7, 2013
@author: mlenart
'''
import codecs
|
|
8
|
import logging
|
|
9
|
|
|
10
|
class EncodedForm(object):
|
|
11
|
|
|
12
13
14
|
def __init__(self, fromWord, targetWord):
assert type(fromWord) == unicode
assert type(targetWord) == unicode
|
|
15
|
root = u''
|
|
16
|
for o, b in zip(fromWord, targetWord):
|
|
17
|
if o.lower() == b.lower():
|
|
18
|
root += b
|
|
19
20
|
else:
break
|
|
21
22
23
24
|
self.cutLength = len(fromWord) - len(root)
self.suffixToAdd = targetWord[len(root):]
self.casePattern = [c == c.upper() for c in root]
|
|
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]
|
|
43
|
class Interpretation4Analyzer(object):
|
|
44
45
|
def __init__(self, orth, base, tagnum, namenum, typenum):
|
|
46
|
self.encodedForm = EncodedForm(orth, base)
|
|
47
48
|
self.tagnum = tagnum
self.namenum = namenum
|
|
49
|
self.typenum = typenum
|
|
50
51
|
def getSortKey(self):
|
|
52
|
return (
|
|
53
54
55
|
self.encodedForm.cutLength,
tuple(self.encodedForm.suffixToAdd),
tuple(self.encodedForm.casePattern),
|
|
56
57
|
self.tagnum,
self.namenum)
|
|
58
59
|
def __eq__(self, other):
|
|
60
|
if isinstance(other, Interpretation4Analyzer):
|
|
61
62
63
64
65
66
67
|
return self.getSortKey() == other.getSortKey()
else:
return False
def __hash__(self):
return hash(self.getSortKey())
|
|
68
69
|
class Interpretation4Generator(object):
|
|
70
|
def __init__(self, orth, base, tagnum, namenum, typenum):
|
|
71
72
|
self.encodedForm = base
self.encodedForm = EncodedFormWithPrefix(base, orth)
|
|
73
74
|
self.tagnum = tagnum
self.namenum = namenum
|
|
75
|
self.typenum = typenum
|
|
76
77
78
|
def getSortKey(self):
return (
|
|
79
|
self.tagnum,
|
|
80
81
82
|
self.encodedForm.cutLength,
tuple(self.encodedForm.suffixToAdd),
# tuple(self.encodedForm.casePattern),
|
|
83
84
85
|
self.namenum)
def __eq__(self, other):
|
|
86
|
if isinstance(other, Interpretation4Generator):
|
|
87
88
89
90
91
92
|
return self.getSortKey() == other.getSortKey()
else:
return False
def __hash__(self):
return hash(self.getSortKey())
|
|
93
94
|
def __unicode__(self):
|
|
95
|
return u'<%s,(%d %s),%d,%d>' % (self.encodedForm.decode('utf8'), self.encodedForm.cutLength, self.encodedForm.suffixToAdd.decode('utf8'), self.tagnum, self.namenum)
|
|
96
97
98
|
def __repr__(self):
return unicode(self)
|