The rapid pace of the world around us often makes us take many small miracles for granted. For example, I can go on Amazon Japan, order some gadget, and have it delivered to my door within days (eventually, within minutes). We only get to see the delivery guy (or girl, if you’re lucky) dropping off the package, but there are many things happening in the background:
- Amazon packages the item and prints my address on the outside.
- Amazon entrusts the package to some domestic delivery company (in Japan, typically Yamato or Sagawa).
- The delivery company interprets the address, in particular, looks for the postcode, prefecture, city and street address.
- The delivery company, which has depos all around Japan, sends the package to a depo location that is closest to me, based on the above.
- A driver from the depo goes out to the address and delivers the package.
In this article, I’d like to focus on the third step: interpreting an address. This isn’t something that is restricted to delivery companies: there are many applications that involve finding out where exactly something is. This particular task is known as geocoding and it is part of a broader problem area known as geolocation.
But let’s get back to the specific problem and formulate it. We have a string that contains an address - nothing but the address. For example:
Level 7 3 Thomas Holt Drive North Ryde NSW 2113, Australia
We’d like to interpret that address. More specifically, we’d like to determine:
- the street number (3)
- the street name (Thomas Holt Drive)
- the city (North Ryde)
- the state (New South Wales)
- the postcode (2113)
- the country (Australia)
- any other relevant information (such as “Level 7” or PO box numbers)
If that seems too easy, then this is the same address:
L7, 3 Thomas Holt Dr., Nth. Ryde, NSW 2113
- Regular expressions can get you far, but not all the way
- Some Python packages exist, but YMVA
- There are several online services for parsing address (some free, some not free) (read a review)
The online services tend to give the best results, but it’s not practical to use them if, for example, you want to parse several million addresses in a hurry. And cheap. In that case, you’d have to reinvent the wheel.
Luckily, you don’t need to start from scratch. Many postal services maintain standards documents that dictate how exactly addresses need to be formatted (for example, USPS Publication 28). This document is a goldmine of information that contains goodies like:
- Acceptable abbreviations for US street types and states
- The expected order of address components
- Mappings between English and Spanish address terms (for addresses in the US Territory of Puerto Rico).
By treating these documents as specifications, it’s possible to write a fairly robust parser for a particular country. Furthermore, the specifications of different countries share many common points. For example:
- Addresses in English-speaking countries generally follow the same order (street, city, state, postcode, country)
- Street types in English-speaking countries tend to be pretty common, such as the usual “street”, “road”, “avenue” and the more exotic “boulevard” and “esplanade”.
- British and Canadian postcodes follow a similar pattern
- … and many more.
That’s all for today, but in future articles, I’ll be looking at the actual mechanics of parsing an address, as well as other essential supplementary materials. Stay tuned!