r/dailyprogrammer • u/jnazario 2 0 • Jul 12 '17
[2017-07-12] Challenge #323 [Intermediate] Parsing Postal Addresses
Description
Nealy everyone is familiar with mailing addresses - typically a person, optionally an organization, a street address or a postal box, a city, state or province, country, and a postal code. A practical bit of code to have is something that parses addresses, perhaps for validation or for shipping cost calculations.
Today's challenge is to parse addresses into some sort of data structure - an object (if you're using an OOP language), a record, a struct, etc. You should label the fields as correctly or appropriately as possible, and map them into a reasonable structure. Not all fields will be present, so you'll want to look over the challenge input first and design your data structure appropriately. Note that these include international addresses.
Input Description
You'll be given an address, one per multi-line block. Example:
Tudor City Greens
24-38 Tudor City Pl
New York, NY
10017
USA
Output Description
Your program should emit a labeled data structure representing the address. From the above example:
business=Tudor City Greens
address=24-38
street=Tudor City Pl
city=New York
state=NY
postal_code=10017
country=USA
Your field names may differ but you get the idea.
Challenge Input
Docks
633 3rd Ave
New York, NY
10017
USA
(212) 986-8080
Hotel Hans Egede
Aqqusinersuaq
Nuuk 3900
Greenland
+299 32 42 22
Alex Bergman
Wilhelmgalerie
Platz der Einheit 14
14467 Potsdam
Germany
+49 331 200900
Dr KS Krishnan Marg
South Patel Nagar
Pusa
New Delhi, Delhi
110012
India
15
u/FunkyNoodles Jul 12 '17
A little bit of explanation on the foreign addresses would be nice?
For example, Here's what I am assuming for the India address:
name=Dr KS Krishnan Marg
business???=South Patel Nagar
address???=Pusa
city_and_state=New Delhi, Delhi
postcode=110012
country=India
10
u/gabyjunior 1 2 Jul 12 '17
C
Using curl library to query Google maps geocoding API and get json formatted address with geolocation.
Phone number is not retrieved by this API though (would require to use places API providing unique place_id).
The program takes API key as argument and reads address on standard input.
Source code (program must be linked with libcurl)
Output for address in India
5
u/Bizzlington Jul 16 '17
A bit late to the party - but i used to work for one of the largest retailers in the UK. We had a group of 'experts' (the most senior people we had anyway) who tried to write a program/routine like this - and they failed.
So many different countries have so many different styles of writing addresses. And even within those countries so many people have their own way of doing it to confuse matters even more.
It's something which has plagued us for a long time. Even on our website where we try and force people to input each line seperately and specifically (street, town, state, county, country, zip code, building name, building number, etc) it's still not perfect.
We signed up to a web service (for a lot of money) whose sole purpose was to take an address and parse it into individual fields and they would still get it wrong ~10% of the time.
3
u/FunkyNoodles Jul 12 '17
I asked a question about the India address above
Anyways, here's what I did in Python 2, this doesn't work for India address however. I will clean it up when my question gets resolved
4
u/svgwrk Jul 13 '17
If my boss asked me to do this I'd tell him to eat my shorts. :)
2
u/Working-M4n Jul 12 '17
What is the best approach to this? Try to match each line against a different list of cities, countries, and post codes? Also, I can't think of a good way to determine what to do if the person/place has a number or other confusing nouns in the title, '112 Eatery', 'Eat Street Social', 'St. Louis Grille', etc.
2
u/icalltehbigonebitey Jul 12 '17
Javascript
I cheated a bit with a "localization" variable, which handles fields that don't have a good analogue to US postal addresses.
JSFiddle
2
u/cheers- Jul 13 '17 edited Jul 13 '17
Javascript (Node)
Uses googleMaps api (see /u/gabyjunior solution )
Addresses and the api key are loaded from json files then makes queries to google's api using the client lib @google/maps.
Results are saved in json files
Note :
It uses two functions I created called FileReader FileWriter that wrap async filesystem I/O in Promise objects and are not included in this post.
1
u/cheers- Jul 13 '17
the american addresses you provided use the wrong format:
zip code should be next to the state separated only by a \u0020 whitespace not after a newline.
info:
http://www.bitboost.com/ref/international-address-formats/united_states/
https://en.wikipedia.org/wiki/Address_(geography)#United_States
1
u/Dange_ranger Jul 13 '17
Don't forget about the assumptions we make about addresses. There are many edge cases to consider.
https://www.mjt.me.uk/posts/falsehoods-programmers-believe-about-addresses https://smartystreets.com/articles/regular-expressions-for-street-addresses
0
u/[deleted] Jul 12 '17
[deleted] — view deleted comment