Skip to content
Permalink
Browse files

Create gmdevbloggamemakerfirebasemltranslate.java

  • Loading branch information...
Fritsbie committed Nov 4, 2019
1 parent 1dadd96 commit 07a467ffe5f66ca49cc4eff6d1a5ebed2f6a9440
Showing with 262 additions and 0 deletions.
  1. +262 −0 android/gmdevbloggamemakerfirebasemltranslate.java
@@ -0,0 +1,262 @@
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 androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MediatorLiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;

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;
import com.google.android.gms.tasks.Continuation;
import com.google.android.gms.tasks.Task;
import com.google.android.gms.tasks.Tasks;

import java.util.Map;
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;

//Firebase ML Kit Packages
import com.google.firebase.ml.common.modeldownload.FirebaseModelDownloadConditions;
import com.google.firebase.ml.common.modeldownload.FirebaseModelManager;
import com.google.firebase.ml.naturallanguage.FirebaseNaturalLanguage;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateLanguage;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslateRemoteModel;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslator;
import com.google.firebase.ml.naturallanguage.translate.FirebaseTranslatorOptions;



public class gmdevbloggamemakerfirebasemltranslate {

final Map < String, FirebaseTranslator > translatorBase = new HashMap < String, FirebaseTranslator > ();
//Game Maker Async Event Code
private static final int EVENT_OTHER_SOCIAL = 70;
public static boolean mlDeb = false;

public void firebase_ml_translator_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) {
mlDeb = true;
} else {
mlDeb = false;
}


}


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

public void firebase_ml_translator_init() {

Log.i("yoyo", "Game Maker Firebase Machine Learning - Translator 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_translator_create(final String translatorID, final String sourceLang, final String targetLang) {

int sourceLanguage = FirebaseTranslateLanguage.languageForLanguageCode(sourceLang);
int targetLanguage = FirebaseTranslateLanguage.languageForLanguageCode(targetLang);

FirebaseTranslatorOptions options =
new FirebaseTranslatorOptions.Builder()
.setSourceLanguage(sourceLanguage)
.setTargetLanguage(targetLanguage)
.build();

final FirebaseTranslator langTranslator = FirebaseNaturalLanguage.getInstance().getTranslator(options);

FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder().build();

langTranslator.downloadModelIfNeeded(conditions)
.addOnSuccessListener(
new OnSuccessListener < Void > () {
@Override
public void onSuccess(Void v) {
debugMessage("Translator", "The translation model for <" + sourceLang + "/" + targetLang + "> is downloaded and can be used hereafter.");
translatorBase.put(translatorID, langTranslator);

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_translator_create");
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 1);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
debugMessage("Translator", "Error happened during language translator downloading proces. Error code : " + e.toString());

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




}

public void firebase_ml_translator_remove(String translatorID) {

translatorBase.remove(translatorID);
}


public void firebase_ml_translator_translate(String translatorID, String text) {
//translate source text to english
translatorBase.get(translatorID).translate(text)
.addOnSuccessListener(
new OnSuccessListener < String > () {
@Override
public void onSuccess(@NonNull String translatedText) {
debugMessage("Translator", "The text is translated as " + translatedText);

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_translator_translate");
RunnerJNILib.DsMapAddString(dsMapIndex, "result", translatedText);
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 1);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
}
})
.addOnFailureListener(
new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
debugMessage("Translator", "Error happened during translation. Error code : " + e.toString());

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_translator_translate");
RunnerJNILib.DsMapAddString(dsMapIndex, "result", "N/A");
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 0);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
}
});

}

public void firebase_ml_translator_predownload_model(final String langCodeString) {

debugMessage("Translator", "Attempting to DOWNLOAD the <" + langCodeString + "> language model...");
int languageCode = FirebaseTranslateLanguage.languageForLanguageCode(langCodeString);

FirebaseTranslateRemoteModel model = new FirebaseTranslateRemoteModel.Builder(languageCode).build();
FirebaseModelDownloadConditions conditions = new FirebaseModelDownloadConditions.Builder().build();
FirebaseModelManager.getInstance().download(model, conditions)
.addOnSuccessListener(new OnSuccessListener < Void > () {
@Override
public void onSuccess(Void v) {
debugMessage("Translator", "Predownloading the model <" + langCodeString + "> has been completed!");

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_translator_predownload");
RunnerJNILib.DsMapAddString(dsMapIndex, "language", langCodeString);
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 1);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);

}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
debugMessage("Translator", "ERROR happened during predownloading the model <" + langCodeString + ">. Error code : " + e.toString());

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_translator_predownload");
RunnerJNILib.DsMapAddString(dsMapIndex, "language", langCodeString);
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 0);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
}
});
}


public void firebase_ml_translator_delete_model(final String langCodeString) {

debugMessage("Translator", "Attempting to DELETE the <" + langCodeString + "> language model...");
FirebaseTranslateRemoteModel model = new FirebaseTranslateRemoteModel.Builder(FirebaseTranslateLanguage.DE).build();
FirebaseModelManager.getInstance().deleteDownloadedModel(model)
.addOnSuccessListener(new OnSuccessListener < Void > () {
@Override
public void onSuccess(Void v) {
debugMessage("Translator", "Model for the language <" + langCodeString + "> is deleted!");

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_translator_delete");
RunnerJNILib.DsMapAddString(dsMapIndex, "language", langCodeString);
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 1);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
debugMessage("Translator", "ERROR happened during deleting the model <" + langCodeString + ">. Error code : " + e.toString());

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_translator_delete");
RunnerJNILib.DsMapAddString(dsMapIndex, "language", langCodeString);
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 0);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
}
});
}

public void firebase_ml_translator_get_downloaded_models() {
debugMessage("Translator", "Attempting to get downloaded models...");
FirebaseModelManager.getInstance().getDownloadedModels(FirebaseTranslateRemoteModel.class).addOnSuccessListener(
new OnSuccessListener < Set < FirebaseTranslateRemoteModel >> () {
@Override
public void onSuccess(Set < FirebaseTranslateRemoteModel > remoteModels) {

int dsMapIndex = RunnerJNILib.jCreateDsMap(null, null, null);


debugMessage("Translator", "Downloaded models are;");
List < String > modelCodes = new ArrayList < > (remoteModels.size());
double line = 0;
for (FirebaseTranslateRemoteModel model: remoteModels) {
String langCD = model.getLanguageCode();
debugMessage("", langCD);
RunnerJNILib.DsMapAddString(dsMapIndex, "language_[" + line + "]", langCD);
line += 1;
}

line += 1;

RunnerJNILib.DsMapAddDouble(dsMapIndex, "total", line);
RunnerJNILib.DsMapAddString(dsMapIndex, "category", "ml_translator_downloaded_models");
RunnerJNILib.DsMapAddDouble(dsMapIndex, "status", 1);
RunnerJNILib.CreateAsynEventWithDSMap(dsMapIndex, EVENT_OTHER_SOCIAL);
}
});
}





}

0 comments on commit 07a467f

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