Created
February 6, 2017 17:24
-
-
Save n7st/313298956ccb2f6db822eb19b28be404 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class AddressMatcher | |
require 'yaml' | |
def initialize(addresses) | |
@addresses = addresses | |
@postcodes = {} | |
@matches = [] | |
end | |
def sanitize | |
@addresses.each do |address| | |
postcode = sanitize_postcode(address[:postcode]) | |
thoroughfare = sanitize_thoroughfare(address[:thoroughfare]) | |
building = sanitize_building(address[:building]) | |
if address[:sub_prem] | |
sub_prem = sanitize_building(address[:sub_prem]) | |
end | |
unless @postcodes[postcode] | |
@postcodes[postcode] = {} | |
end | |
unless @postcodes[postcode][thoroughfare] | |
@postcodes[postcode][thoroughfare] = {} | |
end | |
unless @postcodes[postcode][thoroughfare][building] | |
@postcodes[postcode][thoroughfare][building] = { | |
:sub_prem => {}, | |
:property => [], | |
} | |
end | |
if sub_prem | |
unless @postcodes[postcode][thoroughfare][building][:sub_prem][sub_prem] | |
@postcodes[postcode][thoroughfare][building][:sub_prem][sub_prem] = [] | |
end | |
@postcodes[postcode][thoroughfare][building][:sub_prem][sub_prem].push({ | |
:id => address[:id] | |
}) | |
else | |
@postcodes[postcode][thoroughfare][building][:property].push({ | |
:id => address[:id] | |
}) | |
end | |
end | |
end | |
def locate | |
@postcodes.each do |postcode, p_v| | |
p_v.each do |street, s_v| | |
s_v.each do |num, n_v| | |
if n_v[:property].count > 1 | |
@matches.push(n_v[:property]); | |
end | |
n_v[:sub_prem].each do |sp, sp_v| | |
if sp_v.count > 1 | |
@matches.push(sp_v) | |
end | |
end | |
end | |
end | |
end | |
end | |
def inspect(var_name) | |
puts instance_variable_get(var_name).to_yaml | |
end | |
private | |
def sanitize_postcode(p) | |
p = p.delete(' ') | |
return p | |
end | |
def sanitize_thoroughfare(t) | |
t = t.delete('.') | |
t = t.sub(/(rd|road|st(reet)?)/i, '') | |
t = t.gsub(/\w+/, &:capitalize) | |
return t.strip() | |
end | |
def sanitize_building(b) | |
if b =~ /^[\d]+$/ | |
b = sanitize_building_n(b) | |
elsif b =~ /[\w]+/ | |
b = sanitize_building_s(b) | |
end | |
return b | |
end | |
def sanitize_building_s(s) | |
s = s.sub(/(house|towers?)/i, '') | |
s = s.gsub(/\w+/, &:capitalize) | |
return s.strip() | |
end | |
def sanitize_building_n(n) | |
return n | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment