Skip to content
Permalink
Browse files

Create gmdevbloggamemakerfirebasemlidentifier.java

  • Loading branch information
Fritsbie committed Nov 9, 2019
1 parent deebf76 commit 0c6e065309c9d3c25afe2ef4a1619927c5e2650a
Showing with 92 additions and 0 deletions.
  1. +92 −0 android/gmdevbloggamemakerfirebasemlidentifier.java
@@ -0,0 +1,92 @@
package ${YYAndroidPackageName};

//Game Maker Studio 2 Packages
import ${YYAndroidPackageName}.R;
import com.yoyogames.runner.RunnerJNILib;
import ${YYAndroidPackageName}.RunnerActivity;

//Some Android & Java Packages
import android.util.Log;
import java.lang.String;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;


import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
//Firebase ML Kit Packages
import com.google.firebase.ml.naturallanguage.FirebaseNaturalLanguage;
import com.google.firebase.ml.naturallanguage.languageid.FirebaseLanguageIdentification;
import com.google.firebase.ml.naturallanguage.languageid.IdentifiedLanguage;



public class gmdevbloggamemakerfirebasemlidentifier extends RunnerActivity {

private static final int EVENT_OTHER_SOCIAL = 70;
public static boolean mlIdeB = false;

public void firebase_ml_identifier_debug_mode(double arg0) {

//You might ask wtf is this code. With this function, I imitate converting GML boolean to Java boolean.
if (arg0 >= 0.5) {
mlIdeB = true;
} else {
mlIdeB = false;
}


}


private void debugMessage(String tool, String message) {
if (mlIdeB == true) {
Log.i("yoyo", "Game Maker Firebase | ML Kit <" + tool + "> : " + message);
}
}

public void firebase_ml_identifier_init() {

Log.i("yoyo", "Game Maker Firebase Machine Learning - Identifier Extension. For more information check the blog post : https://gmdevblog.com/game-maker-firebase-ml-kit-translate-extension/");
Log.i("yoyo", "For any questions and suggestions, please comment to the post. Credits would be appreciated. Have fun!");

}

public void firebase_ml_identifier_identify_language(final String text) {

FirebaseLanguageIdentification languageIdentification = FirebaseNaturalLanguage.getInstance().getLanguageIdentification();
languageIdentification.identifyLanguage(text).addOnSuccessListener(
this, new OnSuccessListener < String > () {
@Override
public void onSuccess(String s) {

debugMessage("Identifier", "Language identification for the text <" + text + "> is resulted as : " + s);

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_identifier_identify");
RunnerJNILib.DsMapAddString(dsMapIndex, "result", s);
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 1);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);

}
})
.addOnFailureListener(this, new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
debugMessage("Identifier", "Error happened during the Language identification for the text <" + text + ">. Error code : " + e.toString());

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_identifier_identify");
RunnerJNILib.DsMapAddString(dsMapIndex, "result", e.toString());
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 0);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
}
});

}


}

0 comments on commit 0c6e065

Please sign in to comment.
You can’t perform that action at this time.