Gradle KTS: Diacritics

This example shows how to add diacritics to text. The model used here is the CATT Esperanto model.

It is the Java version of OfflineAddDiacritics.java in sherpa-onnx/java-api-examples.

Requirements

  • JDK 11 or above

  • Gradle 8.x+ (or use the included Gradle wrapper, no separate install needed)

Warning

The sherpa-onnx Java packages are hosted on JitPack, so you need to add the JitPack repository.

Step 1. Create the project directory

mkdir sherpa-onnx-java-demo
cd sherpa-onnx-java-demo

Step 2. Create the build files

Save the following as build.gradle.kts:

plugins {
    application
    java
}

application {
    mainClass.set("OfflineAddDiacritics")
}

repositories {
    mavenCentral()
    // sherpa-onnx packages are hosted on JitPack
    maven { url = uri("https://jitpack.io") }
}

dependencies {
    implementation("com.github.k2-fsa:sherpa-onnx:v2.0.1")
}

java {
    sourceCompatibility = JavaVersion.VERSION_11
    targetCompatibility = JavaVersion.VERSION_11
}

Save the following as settings.gradle.kts:

rootProject.name = "sherpa-onnx-java-example"

Step 3. Download the model

wget 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

Step 4. Save the code

Save the following as src/main/java/OfflineAddDiacritics.java:

// 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("---");
    }
  }
}

Step 5. Check the project layout

Your project directory should now look like this:

$ tree .
.
├── build.gradle.kts
├── catt_eo_model_onnx
│   ├── decoder.onnx
│   └── encoder.onnx
├── settings.gradle.kts
└── src
    └── main
        └── java
            └── OfflineAddDiacritics.java

5 directories, 5 files

$ ls -lh
total 16
-rw-r--r--@ 1 fangjun  wheel   413B 19 Nov 14:00 build.gradle.kts
drwxr-xr-x@ 4 fangjun  wheel   128B 19 Nov 10:25 catt_eo_model_onnx
-rw-r--r--@ 1 fangjun  wheel    45B 19 Nov 14:00 settings.gradle.kts
drwxr-xr-x@ 3 fangjun  wheel    96B 19 Nov 14:00 src

Step 6. Build and run

# Generate the Gradle wrapper (only once; requires Gradle to be installed)
gradle wrapper

./gradlew run

Output

1 actionable task: 1 executed
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.6.1/userguide/configuration_cache_enabling.html

OfflineDiacritizationConfig(model=OfflineDiacritizationModelConfig(catt_encoder="./catt_eo_model_onnx/encoder.onnx", catt_decoder="./catt_eo_model_onnx/decoder.onnx", num_threads=1, debug=True, provider="cpu"))
CATT encoder inputs: src src_mask
CATT encoder outputs: encoder_output

CATT decoder inputs: enc_src
CATT decoder outputs: decoder_output

---
Input: وقالت مجلة نيوزويك الأمريكية التحديث الجديد ل إنستجرام يمكن أن يساهم في إيقاف وكشف الحسابات المزورة بسهولة شديدة
Output: وَقَالَتْ مَجَلَّةُ نِيُوزْوِيكَ الْأَمْرِيكِيَّةُ التَّحْدِيثَ الْجَدِيدُ لِ إِنسِتِجْرَامٍ يُمْكِنُ أنْ يُسَاهِمَ فِي إِيقَافِ وَكَشْفِ الْحِسَابَاتِ الْمُزَوَّرَةِ بِسُهُولَةٍ شَدِيدَةٍ
---
Input: اللغة العربية من أقدم اللغات السامية
Output: اللُّغَةُ الْعَرَبِيَّةُ مِنْ أقْدَمِ اللُّغَاتِ السَّامِيَةِ
---

2 actionable tasks: 2 executed
Consider enabling configuration cache to speed up this build: https://docs.gradle.org/9.6.1/userguide/configuration_cache_enabling.html