1-first of all, add the dependencies for the ML Kit Android libraries to your module (app-level) Gradle file (usually app/build.gradle):
implementation 'com.google.firebase:firebase-ml-natural-language:22.0.0'
implementation 'com.google.firebase:firebase-ml-natural-language-language-id-model:20.0.7'
2-Identify the language of a string, get an instance of FirebaseLanguageIdentification, and then pass the string to the identifyLanguage() method.
FirebaseLanguageIdentification languageIdentifier =
FirebaseNaturalLanguage.getInstance().getLanguageIdentification();
languageIdentifier.identifyLanguage(text)
.addOnSuccessListener(
new OnSuccessListener<String>() {
@Override
public void onSuccess(@Nullable String languageCode) {
if (languageCode != "und") {
Log.i(TAG, "Language: " + languageCode);
} else {
Log.i(TAG, "Can't identify language.");
}
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be loaded or other internal error.
// ...
}
});
3-By default, ML Kit returns a value other than und only when it identifies the language with a confidence value of at least 0.5. You can change this threshold by passing a FirebaseLanguageIdentificationOptions object to getLanguageIdentification():
FirebaseLanguageIdentification languageIdentifier = FirebaseNaturalLanguage
.getInstance()
.getLanguageIdentification(
new FirebaseLanguageIdentificationOptions.Builder()
.setIdentifyLanguageConfidenceThreshold(0.34f)
.build());
4-To get the confidence values of a string's most likely languages, get an instance of FirebaseLanguageIdentification, and then pass the string to the identifyAllLanguages() method.
FirebaseLanguageIdentification languageIdentifier =
FirebaseNaturalLanguage.getInstance().getLanguageIdentification();
languageIdentifier.identifyAllLanguages(text)
.addOnSuccessListener(
new OnSuccessListener<String>() {
@Override
public void onSuccess(List<IdentifiedLanguage> identifiedLanguages) {
for (IdentifiedLanguage identifiedLanguage : identifiedLanguages) {
String language = identifiedLanguage.getLanguageCode();
float confidence = identifiedLanguage.getConfidence();
Log.i(TAG, language + " (" + confidence + ")");
}
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
// Model couldn’t be loaded or other internal error.
// ...
}
});
5- By default, ML Kit returns only languages with confidence values of at least 0.01. You can change this threshold by passing a FirebaseLanguageIdentificationOptions object to getLanguageIdentification():
FirebaseLanguageIdentification languageIdentifier = FirebaseNaturalLanguage
.getInstance()
.getLanguageIdentification(
new FirebaseLanguageIdentificationOptions.Builder()
.setIdentifyAllLanguagesConfidenceThreshold(0.5f)
.build());
Use the 5 steps above