class Ubi::Memoria::Address

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.

Constants

DIVIDERS
REGEXES
SPLIT

Attributes

city[RW]
clean[RW]
extra[RW]
name[RW]
nation[RW]
number[RW]
parts[RW]
place[RW]
region[RW]
words[RW]
zip[RW]

Public Class Methods

formats() click to toggle source
# 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
plural() click to toggle source
# File lib/ubi/memorias/address.rb, line 92
def plural
  :addresses
end
regex(hint) click to toggle source
# File lib/ubi/memorias/address.rb, line 88
def regex(hint)
  /(\b(?:#{REGEXES[hint][:prefix].join('|')})\s.*)\b/i
end
sanitize(txt) click to toggle source

Sanitizing

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

# 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_format() click to toggle source
# File lib/ubi/memorias/address.rb, line 82
def zip_format
  {
    br: [/(\d{5})(\d{3})/, '\1-\2']
  }
end

Public Instance Methods

fetch_possible() click to toggle source
# 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) click to toggle source
# File lib/ubi/memorias/address.rb, line 56
def format(location = :br)
  text #.sub(*self.class.formats[location])
end
parse_zip() click to toggle source
# 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
parser() click to toggle source

Init, remove non word chars

# 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