-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathabbreviations.rb
344 lines (333 loc) · 9.29 KB
/
abbreviations.rb
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# encoding: UTF-8
require 'set'
require 'algorithms'
# mod for doing stuff w/ abbrevs
module Abbrev
# a list of abbreviations culled, at least in part, from the USPS
# official list https://www.usps.com/send/official-abbreviations.htm#2
# this might be too country-specific, but it's a start.
classes = [
["alley", "aly"],
["and", "&"],
["annex", "anx"],
["arcade", "arc"],
["avenue", "ave"],
["beach", "bch"],
["burg", "bg"],
["bluff", "blf"],
["boulevard", "blvd"],
["bend", "bnd"],
["branch", "br"],
["bridge", "brg"],
["brook", "brk"],
["bottom", "btm"],
["bayoo", "byu"],
["circle", "cir"],
["club", "clb"],
["cliff", "clf"],
["common", "cmn"],
["corner", "cor"],
["camp", "cp"],
["cape", "cpe"],
["crescent", "cr","cres"],
["creek", "crk"],
["course", "crse"],
["crest", "crst"],
["causeway", "cswy"],
["court", "ct"],
["center", "ctr"],
["curve", "curv"],
["cove", "cv"],
["canyon", "cyn"],
["dale", "dl"],
["dam", "dm"],
["drive", "dr"],
["doctor", "dr"],
["divide", "dv"],
["east", "e"],
["estate", "est"],
["expressway", "expy"],
["extension", "ext"],
["field", "fld"],
["flat", "flt"],
["ford", "frd"],
["forge", "frg"],
["fork", "frk"],
["forest", "frst"],
["ferry", "fry"],
["fort", "ft"],
["freeway", "fwy"],
["garden", "gdn"],
["glen", "gln"],
["green", "grn"],
["grove", "grv"],
["gateway", "gtwy"],
["harbor", "hbr"],
["hill", "hl"],
["hollow", "holw"],
["haven", "hvn"],
["highway", "hwy"],
["inlet", "inlt"],
["island", "is"],
["junction", "jct"],
["knoll", "knl"],
["key", "ky"],
["lock", "lck"],
["lodge", "ldg"],
["loaf", "lf"],
["light", "lgt"],
["lake", "lk"],
["lane", "ln"],
["landing", "lndg"],
["meadow", "mdw"],
["mill", "ml"],
["manor", "mnr"],
["mission", "msn"],
["mount", "mt"],
["mountain", "mtn"],
["motorway", "mtwy"],
["neck", "nck"],
["north", "n"],
["orchard", "orch"],
["parkway", "pkwy"],
["place", "pl"],
["plain", "pln"],
["plaza", "plz"],
["pine", "pne"],
["prairie", "pr"],
["port", "prt"],
["passage", "psge"],
["point", "pt"],
["radial", "radl"],
["road", "rd"],
["ridge", "rdg"],
["river", "riv"],
["ranch", "rnch"],
["row", "row"],
["rapid", "rpd"],
["rest", "rst"],
["route", "rte"],
["shoal", "shl"],
["shore", "shr"],
["skyway", "skwy"],
["south", "s"],
["summit", "smt"],
["spring", "spg"],
["square", "sq"],
["street", "st"],
["station", "sta"],
["stravenue", "stra"],
["stream", "strm"],
["terrace", "ter"],
["turnpike", "tpke"],
["track", "trak"],
["trace", "trce"],
["trafficway", "trfy"],
["trail", "trl"],
["throughway", "trwy"],
["tunnel", "tunl"],
["union", "un"],
["viaduct", "via"],
["vista", "vis"],
["ville", "vl"],
["village", "vlg"],
["valley", "vly"],
["view", "vw"],
["way", "wy"],
["well", "wl"],
["west", "w"],
["crossing", "xing"],
["crossroad", "xrd"],
# Russian abbreviations
# Copyright (C) 2011-2012 Dmitry Marakasov
# from https://github.com/AMDmi3/streetmangler/blob/master/lib/locales/ru.cc#L27
["улица", "ул"],
["площадь", "пл"],
["переулок", "пер", "пер-к"],
["проезд", "пр-д"],
["шоссе", "ш"],
["бульвар", "бул", "б-р"],
["тупик", "туп"],
["набережная", "наб"],
["проспект", "просп", "пр-кт", "пр-т"],
["тракт", "тр-т", "тр"],
["эстакада", "эст"],
["район", "р-н"],
["микрорайон", "мкр-н", "мк-н", "мкр", "мкрн"],
["посёлок", "поселок", "пос"],
["деревня", "дер", "д"],
["квартал", "кв-л", "кв"],
# German abbreviations
["anschlussstelle", "as"],
["an", "a"],
["bahnhof", "bf"],
["bei", "b"],
["bürgermeister", "bgm"],
["der", "d"],
["den", "d"],
["dem", "d"],
["evangelische", "ev", "evang"],
["evangelischer", "ev", "evang"],
["evangelisches", "ev", "evang"],
["evangelisch", "ev", "evang"],
["fachhochschule", "fh"],
["gasse", "g"],
["gemeinschaft", "gem"],
["gemeinschafts", "gem"],
["georg", "gg"],
["groß", "gr"],
["große", "gr"],
["großer", "gr"],
["großes", "gr"],
["grundschule", "gs"],
["gymnasium", "gym", "gymn"],
["hauptbahnhof", "hbf"],
["hauptschule", "hs"],
["hochschule", "hs"],
["in", "i"],
["johann", "joh"],
["johannes", "joh"],
["katholische", "kath"],
["katholischer", "kath"],
["katholisches", "kath"],
["katholisch", "kath"],
["kindergarten", "kiga"],
["kindertagesstätte", "kita"],
["klein", "kl"],
["kleine", "kl"],
["kleiner", "kl"],
["kleines", "kl"],
["krankenhaus", "kh", "krkh", "krh", "krhs"],
["obere", "ob"],
["oberer", "ob"],
["oberes", "ob"],
["platz", "pl"],
["realschule", "rs"],
["römisch", "röm"],
["samtgemeinde", "sg"],
["sankt", "st"],
["sebastian", "seb"],
["straße", "str"],
["und", "u","&"],
["universität", "uni"],
["unterer", "unt","u"],
["unteres", "unt","u"],
["untere", "unt","u"],
["unter", "u"],
["vom", "v"],
["von", "v"],
["weg", "wg"],
["zur", "z"],
["zum", "z"],
["zu", "z"],
# Swiss German
["strasse", "str"],
#other
["ulica","u"],
#compass
# of course, this is horribly english-specific...
# but how would one expand this in a sensible fashion to
# cover other languages?
["n", "north"],
["e", "east"],
["s", "south"],
["w", "west"],
# german
["n", "nord"],
["o", "ost"],
["s", "süd"],
]
#build substitution rules out of classes (plz do this only once per redactionbot-start)
@@rules = Hash.new(Set.new)
for clazz in classes
for elem in clazz
@@rules[elem] = @@rules[elem] | ((Set.new clazz) - (Set.new [elem]))
end
end
#special rules like kill spaces, dashes and dots
@@rules[' '] = @@rules[elem] | Set.new([' ', '', '-', '.', '. '])
@@rules['-'] = @@rules[elem] | Set.new(['-', ' ', ''])
@@rules['.'] = @@rules[elem] | Set.new(['.', ' ', ''])
# function for expanding a string into a list of strings
def self.manglenext(heap, manglerules, target)
if !heap.empty?()
#remove the best unvisited word from queue and mangle it
wordstart, wordend = heap.next!()
#call every rule
for rule in manglerules
#and try to use it
if wordend.size() < rule.size() # TODO: BENCHMARKTEST NEEDED
next
end
#execute rule (just split once!!)
newsplit = []
if rule == ' '
newsplit = wordend.split(/ /,2)
else
newsplit = wordend.split(rule,2)
end
# if rule doesn't apply len != 2
if newsplit.size() == 2
for substitute in @@rules[rule]
newwordstart = wordstart + newsplit[0] + substitute
newwordend = newsplit[1]
#everything in wordstart have to match targets first characters
if target.start_with?(newwordstart)
#if we found our string we're happy
if target == newwordstart + newwordend
return true
end
heap.push([newwordstart,newwordend],newwordstart.size())
#to avoid loops with insert space (and insert special rule ' ')
if rule != ' '
newwordspaceend = ' ' + newwordend
#if we found our string we're happy
if target == newwordstart + newwordspaceend
return true
end
heap.push([newwordstart,newwordspaceend],newwordstart.size()) # insert space
end
end
end
end
end
end
return false
end
# TODO: may need some work for internationalisation
def self.equal_expansions(a, b)
input1 = a.downcase() + ' '
input2 = b.downcase() + ' '
if input1 == input2
#shortcut if string a matches string b
return true
end
# filter rules? maybe if words are long enough / dont remove special rules
forwardrules = [] # TODO Benchmark needed
backwardrules = []
for rule in @@rules.keys()
if rule == ' ' or rule == '-' or rule == '.' or input1.include? rule
forwardrules.push(rule)
end
end
for rule in @@rules.keys()
if rule == ' ' or rule == '-' or rule == '.' or input2.include? rule
backwardrules.push(rule)
end
end
#init toextend (priorityqueue)
extendforwpq = Containers::PriorityQueue.new
extendforwpq.push(['',input1],0)
extendbackwpq = Containers::PriorityQueue.new
extendbackwpq.push(['',input2],0)
until extendforwpq.empty?() and extendbackwpq.empty?()
if manglenext(extendforwpq, forwardrules, input2)
return true
end
if manglenext(extendbackwpq, backwardrules, input1)
return true
end
end
return false
end
end