Permalink
Browse files

Fixed problem with removing city name from street when already parsed…

… values given
  • Loading branch information...
1 parent 1d78285 commit 1c2c3963f136fbc726bb726ba2c0aa7d2c33adc5 Semprebon committed May 17, 2012
Showing with 11 additions and 7 deletions.
  1. +7 −5 lib/geocoder/us/address.rb
  2. +2 −2 lib/geocoder/us/database.rb
  3. +2 −0 test/database.rb
View
@@ -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,15 +37,15 @@ 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, " ")
end
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?
@@ -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?
View
@@ -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

0 comments on commit 1c2c396

Please sign in to comment.