-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add OfflineDiacritization JAVA bindings #3669
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
csukuangfj
merged 1 commit into
k2-fsa:master
from
matiaslin:offline-diacritization-java-bindings
Jun 12, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| // Copyright 2026 Matias Lin | ||
|
|
||
| // This file shows how to use a CATT diacritization model to add diacritics to Arabic text. | ||
| import com.k2fsa.sherpa.onnx.*; | ||
|
|
||
| public class OfflineAddDiacritics { | ||
| public static void main(String[] args) { | ||
| // please download the model from | ||
| // https://github.com/abjadai/catt/releases/download/v2/eo_model_onnx.zip | ||
| String cattEncoder = "./catt_eo_model_onnx/encoder.onnx"; | ||
| String cattDecoder = "./catt_eo_model_onnx/decoder.onnx"; | ||
| OfflineDiacritizationModelConfig modelConfig = | ||
| OfflineDiacritizationModelConfig.builder() | ||
| .setCattEncoder(cattEncoder) | ||
| .setCattDecoder(cattDecoder) | ||
| .setNumThreads(1) | ||
| .setDebug(true) | ||
| .build(); | ||
| OfflineDiacritizationConfig config = | ||
| OfflineDiacritizationConfig.builder().setModel(modelConfig).build(); | ||
|
|
||
| OfflineDiacritization diacrt = new OfflineDiacritization(config); | ||
|
|
||
| String[] sentences = | ||
| new String[] { | ||
| "وقالت مجلة نيوزويك الأمريكية التحديث الجديد ل إنستجرام يمكن أن يساهم في إيقاف وكشف الحسابات المزورة بسهولة شديدة", | ||
| "اللغة العربية من أقدم اللغات السامية", | ||
| }; | ||
|
|
||
| System.out.println("---"); | ||
| for (String text : sentences) { | ||
| String out = diacrt.addDiacritics(text); | ||
| System.out.printf("Input: %s\n", text); | ||
| System.out.printf("Output: %s\n", out); | ||
| System.out.println("---"); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #!/usr/bin/env bash | ||
|
|
||
| set -ex | ||
|
|
||
| if [[ ! -f ../build/lib/libsherpa-onnx-jni.dylib && ! -f ../build/lib/libsherpa-onnx-jni.so ]]; then | ||
| mkdir -p ../build | ||
| pushd ../build | ||
| cmake \ | ||
| -DSHERPA_ONNX_ENABLE_PYTHON=OFF \ | ||
| -DSHERPA_ONNX_ENABLE_TESTS=OFF \ | ||
| -DSHERPA_ONNX_ENABLE_CHECK=OFF \ | ||
| -DBUILD_SHARED_LIBS=ON \ | ||
| -DSHERPA_ONNX_ENABLE_PORTAUDIO=OFF \ | ||
| -DSHERPA_ONNX_ENABLE_JNI=ON \ | ||
| .. | ||
|
|
||
| make -j4 | ||
| ls -lh lib | ||
| popd | ||
| fi | ||
|
|
||
| if [ ! -f ../sherpa-onnx/java-api/build/sherpa-onnx.jar ]; then | ||
| pushd ../sherpa-onnx/java-api | ||
| make | ||
| popd | ||
| fi | ||
|
|
||
| if [[ ! -f "./catt_eo_model_onnx/encoder.onnx" || ! -f "./catt_eo_model_onnx/decoder.onnx" ]]; then | ||
| curl -SL -O https://github.com/abjadai/catt/releases/download/v2/eo_model_onnx.zip | ||
| unzip eo_model_onnx.zip -d catt_eo_model_onnx | ||
| rm eo_model_onnx.zip | ||
| fi | ||
|
|
||
| java \ | ||
| -Djava.library.path=$PWD/../build/lib \ | ||
| -cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \ | ||
| ./OfflineAddDiacritics.java | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/OfflineDiacritization.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // Copyright 2026 Matias Lin | ||
|
|
||
| package com.k2fsa.sherpa.onnx; | ||
|
|
||
| public class OfflineDiacritization { | ||
|
matiaslin marked this conversation as resolved.
|
||
| private long ptr = 0; | ||
|
|
||
| public OfflineDiacritization(OfflineDiacritizationConfig config) { | ||
| LibraryLoader.maybeLoad(); | ||
| ptr = newFromFile(config); | ||
| if (ptr == 0) { | ||
| throw new IllegalArgumentException("Invalid OfflineDiacritizationConfig: failed to create native OfflineDiacritization"); | ||
| } | ||
| } | ||
|
matiaslin marked this conversation as resolved.
|
||
|
|
||
| public String addDiacritics(String text) { | ||
| return addDiacritics(ptr, text); | ||
| } | ||
|
matiaslin marked this conversation as resolved.
|
||
|
|
||
| @Override | ||
| protected void finalize() throws Throwable { | ||
| release(); | ||
| } | ||
|
|
||
| // You'd better call it manually if it is not used anymore | ||
|
matiaslin marked this conversation as resolved.
|
||
| public void release() { | ||
| if (this.ptr == 0) { | ||
| return; | ||
| } | ||
| delete(this.ptr); | ||
| this.ptr = 0; | ||
| } | ||
|
matiaslin marked this conversation as resolved.
|
||
|
|
||
| private native void delete(long ptr); | ||
|
|
||
| private native long newFromFile(OfflineDiacritizationConfig config); | ||
|
|
||
| private native String addDiacritics(long ptr, String text); | ||
| } | ||
32 changes: 32 additions & 0 deletions
32
sherpa-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/OfflineDiacritizationConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Copyright 2026 Matias Lin | ||
|
|
||
| package com.k2fsa.sherpa.onnx; | ||
|
|
||
| public class OfflineDiacritizationConfig { | ||
| private final OfflineDiacritizationModelConfig model; | ||
|
|
||
| private OfflineDiacritizationConfig(Builder builder) { | ||
| this.model = builder.model; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public OfflineDiacritizationModelConfig getModel() { | ||
| return model; | ||
| } | ||
|
|
||
| public static class Builder { | ||
| private OfflineDiacritizationModelConfig model = OfflineDiacritizationModelConfig.builder().build(); | ||
|
|
||
| public OfflineDiacritizationConfig build() { | ||
| return new OfflineDiacritizationConfig(this); | ||
| } | ||
|
|
||
| public Builder setModel(OfflineDiacritizationModelConfig model) { | ||
| this.model = model; | ||
| return this; | ||
| } | ||
| } | ||
| } |
68 changes: 68 additions & 0 deletions
68
...a-onnx/java-api/src/main/java/com/k2fsa/sherpa/onnx/OfflineDiacritizationModelConfig.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright 2026 Matias Lin | ||
|
|
||
| package com.k2fsa.sherpa.onnx; | ||
|
|
||
| public class OfflineDiacritizationModelConfig { | ||
| private final String cattEncoder; | ||
| private final String cattDecoder; | ||
| private final int numThreads; | ||
| private final boolean debug; | ||
| private final String provider; | ||
|
|
||
| private OfflineDiacritizationModelConfig(Builder builder) { | ||
| this.cattEncoder = builder.cattEncoder; | ||
| this.cattDecoder = builder.cattDecoder; | ||
| this.numThreads = builder.numThreads; | ||
| this.debug = builder.debug; | ||
| this.provider = builder.provider; | ||
| } | ||
|
|
||
| public static Builder builder() { | ||
| return new Builder(); | ||
| } | ||
|
|
||
| public String getCattEncoder() { | ||
| return cattEncoder; | ||
| } | ||
|
|
||
| public String getCattDecoder() { | ||
| return cattDecoder; | ||
| } | ||
|
matiaslin marked this conversation as resolved.
|
||
|
|
||
| public static class Builder { | ||
| private String cattEncoder = ""; | ||
| private String cattDecoder = ""; | ||
| private int numThreads = 1; | ||
| private boolean debug = true; | ||
| private String provider = "cpu"; | ||
|
|
||
| public OfflineDiacritizationModelConfig build() { | ||
| return new OfflineDiacritizationModelConfig(this); | ||
| } | ||
|
|
||
| public Builder setCattEncoder(String cattEncoder) { | ||
| this.cattEncoder = cattEncoder; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setCattDecoder(String cattDecoder) { | ||
| this.cattDecoder = cattDecoder; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setNumThreads(int numThreads) { | ||
| this.numThreads = numThreads; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setDebug(boolean debug) { | ||
| this.debug = debug; | ||
| return this; | ||
| } | ||
|
|
||
| public Builder setProvider(String provider) { | ||
| this.provider = provider; | ||
| return this; | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| // sherpa-onnx/jni/offline-diacritization.cc | ||
| // | ||
| // Copyright (c) 2026 Matias Lin | ||
|
|
||
| #include "sherpa-onnx/csrc/offline-diacritization.h" | ||
|
|
||
| #include <string> | ||
|
|
||
| #include "sherpa-onnx/csrc/macros.h" | ||
| #include "sherpa-onnx/jni/common.h" | ||
|
|
||
| namespace sherpa_onnx { | ||
|
|
||
| static OfflineDiacritizationConfig GetOfflineDiacritizationConfig( | ||
| JNIEnv *env, jobject config, bool *ok) { | ||
| OfflineDiacritizationConfig ans; | ||
|
|
||
| jclass cls = env->GetObjectClass(config); | ||
| jfieldID fid; | ||
|
|
||
| fid = env->GetFieldID( | ||
| cls, "model", "Lcom/k2fsa/sherpa/onnx/OfflineDiacritizationModelConfig;"); | ||
| jobject model_config = env->GetObjectField(config, fid); | ||
| jclass model_config_cls = env->GetObjectClass(model_config); | ||
|
matiaslin marked this conversation as resolved.
|
||
|
|
||
|
matiaslin marked this conversation as resolved.
|
||
| SHERPA_ONNX_JNI_READ_STRING(ans.model.catt_encoder, cattEncoder, | ||
| model_config_cls, model_config); | ||
|
|
||
| SHERPA_ONNX_JNI_READ_STRING(ans.model.catt_decoder, cattDecoder, | ||
| model_config_cls, model_config); | ||
|
|
||
| SHERPA_ONNX_JNI_READ_INT(ans.model.num_threads, numThreads, model_config_cls, | ||
| model_config); | ||
|
|
||
| SHERPA_ONNX_JNI_READ_BOOL(ans.model.debug, debug, model_config_cls, | ||
| model_config); | ||
|
|
||
| SHERPA_ONNX_JNI_READ_STRING(ans.model.provider, provider, model_config_cls, | ||
| model_config); | ||
|
|
||
| *ok = true; | ||
| return ans; | ||
| } | ||
|
|
||
| } // namespace sherpa_onnx | ||
|
|
||
| SHERPA_ONNX_EXTERN_C | ||
| JNIEXPORT jlong JNICALL | ||
| Java_com_k2fsa_sherpa_onnx_OfflineDiacritization_newFromAsset( | ||
| JNIEnv *env, jobject /*obj*/, jobject asset_manager, jobject _config) { | ||
| #if __ANDROID_API__ >= 9 | ||
| AAssetManager *mgr = AAssetManager_fromJava(env, asset_manager); | ||
| if (!mgr) { | ||
| SHERPA_ONNX_LOGE("Failed to get asset manager: %p", mgr); | ||
| return 0; | ||
| } | ||
| #endif | ||
| bool ok = false; | ||
| auto config = sherpa_onnx::GetOfflineDiacritizationConfig(env, _config, &ok); | ||
|
|
||
| if (!ok) { | ||
| SHERPA_ONNX_LOGE("Please read the error message carefully"); | ||
| return 0; | ||
| } | ||
|
|
||
| SHERPA_ONNX_LOGE("config:\n%s", config.ToString().c_str()); | ||
|
|
||
| auto model = new sherpa_onnx::OfflineDiacritization( | ||
| #if __ANDROID_API__ >= 9 | ||
| mgr, | ||
| #endif | ||
| config); | ||
|
|
||
| return (jlong)model; | ||
| } | ||
|
|
||
| SHERPA_ONNX_EXTERN_C | ||
| JNIEXPORT jlong JNICALL | ||
| Java_com_k2fsa_sherpa_onnx_OfflineDiacritization_newFromFile(JNIEnv *env, | ||
| jobject /*obj*/, | ||
| jobject _config) { | ||
| bool ok = false; | ||
| auto config = sherpa_onnx::GetOfflineDiacritizationConfig(env, _config, &ok); | ||
|
|
||
| if (!ok) { | ||
| SHERPA_ONNX_LOGE("Please read the error message carefully"); | ||
| return 0; | ||
| } | ||
|
|
||
| SHERPA_ONNX_LOGE("config:\n%s", config.ToString().c_str()); | ||
|
|
||
| if (!config.Validate()) { | ||
| SHERPA_ONNX_LOGE("Errors found in config!"); | ||
| return 0; | ||
| } | ||
|
|
||
| auto model = new sherpa_onnx::OfflineDiacritization(config); | ||
|
|
||
| return (jlong)model; | ||
| } | ||
|
|
||
| SHERPA_ONNX_EXTERN_C | ||
| JNIEXPORT void JNICALL Java_com_k2fsa_sherpa_onnx_OfflineDiacritization_delete( | ||
| JNIEnv * /*env*/, jobject /*obj*/, jlong ptr) { | ||
| delete reinterpret_cast<sherpa_onnx::OfflineDiacritization *>(ptr); | ||
| } | ||
|
|
||
| SHERPA_ONNX_EXTERN_C | ||
| JNIEXPORT jstring JNICALL | ||
| Java_com_k2fsa_sherpa_onnx_OfflineDiacritization_addDiacritics(JNIEnv *env, | ||
| jobject /*obj*/, | ||
| jlong ptr, | ||
| jstring text) { | ||
| auto diacrt = | ||
| reinterpret_cast<const sherpa_onnx::OfflineDiacritization *>(ptr); | ||
|
|
||
| const char *ptext = env->GetStringUTFChars(text, nullptr); | ||
|
matiaslin marked this conversation as resolved.
|
||
|
|
||
| std::string result = diacrt->AddDiacritics(ptext); | ||
|
|
||
| env->ReleaseStringUTFChars(text, ptext); | ||
|
|
||
| return SafeNewStringUTF(env, result); | ||
| } | ||
|
matiaslin marked this conversation as resolved.
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.