Skip to content

Instantly share code, notes, and snippets.

@ahoward
Created August 1, 2018 16:04
Show Gist options
  • Save ahoward/14d8106fbf2340ebe1c8149214a0f9f3 to your computer and use it in GitHub Desktop.
Save ahoward/14d8106fbf2340ebe1c8149214a0f9f3 to your computer and use it in GitHub Desktop.
#
require 'open-uri'
require 'json'
#
module TargetSmartStateDictionary
def TargetSmartStateDictionary.url
@url ||= (
'https://gist.githubusercontent.com/mshafrir/2646763/raw/8b0dbb93521f5d6889502305335104218454c2bf/states_hash.json'
)
end
def TargetSmartStateDictionary.states
@states ||= (
JSON.parse(open(url){|s| s.read})
)
end
def TargetSmartStateDictionary.patterns
@patterns ||= (
list = []
states.each do |key, value|
pattern = '\b' + '(' + key + ')' + '([\s0-9]*)\b'
list.push(/#{ pattern }/i)
end
list
)
end
def TargetSmartStateDictionary.match(string)
string = string.to_s
captures = []
patterns.each do |pattern|
captured = pattern.match(string).to_a
if captured[0]
captures.push(captured)
end
end
case captures.size
when 0
raise Error.new("0 matches in #{ string.inspect }")
when 1
captured = captures[0]
matched = captured[0].to_s
key = captured[1].to_s
code = captured[2].to_s
key = key.strip.upcase
state = states[key]
code = code.gsub(/\s+/, '').strip
description = string.gsub(matched, '').strip
if code.empty?
code = nil
end
result = {
'key' => key,
'state' => state,
'code' => code,
'description' => description
}
else
raise Error.new("N matches in #{ string.inspect }")
end
end
class Error < ::StandardError; end
end
#
p 'STATES' => TargetSmartStateDictionary.states
p 'patterns' => TargetSmartStateDictionary.patterns
[
'ma 123 foo bar',
' ma123 one two three ',
' co four five six',
].each do |string|
result = TargetSmartStateDictionary.match(string)
pp(
'string' => string,
'result' => result
)
end
__END__
{"STATES"=>{"AL"=>"Alabama", "AK"=>"Alaska", "AS"=>"American Samoa", "AZ"=>"Arizona", "AR"=>"Arkansas", "CA"=>"California", "CO"=>"Colorado", "CT"=>"Connecticut", "DE"=>"Delaware", "DC"=>"District Of Columbia", "FM"=>"Federated States Of Micronesia", "FL"=>"Florida", "GA"=>"Georgia", "GU"=>"Guam", "HI"=>"Hawaii", "ID"=>"Idaho", "IL"=>"Illinois", "IN"=>"Indiana", "IA"=>"Iowa", "KS"=>"Kansas", "KY"=>"Kentucky", "LA"=>"Louisiana", "ME"=>"Maine", "MH"=>"Marshall Islands", "MD"=>"Maryland", "MA"=>"Massachusetts", "MI"=>"Michigan", "MN"=>"Minnesota", "MS"=>"Mississippi", "MO"=>"Missouri", "MT"=>"Montana", "NE"=>"Nebraska", "NV"=>"Nevada", "NH"=>"New Hampshire", "NJ"=>"New Jersey", "NM"=>"New Mexico", "NY"=>"New York", "NC"=>"North Carolina", "ND"=>"North Dakota", "MP"=>"Northern Mariana Islands", "OH"=>"Ohio", "OK"=>"Oklahoma", "OR"=>"Oregon", "PW"=>"Palau", "PA"=>"Pennsylvania", "PR"=>"Puerto Rico", "RI"=>"Rhode Island", "SC"=>"South Carolina", "SD"=>"South Dakota", "TN"=>"Tennessee", "TX"=>"Texas", "UT"=>"Utah", "VT"=>"Vermont", "VI"=>"Virgin Islands", "VA"=>"Virginia", "WA"=>"Washington", "WV"=>"West Virginia", "WI"=>"Wisconsin", "WY"=>"Wyoming"}}
{"patterns"=>[/\b(AL)([\s0-9]*)\b/i, /\b(AK)([\s0-9]*)\b/i, /\b(AS)([\s0-9]*)\b/i, /\b(AZ)([\s0-9]*)\b/i, /\b(AR)([\s0-9]*)\b/i, /\b(CA)([\s0-9]*)\b/i, /\b(CO)([\s0-9]*)\b/i, /\b(CT)([\s0-9]*)\b/i, /\b(DE)([\s0-9]*)\b/i, /\b(DC)([\s0-9]*)\b/i, /\b(FM)([\s0-9]*)\b/i, /\b(FL)([\s0-9]*)\b/i, /\b(GA)([\s0-9]*)\b/i, /\b(GU)([\s0-9]*)\b/i, /\b(HI)([\s0-9]*)\b/i, /\b(ID)([\s0-9]*)\b/i, /\b(IL)([\s0-9]*)\b/i, /\b(IN)([\s0-9]*)\b/i, /\b(IA)([\s0-9]*)\b/i, /\b(KS)([\s0-9]*)\b/i, /\b(KY)([\s0-9]*)\b/i, /\b(LA)([\s0-9]*)\b/i, /\b(ME)([\s0-9]*)\b/i, /\b(MH)([\s0-9]*)\b/i, /\b(MD)([\s0-9]*)\b/i, /\b(MA)([\s0-9]*)\b/i, /\b(MI)([\s0-9]*)\b/i, /\b(MN)([\s0-9]*)\b/i, /\b(MS)([\s0-9]*)\b/i, /\b(MO)([\s0-9]*)\b/i, /\b(MT)([\s0-9]*)\b/i, /\b(NE)([\s0-9]*)\b/i, /\b(NV)([\s0-9]*)\b/i, /\b(NH)([\s0-9]*)\b/i, /\b(NJ)([\s0-9]*)\b/i, /\b(NM)([\s0-9]*)\b/i, /\b(NY)([\s0-9]*)\b/i, /\b(NC)([\s0-9]*)\b/i, /\b(ND)([\s0-9]*)\b/i, /\b(MP)([\s0-9]*)\b/i, /\b(OH)([\s0-9]*)\b/i, /\b(OK)([\s0-9]*)\b/i, /\b(OR)([\s0-9]*)\b/i, /\b(PW)([\s0-9]*)\b/i, /\b(PA)([\s0-9]*)\b/i, /\b(PR)([\s0-9]*)\b/i, /\b(RI)([\s0-9]*)\b/i, /\b(SC)([\s0-9]*)\b/i, /\b(SD)([\s0-9]*)\b/i, /\b(TN)([\s0-9]*)\b/i, /\b(TX)([\s0-9]*)\b/i, /\b(UT)([\s0-9]*)\b/i, /\b(VT)([\s0-9]*)\b/i, /\b(VI)([\s0-9]*)\b/i, /\b(VA)([\s0-9]*)\b/i, /\b(WA)([\s0-9]*)\b/i, /\b(WV)([\s0-9]*)\b/i, /\b(WI)([\s0-9]*)\b/i, /\b(WY)([\s0-9]*)\b/i]}
{"string"=>"ma 123 foo bar",
"result"=>
{"key"=>"MA",
"state"=>"Massachusetts",
"code"=>"123",
"description"=>"foo bar"}}
{"string"=>" ma123 one two three ",
"result"=>
{"key"=>"MA",
"state"=>"Massachusetts",
"code"=>"123",
"description"=>"one two three"}}
{"string"=>" co four five six",
"result"=>
{"key"=>"CO",
"state"=>"Colorado",
"code"=>nil,
"description"=>"four five six"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment