class Fias::Query::Estimate

Constants

RATES

Public Class Methods

new(params, chain) click to toggle source
# File lib/fias/query/estimate.rb, line 4
def initialize(params, chain)
  @params = params
  @chain = chain
end

Public Instance Methods

estimate() click to toggle source
# File lib/fias/query/estimate.rb, line 9
def estimate
  for_subject +
    for_found_parts +
    for_type +
    for_deepness +
    for_name_proximity
end

Private Instance Methods

chain_by_key() click to toggle source
# File lib/fias/query/estimate.rb, line 54
def chain_by_key
  @chain_by_key ||= @chain.index_by { |item| item[:key] }
end
for_deepness() click to toggle source
# File lib/fias/query/estimate.rb, line 40
def for_deepness
  @chain.first[:ancestry].size * RATES[:deep]
end
for_found_parts() click to toggle source
# File lib/fias/query/estimate.rb, line 24
def for_found_parts
  @chain.size * RATES[:found_part]
end
for_name_proximity() click to toggle source
# File lib/fias/query/estimate.rb, line 44
def for_name_proximity
  @params.synonyms.sum do |key, (expected, _)|
    given = chain_by_key[key].try(:[], :tokens) || []
    expected = expected.flatten.uniq

    proximity = (given & expected).size
    proximity * RATES[:name]
  end
end
for_subject() click to toggle source
# File lib/fias/query/estimate.rb, line 19
def for_subject
  expected_type = @params.sanitized.keys.first
  expected_type == @chain.first[:key] ? RATES[:subject] : 0
end
for_type() click to toggle source
# File lib/fias/query/estimate.rb, line 28
def for_type
  @params.sanitized.sum do |key, (_, *expected_status)|
    received_status = chain_by_key[key].try(:[], :abbr)

    status_found =
      expected_status.present? &&
      expected_status.include?(received_status)

    status_found ? RATES[:type] : 0
  end
end