cld3 binding

pull/193/head
M66B 4 years ago
parent 01328483f5
commit 77674b0730

@ -41,11 +41,23 @@ import java.util.Locale;
import java.util.Set;
public class TextHelper {
static {
System.loadLibrary("fairemail");
}
private static native String jni_language(byte[] octets);
static Locale detectLanguage(Context context, String text) {
// Why not ML kit? https://developers.google.com/ml-kit/terms
if (TextUtils.isEmpty(text))
return null;
if (BuildConfig.DEBUG) {
// https://github.com/google/cld3
String lang = jni_language(text.getBytes());
return Locale.forLanguageTag(lang);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q)
return null;

@ -7,6 +7,7 @@
#include <netinet/tcp.h>
#include "compact_enc_det/compact_enc_det.h"
#include "cld_3/src/nnet_language_identifier.h"
void log_android(int prio, const char *fmt, ...) {
if (prio >= ANDROID_LOG_DEBUG) {
@ -57,6 +58,22 @@ Java_eu_faircode_email_CharsetHelper_jni_1detect(JNIEnv *env, jclass type, jbyte
(jboolean) is_reliable);
}
extern "C"
JNIEXPORT jstring JNICALL
Java_eu_faircode_email_TextHelper_jni_1language(JNIEnv *env, jclass clazz, jbyteArray _octets) {
int len = env->GetArrayLength(_octets);
jbyte *octets = env->GetByteArrayElements(_octets, nullptr);
std::string text(reinterpret_cast<char const *>(octets), len);
chrome_lang_id::NNetLanguageIdentifier lang_id(0, 1000);
const chrome_lang_id::NNetLanguageIdentifier::Result result = lang_id.FindLanguage(text);
env->ReleaseByteArrayElements(_octets, octets, JNI_ABORT);
return env->NewStringUTF(result.language.c_str());
}
extern "C"
JNIEXPORT jint JNICALL
Java_eu_faircode_email_EmailService_jni_1socket_1keep_1alive(

Loading…
Cancel
Save