Language Identification with ML Kit on Android

This post is about how to setup ML Kit’s language Identification feature in Android application with the help of simple demo app.

ML Kit’s Language Identification API

ML Kit’s language Identification API allows you to identify the language of a string of text. You can get the string’s most likely language as well as confidence scores for all of the string’s possible languages.

Creating new project

1 . Create a new project by going to File ⇒ New Android Project, select Empty Activity , provide app name, select language to kotlin and then finally click on finish.

Adding Dependency

2 . Open build.gradle (Module: app) file and add the ML Kit’s language Identification API dependency inside the dependencies section:

build.gradle

dependencies {
//bundled language identification dependency
implementation 'com.google.mlkit:language-id:17.0.1'
}
Creating Layout File

3. The activity_main.xml layout file defines the UI of the application.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
tools:context=".MainActivity">

<EditText
android:id="@+id/edt_input"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter yout text"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btn_identify_language"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="Identify Language"
app:layout_constraintEnd_toEndOf="@+id/tv_output_language"
app:layout_constraintStart_toStartOf="@+id/tv_output_language"
app:layout_constraintTop_toBottomOf="@+id/tv_output_language" />

<Button
android:id="@+id/btn_possible_language"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Identify Possible Language"
app:layout_constraintEnd_toEndOf="@+id/btn_identify_language"
app:layout_constraintStart_toStartOf="@+id/btn_identify_language"
app:layout_constraintTop_toBottomOf="@+id/btn_identify_language" />

<TextView
android:id="@+id/tv_input_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="@+id/edt_input"
app:layout_constraintStart_toStartOf="@+id/edt_input"
app:layout_constraintTop_toBottomOf="@+id/edt_input" />

<TextView
android:id="@+id/tv_output_language"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="@+id/tv_input_text"
app:layout_constraintStart_toStartOf="@+id/tv_input_text"
app:layout_constraintTop_toBottomOf="@+id/tv_input_text" />

</androidx.constraintlayout.widget.ConstraintLayout>
Identify the language of a string

4 . To identify the language of a string, create an instance of LanguageIdentifier, and then pass the string to the identifyLanguage() method of LanguageIdentifier.

val languageIdentifier = LanguageIdentification.getClient()

// Identify a language.

private fun identifyLanguage(inputText: String) {
languageIdentifier
.identifyLanguage(inputText)
.addOnSuccessListener { identifiedLanguage ->
tvLanguage.text = "Identified language(s): " + identifiedLanguage
}
.addOnFailureListener { e ->
Log.e(TAG, "Language identification error", e)
}
}
  • If the call succeeds, identified language is passed to the success listener or If no language is detected, und (undetermined) is passed.
  • In case of error, exception is passed to the failure listener.
Identify the possible languages of a string

5 . To get the confidence values of a string’s most likely languages, get an instance of LanguageIdentifier and then pass the string to the identifyPossibleLanguages() method.

languageIdentifier = LanguageIdentification.getClient()

// Identify all possible languages.
private fun identifyPossibleLanguages(inputText: String) {
languageIdentifier
.identifyPossibleLanguages(inputText)
.addOnSuccessListener { identifiedLanguages ->
var output = ""
for (identifiedLanguage in identifiedLanguages) {
output += identifiedLanguage.languageTag + " (" + identifiedLanguage.confidence + "), "
}
tvLanguage.text =
"Identified language (s):" + output.substring(0, output.length - 2)
}
.addOnFailureListener { e ->
Log.e(TAG, "Language identification error", e)
}
}
  • If the call succeeds, a list of IdentifiedLanguage objects is passed to the success listener.
  • In case of error, exception is passed to the failure listener.

Note: language confidence values indicate the confidence that the entire string is in the given language; ML Kit doesn’t identify multiple languages in a single string.

Complete MainActivity Code

MainActivity.kt

class MainActivity : AppCompatActivity() {
private lateinit var edtInput: EditText
private lateinit var tvLanguage: TextView
private lateinit var tvInput: TextView
private lateinit var languageIdentifier: LanguageIdentifier

companion object {
private const val TAG = "MainActivity"
}

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

languageIdentifier = LanguageIdentification.getClient()
lifecycle.addObserver(languageIdentifier)
edtInput = findViewById(R.id.edt_input)

tvInput = findViewById(R.id.tv_input_text)
tvLanguage = findViewById(R.id.tv_output_language)

findViewById<Button>(R.id.btn_identify_language).setOnClickListener { _ ->
val input = edtInput.text.toString()

if (!input.isEmpty()) {
identifyLanguage(input)
}
}

findViewById<Button>(R.id.btn_possible_language).setOnClickListener { _ ->
val input = edtInput.text.toString()

if (!input.isEmpty()) {
identifyPossibleLanguages(input)
}
}
}

// Identify a language.
private fun identifyLanguage(inputText: String) {
tvLanguage.text = "Waiting…"
languageIdentifier
.identifyLanguage(inputText)
.addOnSuccessListener { identifiedLanguage ->
tvInput.text = "Input Text: " + inputText
tvLanguage.text = "Identified language(s): " + identifiedLanguage
}
.addOnFailureListener { e ->
Log.e(TAG, "Language identification error", e)
}
}

// Identify all possible languages.
private fun identifyPossibleLanguages(inputText: String) {
tvLanguage.text = "Waiting…"
languageIdentifier
.identifyPossibleLanguages(inputText)
.addOnSuccessListener { identifiedLanguages ->
tvInput.text = "Input Text: " + inputText

var output = ""
for (identifiedLanguage in identifiedLanguages) {
output += identifiedLanguage.languageTag + " (" + identifiedLanguage.confidence + "), "
}
tvLanguage.text =
"Identified language (s):" + output.substring(0, output.length - 2)
}
.addOnFailureListener { e ->
Log.e(TAG, "Language identification error", e)
}
}
}
Run the App

When you run the app it will look like this:

       

 

Leave a Reply