How to parse an Unstructured International Address in Python using NLP

Working with mailing addresses can be a source of all kinds of pain and suffering if you don’t have the right support systems in place. Oftentimes, customers will submit an address in a completely jumbled order, which can wreak havoc, especially when dealing with product shipping. To combat this, a little cutting edge Natural Language Processing can be used to sort out messy strings into proper addresses. This lifesaver would normally require hours of programming to set up, but I can show you how to do it in about 5 minutes. Let’s begin.

pip install cloudmersive-validate-api-client

Next we are going to make a function call for address_parse_string, which can be set up in this fashion:

from __future__ import print_functionimport timeimport cloudmersive_validate_api_clientfrom import ApiExceptionfrom pprint import pprint# Configure API key authorization: Apikeyconfiguration = cloudmersive_validate_api_client.Configuration()configuration.api_key['Apikey'] = 'YOUR_API_KEY'# Uncomment below to setup prefix (e.g. Bearer) for API key, if needed# configuration.api_key_prefix['Apikey'] = 'Bearer'# create an instance of the API classapi_instance = cloudmersive_validate_api_client.AddressApi(cloudmersive_validate_api_client.ApiClient(configuration))input = cloudmersive_validate_api_client.ParseAddressRequest() # ParseAddressRequest | Input parse requesttry:# Parse an unstructured input text string into an international, formatted addressapi_response = api_instance.address_parse_string(input)pprint(api_response)except ApiException as e:print("Exception when calling AddressApi->address_parse_string: %s\n" % e)

Now go ahead and drop in a garbled address to test it out. Easy.

Image for post
Image for post

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store