|
1
2
3
4
5
6
7
|
'''
Created on Nov 7, 2013
@author: mlenart
'''
import codecs
|
|
8
|
import logging
|
|
9
|
|
|
10
|
class EncodedFormWithoutPrefix(object):
|
|
11
|
|
|
12
|
def __init__(self, fromWord, targetWord, lowercase):
|
|
13
14
|
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()) if lowercase else o == b):
|
|
18
|
root += b
|
|
19
20
|
else:
break
|
|
21
22
|
self.cutLength = len(fromWord) - len(root)
self.suffixToAdd = targetWord[len(root):]
|
|
23
|
self.casePattern = [c == c.upper() and c != c.lower() for c in root]
|
|
24
|
# self.prefixCutLength = 0
|
|
25
|
|
|
26
|
class EncodedForm4Generator(object):
|
|
27
|
|
|
28
|
def __init__(self, fromWord, targetWord):
|
|
29
30
31
32
33
|
assert type(fromWord) == unicode
assert type(targetWord) == unicode
bestEncodedForm = None
bestPrefixLength = -1
for prefixLength in range(min(len(targetWord), 5)):
|
|
34
|
encodedForm = EncodedFormWithoutPrefix(fromWord, targetWord[prefixLength:], lowercase=False)
|
|
35
36
37
38
39
|
if not bestEncodedForm \
or len(encodedForm.suffixToAdd) + prefixLength < len(bestEncodedForm.suffixToAdd) + bestPrefixLength:
bestEncodedForm = encodedForm
bestPrefixLength = prefixLength
assert bestPrefixLength >= 0
|
|
40
|
|
|
41
42
43
|
self.cutLength = bestEncodedForm.cutLength
self.suffixToAdd = bestEncodedForm.suffixToAdd
self.prefixToAdd = targetWord[:bestPrefixLength]
|
|
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
|
class EncodedForm4Analyzer(object):
def __init__(self, fromWord, targetWord):
assert type(fromWord) == unicode
assert type(targetWord) == unicode
bestEncodedForm = None
bestPrefixCutLength = -1
for prefixCutLength in range(min(len(fromWord), 5)):
encodedForm = EncodedFormWithoutPrefix(fromWord[prefixCutLength:], targetWord, lowercase=True)
if not bestEncodedForm \
or len(encodedForm.suffixToAdd) + prefixCutLength < len(bestEncodedForm.suffixToAdd):
bestEncodedForm = encodedForm
bestPrefixCutLength = prefixCutLength
assert bestPrefixCutLength >= 0
self.prefixCutLength = bestPrefixCutLength
self.cutLength = bestEncodedForm.cutLength
self.suffixToAdd = bestEncodedForm.suffixToAdd
self.casePattern = bestEncodedForm.casePattern
|
|
64
|
|
|
65
|
class Interpretation4Analyzer(object):
|
|
66
|
|
|
67
|
def __init__(self, orth, base, tagnum, namenum, typenum, qualifiers):
|
|
68
|
self.encodedForm = EncodedForm4Analyzer(orth, base)
|
|
69
|
self.orthCasePattern = [c == c.upper() and c != c.lower() for c in orth[:len(orth) - self.encodedForm.cutLength]]
|
|
70
71
|
self.tagnum = tagnum
self.namenum = namenum
|
|
72
|
self.typenum = typenum
|
|
73
|
self.qualifiers = qualifiers
|
|
74
75
|
def getSortKey(self):
|
|
76
|
return (
|
|
77
78
|
self.encodedForm.cutLength,
self.encodedForm.prefixCutLength,
|
|
79
|
tuple(self.encodedForm.suffixToAdd),
|
|
80
|
tuple(self.encodedForm.casePattern),
|
|
81
|
tuple(self.orthCasePattern),
|
|
82
83
|
self.tagnum,
self.namenum)
|
|
84
85
|
def __eq__(self, other):
|
|
86
|
if isinstance(other, Interpretation4Analyzer):
|
|
87
88
89
90
91
92
93
|
return self.getSortKey() == other.getSortKey()
else:
return False
def __hash__(self):
return hash(self.getSortKey())
|
|
94
95
|
class Interpretation4Generator(object):
|
|
96
|
def __init__(self, orth, base, tagnum, namenum, typenum, homonymId, qualifiers):
|
|
97
|
self.lemma = base
|
|
98
|
self.encodedForm = EncodedForm4Generator(base, orth)
|
|
99
100
|
self.tagnum = tagnum
self.namenum = namenum
|
|
101
|
self.typenum = typenum
|
|
102
|
self.homonymId = homonymId
|
|
103
|
self.qualifiers = qualifiers
|
|
104
105
106
|
def getSortKey(self):
return (
|
|
107
|
self.homonymId,
|
|
108
|
self.tagnum,
|
|
109
|
self.encodedForm.cutLength,
|
|
110
|
tuple(self.encodedForm.suffixToAdd),
|
|
111
|
# tuple(self.encodedForm.casePattern),
|
|
112
113
114
|
self.namenum)
def __eq__(self, other):
|
|
115
|
if isinstance(other, Interpretation4Generator):
|
|
116
117
118
119
120
121
|
return self.getSortKey() == other.getSortKey()
else:
return False
def __hash__(self):
return hash(self.getSortKey())
|
|
122
123
|
def __unicode__(self):
|
|
124
|
return u'<%s,(%d %s),%d,%d>' % (self.lemma.decode('utf8'), self.encodedForm.cutLength, self.encodedForm.suffixToAdd.decode('utf8'), self.tagnum, self.namenum)
|
|
125
126
127
|
def __repr__(self):
return unicode(self)
|