forked from roberdam/Xaddress
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecode_func.rb
More file actions
578 lines (522 loc) · 14.8 KB
/
decode_func.rb
File metadata and controls
578 lines (522 loc) · 14.8 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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
def check_arguments(argumentos)
# If empty
if argumentos[0].nil? then
puts " "
puts "ruby decode.rb 'WORD WORD NUMBER - (STATE),COUNTRY' "
puts "ruby decode.rb 'NUMBER WORD WORD - (STATE),COUNTRY' "
puts "***************************************************************"
puts "Example: ruby decode.rb '7150 MAGICAL PEARL - Maluku, Indonesia' "
puts "Example: ruby decode.rb 'RENEGADO SABIO 0989 - Rwanda' "
puts "Example: ruby decode.rb 'LIBRES 2956 - Asuncion, Paraguay' "
puts "*********************************************************************"
exit
end
# Check formatting
if argumentos[0].split("-")[1].nil? then
puts "Incorrect format, use : 'WORD WORD NUMBER - (STATE),COUNTRY'"
puts "Separate the address from the country and state with a - "
puts "or XXXXXXX-XXXXXXX for short addresses"
exit
end
if argumentos[0].split("-")[0].strip.length < 8 and argumentos[0].split("-")[1].strip.length < 8 then
short_decode(argumentos[0])
exit
end
direccion = argumentos[0].split("-")[0].split(" ")
if direccion.count > 3 or direccion.count ==1 then
puts "Address must have this format :"
puts "WORD WORD NUMBER"
puts "WORD NUMBER"
puts "NUMBER WORD WORD"
puts "NUMBER WORD"
exit
end
okey="no"
cuantos = 0
$numero=""
direccion.each do |dire|
unless (dire =~ /\A[-+]?[0-9]*\.?[0-9]+\Z/).nil?
cuantos= cuantos+1
if dire.to_i <10000 then
okey="si"
$numero=dire
if dire.to_i<1000 then
$numero="0"+dire.to_i.to_s.strip
end
if dire.to_i<100 then
$numero="00"+dire.to_i.to_s.strip
end
if dire.to_i<10 then
$numero="000"+dire.to_i.to_s.strip
end
end
end
end
if okey=="no" then
puts "number in address must be < 9999"
exit
end
if cuantos>1 then
puts "incorrect address format, use only one number"
exit
end
ubicacion= ARGV[0].split("-")[1].strip
pais = controla_pais(ubicacion)
$pais= pais[0]
$palabras=[]
direccion.each do |pala|
unless pala==$numero
$palabras << cambia(pala).upcase
end
end
if $no_states[pais[0]].nil? then
$estados = CSV.read("states.csv", :headers => true )
states=[]
$estados.each do |guarda|
if guarda["countryCode"]==pais[0] then
states << guarda
end
end
return states
else
return pais
end
end
def controla_states(argumento)
argum = argumento[0].dup
xestado = cambia(argum.split("-")[1].split(",")[0]).strip
estado=""
$datospais.each do |recorre|
if recorre["stateName1"].upcase==xestado.upcase or recorre["stateName2"].upcase==xestado.upcase or recorre["stateName3"].upcase==xestado.upcase or recorre["googleName"].upcase==xestado.upcase then
estado = recorre
end
end
if estado=="" then
opciones=[]
$datospais.each do |recorre|
if recorre["stateName1"].upcase.include? xestado.upcase or recorre["stateName2"].include? xestado.upcase or recorre["stateName3"].include? xestado.upcase or recorre["googleName"].include? xestado.upcase then
opciones << recorre
end
end
if opciones.count>0 then
puts "State not found, you can try:"
puts "#{argum.split("-")[0]} - #{opciones[0]["stateName1"]}, #{argum.split("-")[1].split(",")[1].strip}"
exit
else
puts "State not found, you can try this options:"
$datospais.each do |recorre|
puts "#{argum.split("-")[0]} - #{recorre["stateName2"]}, #{$pais_sp[$pais]}"
end
exit
end
end
return estado
end
def controla_pais(argumento)
argum = argumento.dup
ubicacion = argum.split(",")
pais =""
if ubicacion.count ==1 then
pais= cambia(ubicacion[0]).strip
else
pais=cambia(ubicacion.last).strip
end
xpais= ""
if pais.length==2 then
$paises.each do |busca|
if busca["countryCode"]==pais.upcase then
xpais = busca
end
end
else
$paises.each do |busca|
if (busca["countryName"].upcase==pais.upcase) or (busca["Nombre"].upcase==pais.upcase) then
xpais = busca
end
end
end
xoptions=[]
if xpais=="" then
$paises.each do |busca|
if (busca["countryName"].upcase.include? pais.upcase) or (busca["Nombre"].upcase.include? pais.upcase) then
xoptions << busca
end
end
if ubicacion.count>1 then
anterior = " "+ubicacion[0]+","
else
anterior = ""
end
puts "Country not fond, You can try this instead:"
xoptions.each do |opci|
puts "#{ARGV[0].split("-")[0]}-#{anterior} #{opci["Nombre"]}"
end
exit
else
return xpais
end
end
def cambia (texto)
@retorno = texto
@retorno.gsub!("Á", "A")
@retorno.gsub!("á", "a")
@retorno.gsub!("ã", "a")
@retorno.gsub!("à", "a")
@retorno.gsub!("ä", "a")
@retorno.gsub!("ā", "a")
@retorno.gsub!("ă", "a")
@retorno.gsub!("ą", "a")
@retorno.gsub!("å", "a")
@retorno.gsub!("â", "a")
@retorno.gsub!("Ā", "A")
@retorno.gsub!("ǝ", "a")
@retorno.gsub!("ầ", "a")
@retorno.gsub!("ả", "a")
@retorno.gsub!("ẵ", "a")
@retorno.gsub!("ậ", "a")
@retorno.gsub!("ạ", "a")
@retorno.gsub!("ắ", "a")
@retorno.gsub!("ə", "a")
@retorno.gsub!("ằ", "a")
@retorno.gsub!("É", "E")
@retorno.gsub!("é", "e")
@retorno.gsub!("ë", "e")
@retorno.gsub!("ě", "e")
@retorno.gsub!("ē", "e")
@retorno.gsub!("ė", "e")
@retorno.gsub!("Ē", "E")
@retorno.gsub!("ę", "e")
@retorno.gsub!("è", "e")
@retorno.gsub!("ê", "e")
@retorno.gsub!("ế", "e")
@retorno.gsub!("ệ", "e")
@retorno.gsub!("ề", "e")
@retorno.gsub!("Í", "I")
@retorno.gsub!("í", "i")
@retorno.gsub!("Î", "I")
@retorno.gsub!("ī", "i")
@retorno.gsub!("î", "i")
@retorno.gsub!("ï", "i")
@retorno.gsub!("ĭ", "i")
@retorno.gsub!("ï", "i")
@retorno.gsub!("ı", "i")
@retorno.gsub!("İ", "I")
@retorno.gsub!("Ī", "I")
@retorno.gsub!("ị", "i")
@retorno.gsub!("ĩ", "i")
@retorno.gsub!("ì", "i")
@retorno.gsub!("ỉ", "i")
@retorno.gsub!("Ó", "O")
@retorno.gsub!("ó", "o")
@retorno.gsub!("ô", "o")
@retorno.gsub!("ö", "o")
@retorno.gsub!("õ", "o")
@retorno.gsub!("ð", "o")
@retorno.gsub!("ø", "o")
@retorno.gsub!("ō", "o")
@retorno.gsub!("Ö", "O")
@retorno.gsub!("Ō", "O")
@retorno.gsub!("ŏ", "o")
@retorno.gsub!("ơ", "o")
@retorno.gsub!("ộ", "o")
@retorno.gsub!("ò", "o")
@retorno.gsub!("Ŏ", "O")
@retorno.gsub!("ő", "o")
@retorno.gsub!("Ø", "O")
@retorno.gsub!("ớ", "o")
@retorno.gsub!("ồ", "o")
@retorno.gsub!("ọ", "o")
@retorno.gsub!("Ú", "U")
@retorno.gsub!("ú", "u")
@retorno.gsub!("ü", "u")
@retorno.gsub!("ŭ", "u")
@retorno.gsub!("ū", "u")
@retorno.gsub!("ų", "u")
@retorno.gsub!("ũ", "u")
@retorno.gsub!("ư", "u")
@retorno.gsub!("ừ", "u")
# @retorno.gsub!("ñ", "n")
# @retorno.gsub!("Ñ", "n")
@retorno.gsub!("ň", "n")
@retorno.gsub!("ņ", "n")
@retorno.gsub!("ń", "n")
@retorno.gsub!("ç", "c")
@retorno.gsub!("č", "c")
@retorno.gsub!("Č", "C")
@retorno.gsub!("ċ", "c")
@retorno.gsub!("Ç", "C")
@retorno.gsub!("ć", "c")
@retorno.gsub!("Ž", "Z")
@retorno.gsub!("ž", "z")
@retorno.gsub!("ż", "z")
@retorno.gsub!("Ż", "Z")
@retorno.gsub!("Z̧", "Z")
@retorno.gsub!("z̧", "z")
@retorno.gsub!("z̄", "z")
@retorno.gsub!("š", "s")
@retorno.gsub!("Š", "S")
@retorno.gsub!("ş", "s")
@retorno.gsub!("Ş", "S")
@retorno.gsub!("ś", "s")
@retorno.gsub!("Ś", "S")
@retorno.gsub!("ķ", "k")
@retorno.gsub!("Ķ", "K")
@retorno.gsub!("đ", "d")
@retorno.gsub!("ḑ", "d")
@retorno.gsub!("ř", "r")
@retorno.gsub!("ý", "y")
@retorno.gsub!("æ", "e")
@retorno.gsub!("ļ", "l")
@retorno.gsub!("ġ", "g")
@retorno.gsub!("ğ", "g")
@retorno.gsub!("ħ", "h")
@retorno.gsub!("ḩ", "h")
@retorno.gsub!("Ħ", "H")
@retorno.gsub!("ţ", "t")
@retorno.gsub!("ł", "l")
@retorno.gsub!("Ł", "L")
@retorno.gsub!("Ḩ", "H")
@retorno.gsub!("Ḑ", "D")
@retorno.gsub!("Đ", "D")
@retorno.gsub!("ț", "t")
@retorno.gsub!("Ð", "D")
return @retorno
end
def codigo(palabra)
texto=palabra
a=texto[0].ord.to_f
ope=0
if texto.length>1 then
for i in 1..texto.length-1
if ope==0 then
a=a / texto[i].ord
ope=1
else
a=a * texto[i].ord
ope=0
end
end
largo = (a - a.to_i).to_s.split(".")[1][0..2].to_i
completo = (a - a.to_i).to_s.split(".")[1]
long= completo.length
return completo[0..3]
end
end
def codilalo(latini,latfin,lonini,lonfin)
# Example: codilalo("-25","-27","-55","-57")
$laat = (-90..90).to_a.collect{|i| i.to_s}.insert(90, "-0")
$loon = (-180..180).to_a.collect{|i| i.to_s}.insert(180, "-0")
$looni = ((-180..0).to_a.collect{|i| i.to_s}.reverse + (0..180).to_a.collect{|i| i.to_s}.reverse)
$looni[0]="-0"
lai = $laat.index(latini)
laf = $laat.index(latfin)
exept=0
if (lonini.to_i < 0) and (lonfin.to_i >= 0) then
loi = $looni.index(lonini)
lof = $looni.index(lonfin)
exept=1
else
loi = $loon.index(lonini)
lof = $loon.index(lonfin)
end
if lai < laf then
latsecu = $laat[lai, (laf-lai)+1 ]
else
latsecu = $laat[laf, (lai-laf)+1 ]
end
if exept==0 then
if loi < lof then
lonsecu = $loon[loi,(lof-loi)+1]
else
lonsecu = $loon[lof,(loi-lof)+1]
end
else
if loi < lof then
lonsecu = $looni[loi,(lof-loi)+1]
else
lonsecu = $looni[lof,(loi-lof)+1]
end
end
$tablalalo=[]
latsecu.each do |x|
lonsecu.each do |y|
guarda = x+","+y
$tablalalo << guarda
end
end
return $tablalalo[($numadjetivo.to_i-1)]
end
def codigo_imagen(texto)
a=texto[0].ord.to_f
if texto.length>1 then
opera = 1
for i in 1..texto.length-1
if opera==1 then
a=a / texto[i].ord
end
if opera==2 then
a=a * texto[i].ord
end
if opera==3 then
a=a + texto[i].ord
end
opera = opera +1
if opera==4 then
opera=1
end
end
end
long = a.to_s.split(".")[1].length
return a.to_s.split(".")[1][long-4..long-1]
end
def latlo(texto,tipo)
if tipo==1 then
if $latp.index texto then
retorno =($latp.index texto)
if retorno>90 then
abort ("Incorrect latitude >90")
end
return retorno
else
if $latn.index texto then
retorno =($latn.index texto)
if retorno>90 then
abort ("Incorrect latitude >90")
end
if retorno == 0 then
return ("-0")
end
return (retorno *-1)
else
abort ("Latitud incorrecta 4")
end
end
else
if $latp.index texto then
retorno =($latp.index texto)
if retorno>180 then
abort ("Incorrect Longitude >180")
end
return retorno
else
if $latn.index texto then
retorno =($latn.index texto)
if retorno>180 then
abort ("Incorrect Longitude")
end
if retorno == 0 then
return ("-0")
end
return (retorno *-1)
else
abort ("Incorrect Longitude")
end
end
end
end
def minseg(texto)
if $latlongms.index texto then
retorno = ($latlongms.index texto)
return retorno.to_s.rjust(3,'0')
else
abort ("Incorrect segment")
end
end
def segu(texto)
if $one.index texto then
retorno = ($one.index texto)
return retorno
else
abort ("Incorrect Segment")
end
end
def decode(texto,which)
case texto.strip.length
when 4
if (texto[0].upcase.delete ("DGJKLMNRST")).length ==0 then
lat=latlo(texto[1..3],which)
segu=segu(texto[0])
return (lat.to_s+"."+segu.to_s)
else
abort ("Segment #{which} incorrect")
end
when 5
abort ("Segment #{which} incorrect")
when 6
lat=latlo(texto[3..5].strip,which)
minu=minseg(texto[0..2].strip)
return (lat.to_s+"."+minu.to_s)
when 7
if (texto[6].upcase.delete ("DGJKLMNRST")).length ==0 then
se= segu(texto[6])
lat=latlo(texto[3..5].strip,which)
minu=minseg(texto[0..2].strip)
minu=minu.to_s+se.to_s
return (lat.to_s+"."+minu.to_s)
else
abort ("Segment #{which} incorrect")
end
when 8
abort ("Segment #{which} incorrect")
when 9
lat=latlo(texto[3..5].strip,which)
minu=minseg(texto[0..2].strip)
minu2= minseg(texto[6..8].strip)
return (lat.to_s+"."+minu.to_s+minu2.to_s)
end
end
def short_decode(codigo)
temp = codigo.dup
parts = temp.upcase.split("-")
if parts[0].strip.length < 3 then
abort ("ERROR: Wrong format on first part of short address")
end
if parts[1].strip.length < 3 then
abort ("ERROR: Wrong format on second part of short address")
end
latlong=[]
$latlongms=[]
$latp=[]
$latn=[]
latfinal=""
lonfinal=""
consonantsplit ="BCDFGJKLMNPRSTZ".split ("")
consonants ="BCDFGJKLMNPRSTZ"
#We exclude "HQVWXY" from consonants & consonantsplit
vocals = "AEIOU"
abc = "ABCDEFGIJKLMNOPRSTUVXZ".split ("")
#We exclude "HQWY" from abc
$one="DGJKLMNRST".split ("")
# Create latitude & longitude arrays.
for i in 0..4
for z in 0..4
abc.each do |x|
result= vocals[i]+x+vocals[z]
unless result.delete("AEIOU")==""
# We discard the all vocals combinations, Ex: AAA, AEA, etc.
latlong << result
end
end
end
end
# Store positive & negative values
(0...362).step(2) do |i|
$latp << latlong[i]
$latn << latlong[i+1]
end
#Create arrays of minutes & seconds.
consonantsplit.each do |x|
for i in 0..consonants.size-1
for z in 0..vocals.size-1
result= x+vocals[z]+consonants[i]
$latlongms << result
end
end
end
#--------------------------------
firstpart=parts[0].strip
secondpart=parts[1].strip
la = decode(firstpart,1)
lo = decode(secondpart,2)
puts "#{la},#{lo}"
end