module Fias::Name::HouseNumber

Constants

COLON
ENDS_WITH_NUMBER
HOUSE_WORD
HOUSE_WORDS
HOUSING_WORDS
JUST_A_NUMBER
LINE_OR_MICRODISTRICT
NUMBER
NUMBER_WITH_HOUSING
STOPWORDS

Public Class Methods

extract(name) click to toggle source
# File lib/fias/name/house_number.rb, line 5
def extract(name)
  return [name, nil] unless contains_number?(name)

  name, number =
    try_split_by_colon(name)   ||
    try_housing(name)          ||
    try_house_word(name)       ||
    try_ends_with_number(name)

  [name.strip, number.strip]
end

Private Class Methods

contains_number?(name) click to toggle source
# File lib/fias/name/house_number.rb, line 19
def contains_number?(name)
  !(name =~ JUST_A_NUMBER) && !(name =~ LINE_OR_MICRODISTRICT) &&
    (
      name =~ COLON ||
      name =~ ENDS_WITH_NUMBER ||
      name =~ HOUSE_WORD ||
      name =~ NUMBER_WITH_HOUSING
    )
end
or_words(words) click to toggle source
# File lib/fias/name/house_number.rb, line 48
def or_words(words)
  words
    .sort_by(&:length)
    .reverse
    .map { |w| Regexp.escape(w) }
    .join('|')
end
try_ends_with_number(name) click to toggle source
# File lib/fias/name/house_number.rb, line 43
def try_ends_with_number(name)
  match = name.match(ENDS_WITH_NUMBER)
  [match.pre_match, match[1]] if match
end
try_house_word(name) click to toggle source
# File lib/fias/name/house_number.rb, line 38
def try_house_word(name)
  match = name.match(HOUSE_WORD)
  [match.pre_match, match.post_match] if match
end
try_housing(name) click to toggle source
# File lib/fias/name/house_number.rb, line 33
def try_housing(name)
  match = name.match(NUMBER_WITH_HOUSING)
  [match.pre_match, "#{match} #{match.post_match}"] if match
end
try_split_by_colon(name) click to toggle source
# File lib/fias/name/house_number.rb, line 29
def try_split_by_colon(name)
  name.split(/\s*,\s*/, 2) if name =~ COLON
end