Skip to content

Instantly share code, notes, and snippets.

@iamcarrico
Last active August 14, 2017 15:27
Show Gist options
  • Save iamcarrico/bc32e546b3a08f77946fecf39c154ba5 to your computer and use it in GitHub Desktop.
Save iamcarrico/bc32e546b3a08f77946fecf39c154ba5 to your computer and use it in GitHub Desktop.
A little converter for the pieces I need to change a csv of addresses to their long-form
addresses.csv
export.csv
require 'csv'
input_file = "addresses.csv"
output_file = "export.csv"
word_replacements = [
# States
["AK", "Alaska"],
["AL", "Alabama"],
["AR", "Arkansas"],
["AS", "American Samoa"],
["AZ", "Arizona"],
["CA", "California"],
["CO", "Colorado"],
["CT", "Connecticut"],
["DC", "District of Columbia"],
["DE", "Delaware"],
["FL", "Florida"],
["GA", "Georgia"],
["GU", "Guam"],
["HI", "Hawaii"],
["IA", "Iowa"],
["ID", "Idaho"],
["IL", "Illinois"],
["IN", "Indiana"],
["KS", "Kansas"],
["KY", "Kentucky"],
["LA", "Louisiana"],
["MA", "Massachusetts"],
["MD", "Maryland"],
["ME", "Maine"],
["MI", "Michigan"],
["MN", "Minnesota"],
["MO", "Missouri"],
["MS", "Mississippi"],
["MT", "Montana"],
["NC", "North Carolina"],
["ND", "North Dakota"],
["NE", "Nebraska"],
["NH", "New Hampshire"],
["NJ", "New Jersey"],
["NM", "New Mexico"],
["NV", "Nevada"],
["NY", "New York"],
["OH", "Ohio"],
["OK", "Oklahoma"],
["OR", "Oregon"],
["PA", "Pennsylvania"],
["PR", "Puerto Rico"],
["RI", "Rhode Island"],
["SC", "South Carolina"],
["SD", "South Dakota"],
["TN", "Tennessee"],
["TX", "Texas"],
["UT", "Utah"],
["VA", "Virginia"],
["VI", "Virgin Islands"],
["VT", "Vermont"],
["WA", "Washington"],
["WI", "Wisconsin"],
["WV", "West Virginia"],
["WY", "Wyoming"],
# Cardinal directions
["W", "West"],
["E", "East"],
["N", "North"],
["S", "South"],
["SE", "Southeast"],
["SW", "Southwest"],
["NE", "Northeast"],
["NW", "Northwest"],
# Street things
["DR", "Drive"],
["LN", "Lane"],
["ST", "Street"],
["CT", "Court"],
["PL", "Place"],
["AVE", "Avenue"],
["RD", "Road"],
["HL", "Hill"],
["BLVD", "Boulevard"],
["PL", "Place"],
["TER", "Place"],
["CIR", "Circle"],
["KPN", "Key Peninsula North"],
["CV", "Cove"],
["BLF", "Bluff"],
# Cities
["FT", "Fort"],
# Apartments and such.
["APT", "Apartment"],
["STE", "Suite"],
]
File.delete(output_file)
output = []
CSV.foreach(input_file) do |row|
# The names
names = row.shift
column_number = 0
row = row.map do |text|
column_number += 1
if text.is_a?(String)
text = text.split(/ |\_/).map{ |word|
if text.is_a?(String)
word_replacements.each do | replace, replacement |
word.gsub!(Regexp.new(/^#{Regexp.escape(replace)}$/), replacement)
end
# Werid thing, CT is both Connecticut and Court— luckily unless we
# are on the state box, we don't care about Connecticut
if column_number != 4
word.gsub!(/^Connecticut$/, "Court")
end
word = word.split.map(&:capitalize).join(' ')
word.gsub!("Usa", "United States")
end
word
}.join(" ")
text.gsub!("Po Box", "PO Box")
end
text
end
row.unshift(names)
output << row
end
CSV.open(output_file, "a+") do |csv|
output.each do |line|
csv << line
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment