common.py
2.21 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
'''
Created on Nov 7, 2013
@author: mlenart
'''
import codecs
import logging
class EncodedForm(object):
def __init__(self, fromWord, targetWord):
assert type(fromWord) == unicode
assert type(targetWord) == unicode
root = u''
for o, b in zip(fromWord, targetWord):
if o.lower() == b.lower():
root += b
else:
break
self.cutLength = len(fromWord) - len(root)
self.suffixToAdd = targetWord[len(root):]
self.casePattern = [c == c.upper() for c in root]
class Interpretation(object):
def __init__(self, orth, base, tagnum, namenum, typenum):
self.lemma = EncodedForm(orth, base)
self.tagnum = tagnum
self.namenum = namenum
self.typenum = typenum
def getSortKey(self):
return (
self.lemma.cutLength,
tuple(self.lemma.suffixToAdd),
tuple(self.lemma.casePattern),
self.tagnum,
self.namenum)
def __eq__(self, other):
if isinstance(other, Interpretation):
return self.getSortKey() == other.getSortKey()
else:
return False
def __hash__(self):
return hash(self.getSortKey())
class Interpretation4Generator(object):
def __init__(self, orth, base, tagnum, namenum):
self.lemma = base
self.orth = EncodedForm(base, orth)
self.tagnum = tagnum
self.namenum = namenum
def getSortKey(self):
return (
self.tagnum,
self.orth.cutLength,
tuple(self.orth.suffixToAdd),
# tuple(self.lemma.casePattern),
self.namenum)
def __eq__(self, other):
if isinstance(other, Interpretation4Generator):
return self.getSortKey() == other.getSortKey()
else:
return False
def __hash__(self):
return hash(self.getSortKey())
def __unicode__(self):
return u'<%s,(%d %s),%d,%d>' % (self.lemma.decode('utf8'), self.orth.cutLength, self.orth.suffixToAdd.decode('utf8'), self.tagnum, self.namenum)
def __repr__(self):
return unicode(self)