Add Kotlin and Java API for homophone replacer (#2166)

* Add Kotlin API for homonphone replacer

* Add Java API for homonphone replacer
This commit is contained in:
Fangjun Kuang
2025-04-29 22:55:21 +08:00
committed by GitHub
parent 50b5329572
commit e537094b07
21 changed files with 325 additions and 1 deletions

View File

@@ -0,0 +1,58 @@
// Copyright 2025 Xiaomi Corporation
// This file shows how to use an offline SenseVoice model,
// i.e., non-streaming SenseVoice model
// to decode files with homophone replacer.
import com.k2fsa.sherpa.onnx.*;
public class NonStreamingDecodeFileSenseVoiceWithHr {
public static void main(String[] args) {
// please refer to
// https://k2-fsa.github.io/sherpa/onnx/sense-voice/index.html
// to download model files
String model = "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/model.int8.onnx";
String tokens = "./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt";
String waveFilename = "./test-hr.wav";
WaveReader reader = new WaveReader(waveFilename);
OfflineSenseVoiceModelConfig senseVoice =
OfflineSenseVoiceModelConfig.builder().setModel(model).build();
OfflineModelConfig modelConfig =
OfflineModelConfig.builder()
.setSenseVoice(senseVoice)
.setTokens(tokens)
.setNumThreads(1)
.setDebug(true)
.build();
HomophoneReplacerConfig hr =
HomophoneReplacerConfig.builder()
.setDictDir("./dict")
.setLexicon("./lexicon.txt")
.setRuleFsts("./replace.fst")
.build();
OfflineRecognizerConfig config =
OfflineRecognizerConfig.builder()
.setOfflineModelConfig(modelConfig)
.setDecodingMethod("greedy_search")
.setHr(hr)
.build();
OfflineRecognizer recognizer = new OfflineRecognizer(config);
OfflineStream stream = recognizer.createStream();
stream.acceptWaveform(reader.getSamples(), reader.getSampleRate());
recognizer.decode(stream);
String text = recognizer.getResult(stream).getText();
System.out.printf("filename:%s\nresult:%s\n", waveFilename, text);
stream.release();
recognizer.release();
}
}

View File

@@ -31,6 +31,11 @@ This directory contains examples for the JAVA API of sherpa-onnx.
./run-non-streaming-decode-file-nemo.sh
```
## Non-Streaming Speech recognition with homophone replacer
```bash
./run-non-streaming-decode-file-sense-voice-with-hr.sh
```
## Non-Streaming text-to-speech

View File

@@ -0,0 +1,47 @@
#!/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 ./sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17/tokens.txt ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17.tar.bz2
tar xvf sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17.tar.bz2
rm sherpa-onnx-sense-voice-zh-en-ja-ko-yue-2024-07-17.tar.bz2
fi
if [ ! -d dict ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/hr-files/dict.tar.bz2
tar xf dict.tar.bz2
rm dict.tar.bz2
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/hr-files/replace.fst
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/hr-files/test-hr.wav
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/hr-files/lexicon.txt
fi
java \
-Djava.library.path=$PWD/../build/lib \
-cp ../sherpa-onnx/java-api/build/sherpa-onnx.jar \
NonStreamingDecodeFileSenseVoiceWithHr.java