Class: Ubi::Memoria::Address

Inherits:
Base
  • Object
show all
Defined in:
lib/ubi/memorias/address.rb

Overview

An address in this world

Ignorace < Bliss

Yeah, this needs lots of work. Geonames in memory country with zip/cities and regions, and some neural thing. And geocode to use openstreet or else.

Don’t hesitate to improve your AI skills here.

Constant Summary collapse

DIVIDERS =
%r{[,\-\|/]}
SPLIT =
/(?<=\D)#{DIVIDERS}|#{DIVIDERS}(?=\D)/
REGEXES =
{
  br: {
    prefix: %w( r rua av avenida pc pca praça pc pca praca tv travessa est estrada rod rodovia ),
    number: %w( n no  num numero km ),
    ext: %w( comp obs ap apto apart apartamento andar ),
    zip: /\d{5}\s?[-]\s?\d{3}/
  },
  us: {
    prefix: %w( st street av avenue road ),
    zip: /\d{5}/
  }
}

Instance Attribute Summary collapse

Attributes inherited from Base

#hint, #opts, #text

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#==, extract_text, inherited, #initialize, key, name, parse, parse!, #to_s

Constructor Details

This class inherits a constructor from Ubi::Memoria::Base

Instance Attribute Details

#cityObject

Returns the value of attribute city.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def city
  @city
end

#cleanObject

Returns the value of attribute clean.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def clean
  @clean
end

#extraObject

Returns the value of attribute extra.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def extra
  @extra
end

#nameObject

Returns the value of attribute name.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def name
  @name
end

#nationObject

Returns the value of attribute nation.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def nation
  @nation
end

#numberObject

Returns the value of attribute number.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def number
  @number
end

#partsObject

Returns the value of attribute parts.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def parts
  @parts
end

#placeObject

Returns the value of attribute place.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def place
  @place
end

#regionObject

Returns the value of attribute region.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def region
  @region
end

#wordsObject

Returns the value of attribute words.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def words
  @words
end

#zipObject

Returns the value of attribute zip.



29
30
31
# File 'lib/ubi/memorias/address.rb', line 29

def zip
  @zip
end

Class Method Details

.formatsObject



74
75
76
77
78
79
80
# File 'lib/ubi/memorias/address.rb', line 74

def formats
  {
    # br: '%a, %n - %c %z %r',
    # br: '%a, %n - %c %z %r',
    br: '%a, %n - %c %z %r'
  }
end

.pluralObject



92
93
94
# File 'lib/ubi/memorias/address.rb', line 92

def plural
  :addresses
end

.regex(hint) ⇒ Object



88
89
90
# File 'lib/ubi/memorias/address.rb', line 88

def regex(hint)
  /(\b(?:#{REGEXES[hint][:prefix].join('|')})\s.*)\b/i
end

.sanitize(txt) ⇒ Object

Sanitizing

“..” -> “.” “n” -> “-” “ -” -> “-”



68
69
70
71
72
# File 'lib/ubi/memorias/address.rb', line 68

def sanitize(txt)
  v = ActiveSupport::Inflector.transliterate(txt)
  v.gsub(/\s+/, ' ').gsub(/\\n/, '-')
    .gsub(/\s?(#{DIVIDERS})\s?/, '\1')
end

.zip_formatObject



82
83
84
85
86
# File 'lib/ubi/memorias/address.rb', line 82

def zip_format
  {
    br: [/(\d{5})(\d{3})/, '\1-\2']
  }
end

Instance Method Details

#fetch_possibleObject



39
40
41
42
43
44
# File 'lib/ubi/memorias/address.rb', line 39

def fetch_possible
  parse_zip
  @region = clean.scan(/\W([A-Z]{2})\W/).first
  @region = @region.first if @region
  @number = clean.scan(/\d+/).join(' ')
end

#format(location = :br) ⇒ Object



56
57
58
# File 'lib/ubi/memorias/address.rb', line 56

def format(location = :br)
  text #.sub(*self.class.formats[location])
end

#parse_zipObject



32
33
34
35
36
37
# File 'lib/ubi/memorias/address.rb', line 32

def parse_zip
  @zip = text.scan(REGEXES[:br][:zip]).first
  return unless zip
  @zip = zip.gsub(/\D/, '').sub(*Address.zip_format[:br])
  clean.slice!(zip)
end

#parserObject

Init, remove non word chars



49
50
51
52
53
54
# File 'lib/ubi/memorias/address.rb', line 49

def parser
  @clean = Address.sanitize(text)
  @parts = clean.split(SPLIT).map { |v| v.strip.chomp }
  @words = parts.map { |pt| pt.split(/\s+/) }
  fetch_possible
end