Site icon C1CTech

Language Identification with ML Kit on Android

&NewLine;<p>This post is about how to setup ML Kit’s language Identification feature in Android application with the help of simple demo app&period;<&sol;p> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>ML Kit&&num;8217&semi;s Language Identification API<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>ML Kit&&num;8217&semi;s language Identification API allows you to identify the language of a string of text&period; You can get the string&&num;8217&semi;s most likely language as well as confidence scores for all of the string&&num;8217&semi;s possible languages&period;<&sol;p> &NewLine; &NewLine; &NewLine; &NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex"> &NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" style&equals;"background-color&colon; &num;040a65&semi;" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;MLKit-LanguageID"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div> &NewLine;<&sol;div> &NewLine;<p><amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"lsLjQlSI62A" title&equals;"Language Identification with ML Kit on Android"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;lsLjQlSI62A"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;lsLjQlSI62A&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Language Identification with ML Kit on Android"><&sol;a><&sol;amp-youtube><&sol;p> &NewLine;<h4><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new project<&sol;strong><&sol;span><&sol;h4> &NewLine;<p>1 &period; Create a new project by going to File &Implies; New Android Project&comma; select Empty Activity &comma; provide app name&comma; select language to kotlin and then finally click on <span style&equals;"color&colon; &num;008000&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p> &NewLine;<h5><strong><span style&equals;"color&colon; &num;000080&semi;">Adding Dependency<&sol;span><&sol;strong><&sol;h5> &NewLine;<p>2 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>build&period;gradle &lpar;Module&colon; app&rpar;<&sol;strong><&sol;span> file and add the ML Kit’s language Identification API dependency inside the <strong><span style&equals;"color&colon; &num;008000&semi;">dependencies<&sol;span><&sol;strong> section&colon;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">build&period;gradle<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>dependencies &lbrace;<br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol;bundled language identification dependency<&sol;span><&sol;strong><br &sol;> implementation 'com&period;google&period;mlkit&colon;language-id&colon;17&period;0&period;1'<br &sol;>&rcub;<&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating Layout File<&sol;strong><&sol;span><&sol;h5> &NewLine;<p>3&period; The activity&lowbar;main&period;xml layout file defines the UI of the application&period;<&sol;p> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;<br &sol;>&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br &sol;> xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br &sol;> xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;margin&equals;"10dp"<br &sol;> tools&colon;context&equals;"&period;MainActivity"&gt&semi;<br &sol;><br &sol;> &lt&semi;EditText<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;edt&lowbar;input"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;hint&equals;"Enter yout text"<br &sol;> android&colon;inputType&equals;"text"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;identify&lowbar;language"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"30dp"<br &sol;> android&colon;text&equals;"Identify Language"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;tv&lowbar;output&lowbar;language"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;tv&lowbar;output&lowbar;language"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;output&lowbar;language" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;Button<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;possible&lowbar;language"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"10dp"<br &sol;> android&colon;text&equals;"Identify Possible Language"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;btn&lowbar;identify&lowbar;language"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;btn&lowbar;identify&lowbar;language"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;identify&lowbar;language" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;input&lowbar;text"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"30dp"<br &sol;> android&colon;textSize&equals;"18sp"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;edt&lowbar;input"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;edt&lowbar;input"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;edt&lowbar;input" &sol;&gt&semi;<br &sol;><br &sol;> &lt&semi;TextView<br &sol;> android&colon;id&equals;"&commat;&plus;id&sol;tv&lowbar;output&lowbar;language"<br &sol;> android&colon;layout&lowbar;width&equals;"match&lowbar;parent"<br &sol;> android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"<br &sol;> android&colon;layout&lowbar;marginTop&equals;"10dp"<br &sol;> android&colon;textSize&equals;"18sp"<br &sol;> app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"&commat;&plus;id&sol;tv&lowbar;input&lowbar;text"<br &sol;> app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"&commat;&plus;id&sol;tv&lowbar;input&lowbar;text"<br &sol;> app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;tv&lowbar;input&lowbar;text" &sol;&gt&semi;<br &sol;><br &sol;>&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Identify the language of a string<&sol;strong><&sol;span><&sol;h5> &NewLine;<p>4 &period; To identify the language of a string&comma; create an instance of <strong><span style&equals;"color&colon; &num;008000&semi;">LanguageIdentifier<&sol;span><&sol;strong>&comma; and then pass the string to the<span style&equals;"color&colon; &num;008000&semi;"><strong> identifyLanguage&lpar;&rpar;<&sol;strong><&sol;span> method of LanguageIdentifier&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine; &NewLine;<pre><span style&equals;"color&colon; &num;008000&semi;"><span style&equals;"color&colon; &num;000000&semi;">val languageIdentifier &equals; LanguageIdentification&period;getClient&lpar;&rpar;<&sol;span><&sol;span><span style&equals;"color&colon; &num;008000&semi;"><br &sol;><&sol;span><strong><span style&equals;"color&colon; &num;008000&semi;"><br &sol;>&sol;&sol; Identify a language&period;<&sol;span><&sol;strong><br &sol;>private fun identifyLanguage&lpar;inputText&colon; String&rpar; &lbrace;<br &sol;> languageIdentifier<br &sol;> &period;identifyLanguage&lpar;inputText&rpar;<br &sol;> &period;addOnSuccessListener &lbrace; identifiedLanguage -&gt&semi;<br &sol;> tvLanguage&period;text &equals; "Identified language&lpar;s&rpar;&colon; " &plus; identifiedLanguage<br &sol;> &rcub;<br &sol;> &period;addOnFailureListener &lbrace; e -&gt&semi;<br &sol;> Log&period;e&lpar;TAG&comma; "Language identification error"&comma; e&rpar;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<ul> &NewLine;<li>If the call succeeds&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>identified language<&sol;strong><&sol;span> is passed to the <strong><span style&equals;"color&colon; &num;008000&semi;">success<&sol;span><&sol;strong> listener or If no language is detected&comma; <span style&equals;"color&colon; &num;008000&semi;"><strong>und &lpar;undetermined&rpar;<&sol;strong><&sol;span> is passed&period;<&sol;li> &NewLine;<li>In case of error&comma; exception is passed to the <span style&equals;"color&colon; &num;008000&semi;"><strong>failure<&sol;strong><&sol;span> listener&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<h5><strong><span style&equals;"color&colon; &num;000080&semi;">Identify the possible languages of a string<&sol;span><&sol;strong><&sol;h5> &NewLine;<p>5 &period; To get the confidence values of a string&&num;8217&semi;s most likely languages&comma; get an instance of <strong><span style&equals;"color&colon; &num;008000&semi;">LanguageIdentifier<&sol;span><&sol;strong> and then pass the string to the <span style&equals;"color&colon; &num;008000&semi;"><strong>identifyPossibleLanguages&lpar;&rpar;<&sol;strong><&sol;span> method&period;<&sol;p> &NewLine;<pre>languageIdentifier &equals; LanguageIdentification&period;getClient&lpar;&rpar;<br &sol;><br &sol;><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; Identify all possible languages&period;<&sol;strong><&sol;span><br &sol;>private fun identifyPossibleLanguages&lpar;inputText&colon; String&rpar; &lbrace;<br &sol;> languageIdentifier<br &sol;> &period;identifyPossibleLanguages&lpar;inputText&rpar;<br &sol;> &period;addOnSuccessListener &lbrace; identifiedLanguages -&gt&semi;<br &sol;> var output &equals; ""<br &sol;> for &lpar;identifiedLanguage in identifiedLanguages&rpar; &lbrace;<br &sol;> output &plus;&equals; identifiedLanguage&period;languageTag &plus; " &lpar;" &plus; identifiedLanguage&period;confidence &plus; "&rpar;&comma; "<br &sol;> &rcub;<br &sol;> tvLanguage&period;text &equals;<br &sol;> "Identified language &lpar;s&rpar;&colon;" &plus; output&period;substring&lpar;0&comma; output&period;length - 2&rpar;<br &sol;> &rcub;<br &sol;> &period;addOnFailureListener &lbrace; e -&gt&semi;<br &sol;> Log&period;e&lpar;TAG&comma; "Language identification error"&comma; e&rpar; <br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<ul> &NewLine;<li>If the call succeeds&comma; a list of <strong><span style&equals;"color&colon; &num;008000&semi;">IdentifiedLanguage<&sol;span><&sol;strong> objects is passed to the success listener&period;<&sol;li> &NewLine;<li>In case of error&comma; exception is passed to the <span style&equals;"color&colon; &num;008000&semi;"><strong>failure<&sol;strong><&sol;span> listener&period;<&sol;li> &NewLine;<&sol;ul> &NewLine;<p><span style&equals;"color&colon; &num;000080&semi;"><strong> Note&colon;<&sol;strong> <&sol;span>language confidence values indicate the confidence that the entire string is in the given language&semi; ML Kit doesn&&num;8217&semi;t identify multiple languages in a single string&period;<&sol;p> &NewLine;<h5><strong><span style&equals;"color&colon; &num;000080&semi;">Complete MainActivity Code<&sol;span><&sol;strong><&sol;h5> &NewLine;<p><strong><span style&equals;"color&colon; &num;0000ff&semi;">MainActivity&period;kt<&sol;span><&sol;strong><&sol;p> &NewLine;<pre>class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;<br &sol;> private lateinit var edtInput&colon; EditText<br &sol;> private lateinit var tvLanguage&colon; TextView<br &sol;> private lateinit var tvInput&colon; TextView<br &sol;> private lateinit var languageIdentifier&colon; LanguageIdentifier<br &sol;><br &sol;> companion object &lbrace;<br &sol;> private const val TAG &equals; "MainActivity"<br &sol;> &rcub;<br &sol;><br &sol;> override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;<br &sol;> super&period;onCreate&lpar;savedInstanceState&rpar;<br &sol;> setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;<br &sol;><br &sol;> languageIdentifier &equals; LanguageIdentification&period;getClient&lpar;&rpar;<br &sol;> lifecycle&period;addObserver&lpar;languageIdentifier&rpar;<br &sol;> edtInput &equals; findViewById&lpar;R&period;id&period;edt&lowbar;input&rpar;<br &sol;><br &sol;> tvInput &equals; findViewById&lpar;R&period;id&period;tv&lowbar;input&lowbar;text&rpar;<br &sol;> tvLanguage &equals; findViewById&lpar;R&period;id&period;tv&lowbar;output&lowbar;language&rpar;<br &sol;><br &sol;> findViewById&lt&semi;Button&gt&semi;&lpar;R&period;id&period;btn&lowbar;identify&lowbar;language&rpar;&period;setOnClickListener &lbrace; &lowbar; -&gt&semi;<br &sol;> val input &equals; edtInput&period;text&period;toString&lpar;&rpar;<br &sol;><br &sol;> if &lpar;&excl;input&period;isEmpty&lpar;&rpar;&rpar; &lbrace;<br &sol;> identifyLanguage&lpar;input&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;> findViewById&lt&semi;Button&gt&semi;&lpar;R&period;id&period;btn&lowbar;possible&lowbar;language&rpar;&period;setOnClickListener &lbrace; &lowbar; -&gt&semi;<br &sol;> val input &equals; edtInput&period;text&period;toString&lpar;&rpar;<br &sol;><br &sol;> if &lpar;&excl;input&period;isEmpty&lpar;&rpar;&rpar; &lbrace;<br &sol;> identifyPossibleLanguages&lpar;input&rpar;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Identify a language&period;<&sol;span><&sol;strong><br &sol;> private fun identifyLanguage&lpar;inputText&colon; String&rpar; &lbrace;<br &sol;> tvLanguage&period;text &equals; "Waiting…"<br &sol;> languageIdentifier<br &sol;> &period;identifyLanguage&lpar;inputText&rpar;<br &sol;> &period;addOnSuccessListener &lbrace; identifiedLanguage -&gt&semi;<br &sol;> tvInput&period;text &equals; "Input Text&colon; " &plus; inputText<br &sol;> tvLanguage&period;text &equals; "Identified language&lpar;s&rpar;&colon; " &plus; identifiedLanguage<br &sol;> &rcub;<br &sol;> &period;addOnFailureListener &lbrace; e -&gt&semi;<br &sol;> Log&period;e&lpar;TAG&comma; "Language identification error"&comma; e&rpar; <br &sol;> &rcub;<br &sol;> &rcub;<br &sol;><br &sol;><strong><span style&equals;"color&colon; &num;008000&semi;"> &sol;&sol; Identify all possible languages&period;<&sol;span><&sol;strong><br &sol;> private fun identifyPossibleLanguages&lpar;inputText&colon; String&rpar; &lbrace;<br &sol;> tvLanguage&period;text &equals; "Waiting…"<br &sol;> languageIdentifier<br &sol;> &period;identifyPossibleLanguages&lpar;inputText&rpar;<br &sol;> &period;addOnSuccessListener &lbrace; identifiedLanguages -&gt&semi;<br &sol;> tvInput&period;text &equals; "Input Text&colon; " &plus; inputText<br &sol;><br &sol;> var output &equals; ""<br &sol;> for &lpar;identifiedLanguage in identifiedLanguages&rpar; &lbrace;<br &sol;> output &plus;&equals; identifiedLanguage&period;languageTag &plus; " &lpar;" &plus; identifiedLanguage&period;confidence &plus; "&rpar;&comma; "<br &sol;> &rcub;<br &sol;> tvLanguage&period;text &equals;<br &sol;> "Identified language &lpar;s&rpar;&colon;" &plus; output&period;substring&lpar;0&comma; output&period;length - 2&rpar;<br &sol;> &rcub;<br &sol;> &period;addOnFailureListener &lbrace; e -&gt&semi;<br &sol;> Log&period;e&lpar;TAG&comma; "Language identification error"&comma; e&rpar; <br &sol;> &rcub;<br &sol;> &rcub;<br &sol;>&rcub;<&sol;pre> &NewLine;<h5><span style&equals;"color&colon; &num;000080&semi;"><strong>Run the App<&sol;strong><&sol;span><&sol;h5> &NewLine;<p>When you run the app it will look like this&colon;<&sol;p> &NewLine;<p><img class&equals;"alignnone wp-image-2946" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;11&sol;Screenshot&lowbar;20211112-203111&lowbar;MLKit-LanguageID-498x1024&period;jpg" alt&equals;"" width&equals;"324" height&equals;"666" data-wp-editing&equals;"1" &sol;>        <img class&equals;"alignnone wp-image-2947" src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;11&sol;Screenshot&lowbar;20211112-203329&lowbar;MLKit-LanguageID-498x1024&period;jpg" alt&equals;"" width&equals;"325" height&equals;"669" &sol;><&sol;p> &NewLine;<p>&nbsp&semi;<&sol;p> &NewLine;&NewLine;

Exit mobile version