Blog Infos
Author
Published
Topics
, ,
Published
Extract relevant information with the power of Machine Learning

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:

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. 👇

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.
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:

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.

dependencies {
implementation 'com.google.mlkit:entity-extraction:16.0.0-beta2'
}
view raw build.gradle hosted with ❤ by GitHub

build.gralde

Note: At the time of writing, the latest com.google.mlkit:entity-extractionversion 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:

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.

val entityExtractor =
EntityExtraction.getClient(
EntityExtractorOptions.Builder(EntityExtractorOptions.ENGLISH)
.build())

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:

entityExtractor
.downloadModelIfNeeded()
.addOnSuccessListener { _ ->
/* Model downloading succeeded, you can call extraction API here. */
}
.addOnFailureListener { _ -> /* Model downloading failed. */ }

download model

Job Offers

Job Offers

There are currently no vacancies.

OUR VIDEO RECOMMENDATION

,

How to Optimize, Validate and Deploy ML Models On Device

In this workshop we address the common challenges faced by developers migrating AI workloads from the cloud to edge devices. Qualcomm aims to democratize AI at the edge, easing the transition to the edge by…
Watch Video

How to Optimize, Validate and Deploy ML Models On Device

Bhushan Sonawane & Meghan Stronach
Staff ML Engineer & AI Hub Program Manager
Qualcomm

How to Optimize, Validate and Deploy ML Models On Device

Bhushan Sonawane & ...
Staff ML Engineer & ...
Qualcomm

How to Optimize, Validate and Deploy ML Models On Device

Bhushan Sonawane ...
Staff ML Engineer & AI Hu ...
Qualcomm

Jobs

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:

entityExtractor
.downloadModelIfNeeded()
.addOnSuccessListener { _ ->
//step3
val params =
EntityExtractionParams.Builder("My flight is LX373")
.build()
entityExtractor
.annotate(params)
.addOnSuccessListener {
//step4
for (entityAnnotation in it) {
val entities: List<Entity> = entityAnnotation.entities
Log.d("VALUE", "Range: ${entityAnnotation.start} - ${entityAnnotation.end}")
for (entity in entities) {
when (entity) {
is DateTimeEntity -> {
result.text="Granularity: ${entity.dateTimeGranularity} \n TimeStamp: ${entity.timestampMillis}"
Log.d("VALUE", "Granularity: ${entity.dateTimeGranularity}")
Log.d("VALUE", "TimeStamp: ${entity.timestampMillis}")
}
is FlightNumberEntity -> {
result.text= "Airline Code: ${entity.airlineCode} \nFlight number: ${entity.flightNumber}"
Log.d("VALUE", "Airline Code: ${entity.airlineCode}")
Log.d("VALUE", "Flight number: ${entity.flightNumber}")
}
else -> {
Log.d("VALUE", " $entity")
}
}
}
}
}
.addOnFailureListener {
// Check failure message here.
}
}
.addOnFailureListener { _ -> /* Model downloading failed. */ }
Result

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

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.

YOU MAY BE INTERESTED IN

YOU MAY BE INTERESTED IN

blog
Face recognition is a problem where the identity of the person needs to be…
READ MORE
blog
Google has announced a new open-source project, Now in Android. In this article, you…
READ MORE
blog
A while ago, I developed an Android app, Android-Doc-QA which is an instance of…
READ MORE
blog
I implemented the same functionality using both frameworks to compare them side by side.…
READ MORE
Menu