Return language detection result

pull/193/head
M66B 4 years ago
parent 302f223212
commit 48d41afd04

@ -45,7 +45,7 @@ public class TextHelper {
System.loadLibrary("fairemail");
}
private static native String jni_detect_language(byte[] octets);
private static native DetectResult jni_detect_language(byte[] octets);
static Locale detectLanguage(Context context, String text) {
// Why not ML kit? https://developers.google.com/ml-kit/terms
@ -54,8 +54,9 @@ public class TextHelper {
if (BuildConfig.DEBUG) {
// https://github.com/google/cld3
String lang = jni_detect_language(text.getBytes());
return Locale.forLanguageTag(lang);
DetectResult result = jni_detect_language(text.getBytes());
Log.i("Language=" + result);
return Locale.forLanguageTag(result.language);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
@ -121,4 +122,23 @@ public class TextHelper {
return tcm.getTextClassifier().suggestConversationActions(request);
}
private static class DetectResult {
String language;
float probability;
boolean is_reliable;
float proportion;
DetectResult(String language, float probability, boolean is_reliable, float proportion) {
this.language = language;
this.probability = probability;
this.is_reliable = is_reliable;
this.proportion = proportion;
}
@Override
public String toString() {
return language + " p=" + probability + " r=" + is_reliable + " pr=" + proportion;
}
}
}

@ -21,7 +21,8 @@ void log_android(int prio, const char *fmt, ...) {
}
extern "C" JNIEXPORT jobject JNICALL
Java_eu_faircode_email_CharsetHelper_jni_1detect_1charset(JNIEnv *env, jclass type, jbyteArray _octets) {
Java_eu_faircode_email_CharsetHelper_jni_1detect_1charset(JNIEnv *env, jclass type,
jbyteArray _octets) {
int len = env->GetArrayLength(_octets);
jbyte *octets = env->GetByteArrayElements(_octets, nullptr);
@ -59,8 +60,9 @@ Java_eu_faircode_email_CharsetHelper_jni_1detect_1charset(JNIEnv *env, jclass ty
}
extern "C"
JNIEXPORT jstring JNICALL
Java_eu_faircode_email_TextHelper_jni_1detect_1language(JNIEnv *env, jclass clazz, jbyteArray _octets) {
JNIEXPORT jobject JNICALL
Java_eu_faircode_email_TextHelper_jni_1detect_1language(JNIEnv *env, jclass clazz,
jbyteArray _octets) {
int len = env->GetArrayLength(_octets);
jbyte *octets = env->GetByteArrayElements(_octets, nullptr);
@ -71,7 +73,15 @@ Java_eu_faircode_email_TextHelper_jni_1detect_1language(JNIEnv *env, jclass claz
env->ReleaseByteArrayElements(_octets, octets, JNI_ABORT);
return env->NewStringUTF(result.language.c_str());
jclass cls = env->FindClass("eu/faircode/email/TextHelper$DetectResult");
jmethodID ctor = env->GetMethodID(cls, "<init>", "(Ljava/lang/String;FZF)V");
jstring jlanguage = env->NewStringUTF(result.language.c_str());
return env->NewObject(
cls, ctor,
jlanguage,
(jfloat) result.probability,
(jint) result.is_reliable,
(jfloat) result.is_reliable);
}
extern "C"

Loading…
Cancel
Save