From 1c2c3963f136fbc726bb726ba2c0aa7d2c33adc5 Mon Sep 17 00:00:00 2001 From: Semprebon Date: Thu, 17 May 2012 14:59:41 -0400 Subject: [PATCH] Fixed problem with removing city name from street when already parsed values given --- lib/geocoder/us/address.rb | 12 +++++++----- lib/geocoder/us/database.rb | 4 ++-- test/database.rb | 2 ++ 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/geocoder/us/address.rb b/lib/geocoder/us/address.rb index 23dbf1c..750c509 100644 --- a/lib/geocoder/us/address.rb +++ b/lib/geocoder/us/address.rb @@ -25,7 +25,7 @@ class Address attr_accessor :zip, :plus4 # Takes an address or place name string as its sole argument. - def initialize (text) + def initialize(text) raise ArgumentError, "no text provided" unless text and !text.empty? if text.class == Hash @text = "" @@ -37,7 +37,7 @@ def initialize (text) end # Removes any characters that aren't strictly part of an address string. - def clean (value) + def clean(value) value.strip \ .gsub(/[^a-z0-9 ,'&@\/-]+/io, "") \ .gsub(/\s+/o, " ") @@ -45,7 +45,7 @@ def clean (value) def assign_text_to_address(text) - if !text[:address].nil? + if text[:address] @text = clean text[:address] parse else @@ -73,7 +73,6 @@ def assign_text_to_address(text) @city = [] if !text[:city].nil? @city.push(text[:city]) - @text = text[:city].to_s else @city.push("") end @@ -273,7 +272,10 @@ def city= (strings) # Broome, MT or what) strings = expand_streets(strings) # fix for "Mountain View" -> "Mountain Vw" match = Regexp.new('\s*\b(?:' + strings.join("|") + ')\b\s*$', Regexp::IGNORECASE) - @street = @street.map {|string| string.gsub(match, '')}.select {|s|!s.empty?} + # only remove city from street strings if address was parsed + unless @text == "" + @street = @street.map {|string| string.gsub(match, '')}.select {|s|!s.empty?} + end end def po_box? diff --git a/lib/geocoder/us/database.rb b/lib/geocoder/us/database.rb index 9b726db..9cfca23 100644 --- a/lib/geocoder/us/database.rb +++ b/lib/geocoder/us/database.rb @@ -365,12 +365,12 @@ def find_candidates (address) places = places_by_city city, address.city_parts, address.state if places.empty? return [] if places.empty? + # setting city will remove city from street, so save off before address.city = unique_values places, :city return places if address.street.empty? - + zips = unique_values places, :zip street = address.street.sort {|a,b|a.length <=> b.length}[0] - # puts "street parts = #{address.street_parts.inspect}" candidates = features_by_street_and_zip street, address.street_parts, zips if candidates.empty? diff --git a/test/database.rb b/test/database.rb index b11e249..93afec4 100644 --- a/test/database.rb +++ b/test/database.rb @@ -85,6 +85,8 @@ def test_place def test_sample return if @db.nil? + # This test won't run properly on 1.8.7 or lower (?) - APS + return if RUBY_VERSION.split(".")[1] <= '8' CSV.foreach(Base + "/data/db-test.csv", {:headers=>true}) do |row| result = @db.geocode(row[0], true) result[0][:count] = result.map{|a|[a[:lat], a[:lon]]}.to_set.length