Firebase Query request on Arabic string does not work. Works only on English string

Viewed 262

I have a code to check if the username is already taken, it works fine if the username exists in English(or Latin letters in general), but if the username already exists in Arabic, the Query doesn't find the existed username. Please see below my code.

                                //Check if the user is already exist
final Query query =  databaseReference.child("users").orderByChild("uNm").equalTo(userNameText);
                            query.addListenerForSingleValueEvent(new ValueEventListener() {
                                @Override
                                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                                    if(dataSnapshot.getChildrenCount() > 0){
                                        System.out.println ("this is user in query username :" + dataSnapshot.getValue().toString());
                                        Toast toast = Toast.makeText(ProfileEditing.this, "    The username is already taken    ", Toast.LENGTH_LONG);

The userNameText is the new typed username that the user wants to use. if userNameText is in English and username already exists the query works and finds it. if userNameText is in Arabic and the username already exist, the query doesn't work and doesn't' find it.

Appreciate if someone could help!

1 Answers

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

Related