|
| 1 | +/* |
| 2 | + * Copyright 2016 Luca Martino. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copyFile of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include <jni.h> |
| 18 | +#include <string> |
| 19 | +#include <sstream> |
| 20 | +#include "libs/fastText/src/fasttext.h" |
| 21 | + |
| 22 | +extern "C" JNIEXPORT jlong JNICALL |
| 23 | +Java_nie_translator_rtranslator_voice_1translation_neural_1networks_translation_LanguageDetector_initFastText(JNIEnv* env, jobject /* this */) { |
| 24 | + // Return a pointer to the native FastText object |
| 25 | + auto* ft = new fasttext::FastText(); |
| 26 | + return reinterpret_cast<jlong>(ft); |
| 27 | +} |
| 28 | + |
| 29 | +extern "C" JNIEXPORT void JNICALL |
| 30 | +Java_nie_translator_rtranslator_voice_1translation_neural_1networks_translation_LanguageDetector_loadModel(JNIEnv* env, jobject /* this */, jlong ptr, jstring path) { |
| 31 | + auto* ft = reinterpret_cast<fasttext::FastText*>(ptr); |
| 32 | + const char* pathStr = env->GetStringUTFChars(path, nullptr); |
| 33 | + |
| 34 | + // Load the .bin or .ftz model |
| 35 | + ft->loadModel(pathStr); |
| 36 | + env->ReleaseStringUTFChars(path, pathStr); |
| 37 | +} |
| 38 | + |
| 39 | +extern "C" JNIEXPORT jstring JNICALL |
| 40 | +Java_nie_translator_rtranslator_voice_1translation_neural_1networks_translation_LanguageDetector_predictLanguage(JNIEnv* env, jobject /* this */, jlong ptr, jstring text, jdouble confidenceThreshold) { |
| 41 | + auto* ft = reinterpret_cast<fasttext::FastText*>(ptr); |
| 42 | + const char* textStr = env->GetStringUTFChars(text, nullptr); |
| 43 | + |
| 44 | + std::stringstream ioss(textStr); |
| 45 | + std::vector<std::pair<fasttext::real, std::string>> predictions; |
| 46 | + |
| 47 | + // Predict the single most likely language (k = 1) |
| 48 | + ft->predictLine(ioss, predictions, 1, confidenceThreshold); |
| 49 | + env->ReleaseStringUTFChars(text, textStr); |
| 50 | + |
| 51 | + if (!predictions.empty()) { |
| 52 | + std::string label = predictions[0].second; |
| 53 | + // fastText returns labels like "__label__en". We strip the prefix. |
| 54 | + std::string prefix = "__label__"; |
| 55 | + if (label.find(prefix) == 0) { |
| 56 | + label = label.substr(prefix.length()); |
| 57 | + } |
| 58 | + return env->NewStringUTF(label.c_str()); |
| 59 | + } |
| 60 | + |
| 61 | + return env->NewStringUTF("und"); |
| 62 | +} |
| 63 | + |
| 64 | +extern "C" JNIEXPORT void JNICALL |
| 65 | +Java_nie_translator_rtranslator_voice_1translation_neural_1networks_translation_LanguageDetector_release(JNIEnv* env, jobject /* this */, jlong ptr) { |
| 66 | + auto* ft = reinterpret_cast<fasttext::FastText*>(ptr); |
| 67 | + delete ft; // Prevent memory leaks |
| 68 | +} |
0 commit comments