Generate Auto Suggestion using Android Text Classifier API

Pawan Pal
MindOrks
3 min readNov 25, 2019

--

Recently in Android 10 new updates added in TextClassifier API and on-device machine learning model integrated for smart replies and language detection.

What is Text Classifier API?

The text classifier framework first introduced in Android 8.1. You must have observed that when you select some text in your android phone running above 8.1 suggestions appears related to the text. For example, selected some address then an open map link appears similarly with the call, compose an email, open chrome and much more. It searches for the type of text it can be TYPE address, URL, date, email and even flight number for tracking.

source: https://source.android.com/devices/tech/display/images/textclassifier.png

How is this happening?

The Android Open Source Project (AOSP) features several neural network models for classifying text. Each model file is trained for a single language. You may choose to install any combination of models. That model also got changes in different versions of android.

texclassifier.[language-code].model

you can find the list here.

What’s new in Android 10

Android 10 introduces two methods to the TextClassifier API: suggestConversationActions and detectLanguage. The suggestConversationActions method generates suggested replies and actions from a given conversation and the detectLanguage method detects the language of the text. This uses the below-mentioned models for generating suggestions.

  • suggestionConversationActions: actions_suggestions.universal.model
  • detectLanguage: lang_id.model

okay, this is the brief information about the text classifier framework and its use in the android system.

How to use it in our app for generating suggestions?

Android default messaging app uses this to give suggestions directly in notification we can do similarly with any of our apps. I made a sample app in which I added various types of messages to filter the results.

Suggestions are showing below message

Lets code now

  • Add TextClassificationManager
    You have to use TextClassificationManager to get the instance of the classifier. Created all the instances I required such as TextClassifier and ConversationActions.Request.
  • Make ConversationActions Request
    To use TextClassifier to get a suggestion you have to make a request of ConversationActions.Request type. Use its Builder method and add the list of ConversationActions.Message in it. Below I made a method makeRequest and pass the string as a parameter.
  • Generate Suggestions
    The next step is to generate suggestions based on the text. Here I’m using suggestConversationActions method and passed the request in it.

Important: This is blocking operations. Avoid calling them on the UI thread.

This returns ConversationActions which contains a list of ConversationAction.

Through ConversationAction we can get all the information such as

  • getAction() Returns a RemoteAction object, which contains the icon, label, and a PendingIntent, for the specified action type.
  • getConfidenceScore() Returns the confidence score for the specified action.
  • getTextReply() Returns the text reply that could be sent as a reply to the given conversation.
  • getTextReply() Returns the text reply that could be sent as a reply to the given conversation.
  • getType() Return the type of this action, for example, TYPE_VIEW_CALENDAR.
itemView.suggestionName.text = conversationAction.textReply

We can use actions to start an app that can do our work, for example, translate or call.

The good thing is that we can check confidence score and set a threshold as per our use case.

That’s all in this article, hope you learn how to use Text API and integrate new features in your app. Complete code is on my GitHub.

Thanks.

--

--