5 Simple Step To Identify The Language Of Text Using Firebase ML Kit Android Studio 2020 (Complete Guide) With Source Code
In this post, we're going to identify the language of text using a firebase ml kit in an android studio.
This is the output after you are done with all step:
The String used is a dummy text in Spanish and es is language code for the Spanish language.
The output will be in the BCP-47 Language Code. See the complete list of supported languages.
So, now make it happen :)
Step 1: Add Firebase to your android project:
I recommend you to see how to add firebase to the android project in 5minutes to know how to add it or if you already add it then you can move on to 2nd Step.
Step 2: Add this dependency for the ml kit android libraries to your app-level build.gradle file:
as shown below and then click on Sync Now.
Step 3: Declare the String and design the layout:
We're using the Spanish String so declare it in string.xml
as shown below:
and now design the layout to show the Spanish String and the language of String on button click :
as shown below:
Step 4: Get the string from the text:
Step 5: Identify the language of string:
as shown below:
Now, run the app :)
If everything is done correctly then you see the excepted output.
You can see the full source code at GitHub.
If you face any problem or have any suggestion please comment it down we love to answer it.
This is the output after you are done with all step:
The String used is a dummy text in Spanish and es is language code for the Spanish language.
The output will be in the BCP-47 Language Code. See the complete list of supported languages.
Step 1: Add Firebase to your android project:
I recommend you to see how to add firebase to the android project in 5minutes to know how to add it or if you already add it then you can move on to 2nd Step.
Step 2: Add this dependency for the ml kit android libraries to your app-level build.gradle file:
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'
as shown below and then click on Sync Now.
Step 3: Declare the String and design the layout:
We're using the Spanish String so declare it in string.xml
<string name="text_spanish">Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed tempor y vitalidad, por lo que el trabajo y dolor, algunas cosas importantes que hacer eiusmod.
</string>
as shown below:
and now design the layout to show the Spanish String and the language of String on button click :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button" android:layout_centerInParent="true" android:layout_margin="30dp" android:text="@string/text_spanish" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="Find Language !" /> <TextView android:id="@+id/textlanguage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/button" android:layout_margin="30dp" android:textColor="@android:color/black" android:textSize="15sp" />
</RelativeLayout>
as shown below:
Step 4: Get the string from the text:
Step 5: Identify the language of string:
private void identifyLanguage(String string) { //1.Create instance of firebase language identifier FirebaseLanguageIdentification languageIdentifier = FirebaseNaturalLanguage.getInstance().getLanguageIdentification(); //2.Pass string to the identify Language Method languageIdentifier.identifyLanguage(string) .addOnSuccessListener(new OnSuccessListener<String>() { @SuppressLint("SetTextI18n") @Override public void onSuccess(@Nullable String languageCode) { if (languageCode != null) { if (!languageCode.equals("und")) { languageOfText.append("Language With High Probability:" + "\n\n"); languageOfText.append("Language Code: " + languageCode + "\n\n\n"); } else { languageOfText.setText("Can't identify language."); } } } }); //3. List of all possible language FirebaseLanguageIdentification allPossiblelanguageIdentifier = FirebaseNaturalLanguage.getInstance().getLanguageIdentification(); allPossiblelanguageIdentifier.identifyPossibleLanguages(string).addOnCompleteListener(new OnCompleteListener<List<IdentifiedLanguage>>() { @SuppressLint("SetTextI18n") @Override public void onComplete(@NonNull Task<List<IdentifiedLanguage>> task) { List<IdentifiedLanguage> identifiedLanguageList = task.getResult(); if (identifiedLanguageList != null) { languageOfText.append("All Possible Language Identified: " + "\n\n"); for (IdentifiedLanguage identifiedLanguage : identifiedLanguageList) { String language = identifiedLanguage.getLanguageCode(); float confidence = identifiedLanguage.getConfidence(); languageOfText.append("Language Code: " + language + " (" + ("" + confidence * 100).subSequence(0, 4) + "%" + ")" + "\n\n"); } } } });}
as shown below:
Now, run the app :)
If everything is done correctly then you see the excepted output.
You can see the full source code at GitHub.
If you face any problem or have any suggestion please comment it down we love to answer it.
Comment down what next topic you need a guide on? or Drop a message on our social media handle
Happy coding and designing : )
Comments
Post a Comment