Entity Extraction using Google’s ML Kit on Android

Extract relevant information with the power of Machine Learning

Danish Amjad
ProAndroidDev

--

Photo by Kevin Ku on Unsplash

Google’s on-device ML Kit introduced another useful API for Entity Extraction, which extracts the entities from the static text or while typing based on that once an entity is extracted you can easily enable different actions for the user based on the entity type.

The Entity Extraction API supports multiple languages like English, Arabic, French, German rest of the list you can check from here.

The following Supported entities included are:

Source

In this article, we’ll do just that on Android with the help of Google ML Kit’s Entity Extraction API.

If you want to learn from the video then check my video tutorial below and subscribe to my channel. 👇

Entity Extraction Tutorial

What is ML Kit?

ML Kit is a cross-platform mobile SDK (Android and iOS) developed by Google that allows developers to easily access on-device mobile machine learning models.

All the ML Kit’s APIs run on-device, allowing real-time and offline capabilities.

To use the standalone ML Kit on-device SDK, we can just implement it directly — we don’t need to create a project on Firebase or an accompanying google.json file.

If you’re using Firebase Machine Learning, then you can check this link to help migrate.

What You’ll Build

In this article, we’re going to build a simple Android app that shows you how to implement ML Kit’s Entity Extraction on-device API.

For the purposes of this demo project, I have given the text which is “My flight is LX373” and it extracts the entity the flight code and flight number defined in the extractor object EntityExtractorOptions.

By the end of this tutorial, you should see something similar to the screenshot below:

Entity Extraction Screenshot

Step 1: Add Dependency

First thing first, we need to add a mlkit:entity-extraction dependency to our Android project in the app/build.gradle file.

build.gralde

Note: At the time of writing, the latest com.google.mlkit:entity-extraction version was 16.0.0-beta2, but you can use any of the latest stable releases you want from the ML Kit Release Notes.

Sync the Project

After successfully adding the dependency, just sync the project, as shown in the screenshot below:

Sync project screenshot

Step 2: Extract Entities

In this step, we have to create an object for an entity extractor and configure it with the EntityExtractorOption.

For the purposes of this demo project, I have configured the English language settings, you can configure any other language with respect to your need, Now let’s jump into some code to see how these above steps look in practice.

an object for an entity extractor

Step 3: Download the Extraction model

Before executing the text first we have to download the Extraction model. Make sure you have downloaded the model on your device and don’t call extraction API until you know the model is available.

We have OnSuccessListener and, when our model is successfully downloaded, we can call the extraction API.

We also add an OnFailureListener—if the model fails to download, we’ll be able to show the user an error.

Now let’s jump into some code to see how these above steps look in practice:

download model

Step 4: Execute Extraction API

After successfully downloading the model, it’s time to call the extraction API to extract the entities from the text. To do that first we need to create an object for EntityExtractionParams and define the text for entity extraction after that pass that object to the annotate()function.

We have an addOnSuccessListener — once API gives us success then it will return us the list of Entity Annotations so from that we can extract the entities based on the pre-defined entities i.e DateTimeEntity, FlightNumberEntity.

We also have an OnFailureListener—if the model fails to extracts, we’ll be able to show the user an error.

Now let’s jump into some code to see how these above steps look in practice:

Entity Extraction API

Result

Let’s build and run the application to see our Entity Extraction application in practice:

Entity Extraction Result

Conclusion

This article taught you how to implement ML Kit’s on-device Entity Extraction API on Android. To do this, we learned how to configure the Extract object and download the extraction model, and, after downloading the model, we then extract the entities based on pre-defined entities.

If you want to explore in more detail, check out the official docs:

I hope this article was helpful. If you think something is missing, have questions, or would like to offer any thoughts or suggestions, go ahead and leave a comment below. I’d appreciate the feedback.

I’ve written some other Android-related content, and if you liked what you read here, you’ll probably also enjoy my Medium page.

Sharing (knowledge) is caring 😊 Thanks for reading this article. Be sure to clap or recommend this article if you found it helpful. It means a lot to me.

If you need any help, join me on Twitter, LinkedIn, GitHub, and subscribe to my YouTube Channel.

--

--