Tag Archive: transformation



Handy libraries to transform your data part 1: Google phone number

When dealing with data that you want to import into elasticsearch, one of the challenges is to store the data in a uniform way. This will make it easier for you to query your data. To store data in a uniform way you often need to transform the data. In this serie of blog posts we will discuss libraries that make you’re life easier when transformations are needed.

This blog is about the Google’s phone number library. This library allows you to parse, format, store and validate international phone numbers. Google provides a Java, C++ and JavaScript variant which allows you to use these functionalities on either the front-end or back-end of an application.

Parsing single phone number

The first thing that you need to do is parse the phone number. This results in a PhoneNumber object, which can be used to retrieve or remove information like country code. We need to provide a default region to the parse method. This will only be used if the number being parsed is not written in international format. The next code block shows an example of the parsing.

String phoneNumberAsString = "088 5864 670";
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
PhoneNumber phoneNumber = phoneNumberUtil.parse(phoneNumberAsString, "NL");

The phone number in the example is formatted correctly, but the library is smart enough to recognise other formats as well. So it doesn’t matter if you use white spaces, the country code (with or without plus sign) or any other combination.

Parsing text

Another option that this library provides is extracting phone numbers from text. Also for this method you need to provide a default region.

Iterable phoneNumberMatches = phoneNumberUtil.findNumbers(
    "Call Luminis Amsterdam office at 088 5864 670 or at the" +
    " international version +31 (0)88 58 64 670", "NL");

The returned iterable phoneNumberMatches contains in this example two PhoneNumberMatch objects. Each PhoneNumberMatch object has a PhoneNumber object which can be used for further actions.

Validating

With the created PhoneNumber object we can validate if this phone number is a correct phone number.

boolean isValidPhoneNumber = phoneNumberUtil.isValidNumber(phoneNumber);

When the provided phone number does not contain the country code and is not recognised as a phone number from the specified default region, the isValidNumber() method will return false.

Formatting

Now that we have identified that we have a valid phone number, we can format it in a way we want to store it.

phoneNumberUtil.format(phoneNumber,
    PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);

The outcome of the formatter will be +31 88 586 4670.

Final thoughts

The google phone number library contains a few more functionalities like matching numbers or provide geographical information related to a phone number. For more information check out there github page.

That’s it for the first part of libraries that can help you transform your data. Hopefully this can be handy in your project. See you next time!