It’s been almost a year since the release of Firebase ML Kit and the team brought some neat changes to the package: 4 new AI models to do with Natural Language and Preparation for Production.

Of course, as one who advocates the ease of use of AI so endearingly as long as it doesn’t destroy the human race, I must spread the word on this fine art in the way I do best.

This week, we’re heading back into the ML Kit series strong covering the Natural Language models! Split into two parts, Language Identification and Smart Replies.

Prerequisites

If you’re new to Firebase and don’t yet have it in your app, you’ll have to connect it to Firebase like so, then you’ll need these dependencies in your app/build.gradle.

Additionally for the Smart Reply model, you also need to disable tflite compression in your app/build.gradle.

Language Identification

Simple language identification
“My hovercraft is full of eels.” en (English)
“ph’nglui mglw’nafh TensorFlow Google wgah’nagl fhtagn” und (Unidentified)

Table stolen from Firebase Docs

If it’s not already obvious enough, this model takes in a string of text and attempts to identify the language it’s written in. This works just like the other ML Kit AI models, it returns all the possible languages of the string in varying confidence levels with the highest the most likely to be the language it’s written in.

The model supports 103 different languages in their native scripts and can identify romanized text for the languages Arabic, Bulgarian, Chinese, Greek, Hindi, Japanese, and Russian.

Identify String Language

Fortunately, the new models are super easy to use, especially compared with the original set of ML Kit models.

All you need to do is get a LanguageIdentification from an instance of FirebaseNaturalLanguage.

Then call languageIdentifier.identifyLanguage and add success and failure listeners. The success method will return the languageCode as a String which will be the language with the highest confidence level over 0.5. If no such language exists, ‘und’ will be returned instead.

Alternately, you can call languageIdentifier.identifyAllLanguages which will return a list of likely languages of the string as IdentifiedLanguage, an object that contains the language code and its confidence level.

The confidence threshold for a language to be returned in this list is 0.01. Again, if no language meets the threshold, the list will have one item and it’ll be ‘und’.

Change Confidence Thresholds

Optionally, when you call getLanguageIdentification to make the languageIdentifier, you can pass in a FirebaseLanguageIdentificationOptions which you can use to change the confidence thresholds for these methods. The thresholds are separate for the two methods.

Smart Replies

This feature is becoming a trend in many social and email apps including Gmail, LinkedIn, and many SMS apps. Firebase Smart Replies generate up to 3 responses based on the last 10 messages from a conversation history.

The model only works for English and will not generate replies for sensitive topics.

Build the Conversation: A list of FirebaseTextMessages

The conversation you’ll be passing into the Smart Reply object is a List of FirebaseTextMessage. When the user sends a message, add the message by calling FirebaseTextMessage.createForLocalUser, passing in the message and its timestamp.

Then when the user receives a message, call FirebaseTextMessage.createForRemoteUser, passing in the message, timestamp, and a userID to uniquely identify the sender. This userID doesn’t need to correspond to any user data (so it doesn’t have to relate to the Auth UID, though it can) and doesn’t even need to be consistent between separate invocations of the Smart Reply.

A sample conversation list can look like this:

 Timestamp User ID  Local User?  Message
 Thu Feb 21 13:13:39 PST 2019   true   are you on your way?
 Thu Feb 21 13:15:03 PST 2019   FRIEND0   false   Running late, sorry!

Table also stolen from Firebase Docs

Generate the Replies

To generate the suggestions based on the conversation list built, instantiate a FirebaseSmartReply and call suggest, passing in the conversation.

As per usual, add success and failure listeners. the Success method returns a SmartReplySuggestionResult. This contains a list of strings contained in result.suggestions. You can also use result.getStatus to find out if your operation was successful.

For the conversation history above, you might get these replies.

  • No worries
  • 😞
  • No problem!

Note that 3 is the max number of generated replies. ML Kit might not return results if the model isn’t confident in the relevance of the suggested replies, the input conversation isn’t in English, or if the model detects sensitive subject matter.