How to Parse Unstructured Input Text into an International, Formatted Address using Java
The quality of the input fields we use on our websites has a big impact on the quality of the information we ultimately retrieve & store from our website visitors. When it comes to address input fields, we need structured entries which can be subsequently validated with ease — otherwise, we’ll lose critical data that we may not have a second chance at getting. Our Address Parse API can make a big difference in this critical area, using Machine Learning and Natural Language Processing to form unstructured address string inputs into structured & labeled fields. Below, I’ve provided an example response for the input “4695 Chabot Drive, Suite 200 Pleasanton, California 94588 United States”:
{
"Successful": true,
"Building": null,
"StreetNumber": "4695",
"Street": "chabot drive",
"City": "pleasanton",
"StateOrProvince": "california",
"PostalCode": "94588",
"CountryFullName": "United States",
"ISOTwoLetterCode": "US"
}
The above response has parsed all available information into its respective field and has provided a “null” response for fields where the required information was not provided.
To help you take advantage of this API, I’ve provided code examples below which you may use to structure your API call in Java. In order to ultimately call the API, you’ll just need a Cloudmersive API key, which you can get for free by registering a free account on our website (free accounts come with a limit of 800 API calls per month & zero additional commitments — perfect for small-scale projects and startups).
Our first step is to install the API client with Maven. Let’s do so by first adding a reference to the pom.xml repository:
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
Next, let’s add a reference to the pom.xml dependency to wrap up our installation step:
<dependencies>
<dependency>
<groupId>com.github.Cloudmersive</groupId>
<artifactId>Cloudmersive.APIClient.Java</artifactId>
<version>v4.25</version>
</dependency>
</dependencies>
Our next step is to include the imports at the top of our file, and then call the function. The API key & address string input fields are clearly labeled in the code comments for your convenience:
// Import classes:
//import com.cloudmersive.client.invoker.ApiClient;
//import com.cloudmersive.client.invoker.ApiException;
//import com.cloudmersive.client.invoker.Configuration;
//import com.cloudmersive.client.invoker.auth.*;
//import com.cloudmersive.client.AddressApi;ApiClient defaultClient = Configuration.getDefaultApiClient();// Configure API key authorization: Apikey
ApiKeyAuth Apikey = (ApiKeyAuth) defaultClient.getAuthentication("Apikey");
Apikey.setApiKey("YOUR API KEY");
// Uncomment the following line to set a prefix for the API key, e.g. "Token" (defaults to null)
//Apikey.setApiKeyPrefix("Token");AddressApi apiInstance = new AddressApi();
ParseAddressRequest input = new ParseAddressRequest(); // ParseAddressRequest | Input parse request
try {
ParseAddressResponse result = apiInstance.addressParseString(input);
System.out.println(result);
} catch (ApiException e) {
System.err.println("Exception when calling AddressApi#addressParseString");
e.printStackTrace();
}
After that, you’re all done. Nice & easy!