module Fias::Name::Synonyms

Constants

ANNIVESARY_FORMS
IN_BRACKETS
NUMERAL_SUFFIXES
OPTIONAL

Public Class Methods

expand(name) click to toggle source
# File lib/fias/name/synonyms.rb, line 5
def expand(name)
  Split
    .split(name)
    .map { |token| Array.wrap(tokenize(name, token)) }
end
forms(name) click to toggle source
# File lib/fias/name/synonyms.rb, line 15
def forms(name)
  recombine(expand(name))
end
tokens(name) click to toggle source
# File lib/fias/name/synonyms.rb, line 11
def tokens(name)
  expand(name).flatten.uniq
end

Private Class Methods

annivesary(token) click to toggle source
# File lib/fias/name/synonyms.rb, line 51
def annivesary(token)
  return unless token =~ Fias::ANNIVESARIES

  ANNIVESARY_FORMS.map do |form|
    token.gsub(Fias::ANNIVESARIES, form)
  end
end
bracketed(name, token) click to toggle source
# File lib/fias/name/synonyms.rb, line 35
def bracketed(name, token)
  match = name.match(IN_BRACKETS)
  [token, OPTIONAL] if match && match[1].include?(token)
end
initials(token) click to toggle source
# File lib/fias/name/synonyms.rb, line 44
def initials(token)
  return unless
    (Fias::INITIALS =~ token) && (Fias::SINGLE_INITIAL =~ token)

  [token, OPTIONAL]
end
numerals(token) click to toggle source
# File lib/fias/name/synonyms.rb, line 59
def numerals(token)
  return unless (/^\d+/ =~ token) || (Fias::ANNIVESARIES =~ token)
  numerals_for(token)
end
numerals_for(numeral) click to toggle source
# File lib/fias/name/synonyms.rb, line 64
def numerals_for(numeral)
  n = numeral.gsub(/[^\d]/, '')

  suffixes =
    NUMERAL_SUFFIXES.map do |suffix|
      ["#{n}#{suffix}", "#{n}-#{suffix}"]
    end

  suffixes.flatten + [n]
end
proper_names(token) click to toggle source
# File lib/fias/name/synonyms.rb, line 40
def proper_names(token)
  [token, OPTIONAL] if Fias.config.proper_names.include?(token)
end
recombine(variants) click to toggle source
# File lib/fias/name/synonyms.rb, line 75
def recombine(variants)
  return variants if variants.empty?
  head, *rest = variants

  forms = head.product(*rest)
  forms
    .map { |variant| variant.reject(&:blank?).sort.join(' ') }
    .flatten
end
synonyms(token) click to toggle source
# File lib/fias/name/synonyms.rb, line 31
def synonyms(token)
  Fias.config.synonyms_index[token]
end
tokenize(name, token) click to toggle source
# File lib/fias/name/synonyms.rb, line 21
def tokenize(name, token)
  synonyms(token)          ||
    bracketed(name, token) ||
    proper_names(token)    ||
    initials(token)        ||
    annivesary(token)      ||
    numerals(token)        ||
    token
end