forked from tidalmelon/addrseg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
entities.py
219 lines (172 loc) · 5.98 KB
/
entities.py
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
# -*- coding: utf-8 -*-
import bisect
class Singleton(object):
def __new__(cls, *args, **kw):
if not hasattr(cls, '_instance'):
orig = super(Singleton, cls)
cls._instance = orig.__new__(cls, *args, **kw)
return cls._instance
class AddressType(object):
Country = 0
Municipality = 1 # 直辖市
SuffixMunicipality = 2 # 特别行政区后缀
Province = 3 # 省
City = 4 # 市
County = 5 # 区
Town = 6 # 镇
Street = 7 # 街道
StreetNo = 8 # 街门牌号
No = 9 # 编号
Symbol = 10 # 字母符号
LandMark = 11 # 地标性建筑
RelatedPos = 12 # 相对位置
Crossing = 13 # 交叉路
DetailDesc = 14 # 详细描述
ChildFacility = 15 # 子设施
Village = 16 # 村
VillageNo = 17 # 村号
BuildingNo = 18 # 楼号
BuildingUnit = 19 #楼单元
SuffixBuildingUnit = 20 #楼单元后缀
SuffixBuildingNo = 21 #楼号后缀
Start = 22 # 开始状态
End = 23 # 结束状态
StartSuffix = 24
EndSuffix = 25
Unknow = 26
Other = 27
SuffixProvince = 28
SuffixCity = 29
SuffixCounty = 30
District = 31
SuffixDistrict = 32
SuffixTown = 33
SuffixStreet = 34
SuffixLandMark = 35
SuffixVillage = 36
SuffixIndicationFacility = 37 # 指示性设施后缀
IndicationFacility = 38 # 指示性设施
SuffixIndicationPosition = 39 # 指示设施方位后缀
IndicationPosition = 40 # 指示性设施方位
Conj = 41 # 连接词
names = ['Country', 'Municipality', 'SuffixMunicipality', 'Province', 'City', 'County', 'Town', 'Street', 'StreetNo', 'No', 'Symbol', 'LandMark', 'RelatedPos', 'Crossing', 'DetailDesc', 'ChildFacility', 'Village', 'VillageNo', 'BuildingNo', 'BuildingUnit', 'SuffixBuildingUnit', 'SuffixBuildingNo', 'Start', 'End', 'StartSuffix', 'EndSuffix', 'Unknow', 'Other', 'SuffixProvince', 'SuffixCity', 'SuffixCounty', 'District', 'SuffixDistrict', 'SuffixTown', 'SuffixStreet', 'SuffixLandMark', 'SuffixVillage', 'SuffixIndicationFacility', 'IndicationFacility', 'SuffixIndicationPosition', 'IndicationPosition', 'Conj']
def getName(idx):
return AddressType.names[idx]
@staticmethod
def getLen():
return 42
class AddTypeInf(object):
def __init__(self, pos, freq, semanticCode, parent):
"""
param:bestPrev AddTypeInf, herself
param:parent AddressTokenInf
"""
self.pos = pos
self.weight = freq
self.code = semanticCode
self.prob = 0
self.parent = parent
self.bestPre = None
@staticmethod
def convertFrom(addTypeInf, parent):
ins = AddTypeInf(addTypeInf.pos, addTypeInf.freq, addTypeInf.code, parent)
ins.prob = 0
ins.bestPre = None
return ins
def __cmp__(self, other):
return cmp(self.pos, other.pos)
def __str__(self):
return AddressType.names[self.pos] + ':' + str(self.weight)
class AddTypes(object):
def __init__(self):
self.data = []
def put(self, item):
self.data.append(item)
def insert(self, item):
bisect.insort(self.data, item)
def findType(self, pos):
try:
idx = self.data.index(AddTypeInf(pos))
return self.data[idx]
except ValueError:
return None
def size(self):
return len(self.data)
def totalCost(self):
return sum([e.weight for e in self.data])
def getHead(self):
return self.data[0]
def __str__(self):
res = u''
for ati in self.data:
res += str(ati)
return res
class AddDicTypes(object):
class AddTypeInf(object):
def __init__(self, pos, freq, semanticCode):
self.pos = pos
# 频率
self.freq = freq
# 语义编码
self.code = semanticCode
def __cmp__(self, other):
return cmp(self.pos, other.pos)
def __str__(self):
return AddressType.names[self.pos] + ':' + str(self.freq)
def __init__(self):
self.data = []
def put(self, item):
self.data.append(item)
def insert(self, item):
bisect.insort(self.data, item)
def findType(self, pos):
toFind = AddDicTypes.AddTypeInf(pos, 0, 0)
idx = bisect.bisect_left(self.data, toFind)
try:
if self.data[idx] == toFind:
return self.data[idx]
except IndexError:
return None
return None
def size(self):
return len(self.data)
def totalCost(self):
return sum([e.freq for e in self.data])
def getHead(self):
return self.data[0]
def __str__(self):
buf = [str(item) for item in self.data]
return ' '.join(buf)
class AddressTokenInf(object):
def __init__(self, vertexFrom, vertexTo, word):
self.start = vertexFrom
self.end = vertexTo
self.termText = word
self.data = None
self.cost = 0
self.bestPrev = None
self.bestTypeInf = None
def setData(self, d):
self.data = d
self.cost = d.totalCost()
def __str__(self):
line = u'text:' + self.termText + ' '\
+ 'start:' + str(self.start) + ' ' \
+ 'end:' + str(self.end) + ' '\
+ 'cost:' + str(self.cost) + ' '\
+ 'data:' + str(self.data)
return line.encode('utf-8')
class AddressToken(object):
def __init__(self, start=None, end=None, cost=None, word=None, type=None, code=None):
self.termText = word
self.type = type
self.start = start
self.end = end
self.cost = cost
self.code = code
def __str__(self):
line = 'text:' + self.termText + ' start:' + str(self.start) \
+ ' end:' + str(self.end) + ' cost:' + str(self.cost) \
+ ' pos:' + AddressType.names[self.type] + ' code:' + str(self.code)
line = line.encode('utf-8')
return line