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:
1
kotlin-api-examples/HomophoneReplacerConfig.kt
Symbolic link
1
kotlin-api-examples/HomophoneReplacerConfig.kt
Symbolic link
@@ -0,0 +1 @@
|
||||
../sherpa-onnx/kotlin-api/HomophoneReplacerConfig.kt
|
||||
@@ -87,6 +87,7 @@ function testOnlineAsr() {
|
||||
kotlinc-jvm -include-runtime -d $out_filename \
|
||||
test_online_asr.kt \
|
||||
FeatureConfig.kt \
|
||||
HomophoneReplacerConfig.kt \
|
||||
OnlineRecognizer.kt \
|
||||
OnlineStream.kt \
|
||||
WaveReader.kt \
|
||||
@@ -244,6 +245,7 @@ function testOfflineAsr() {
|
||||
kotlinc-jvm -include-runtime -d $out_filename \
|
||||
test_offline_asr.kt \
|
||||
FeatureConfig.kt \
|
||||
HomophoneReplacerConfig.kt \
|
||||
OfflineRecognizer.kt \
|
||||
OfflineStream.kt \
|
||||
WaveReader.kt \
|
||||
@@ -272,6 +274,7 @@ function testInverseTextNormalizationOfflineAsr() {
|
||||
kotlinc-jvm -include-runtime -d $out_filename \
|
||||
test_itn_offline_asr.kt \
|
||||
FeatureConfig.kt \
|
||||
HomophoneReplacerConfig.kt \
|
||||
OfflineRecognizer.kt \
|
||||
OfflineStream.kt \
|
||||
WaveReader.kt \
|
||||
@@ -300,6 +303,7 @@ function testInverseTextNormalizationOnlineAsr() {
|
||||
kotlinc-jvm -include-runtime -d $out_filename \
|
||||
test_itn_online_asr.kt \
|
||||
FeatureConfig.kt \
|
||||
HomophoneReplacerConfig.kt \
|
||||
OnlineRecognizer.kt \
|
||||
OnlineStream.kt \
|
||||
WaveReader.kt \
|
||||
@@ -402,6 +406,38 @@ function testOfflineSpeechDenoiser() {
|
||||
ls -lh *.wav
|
||||
}
|
||||
|
||||
function testOfflineSenseVoiceWithHr() {
|
||||
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
|
||||
|
||||
out_filename=test_offline_sense_voice_with_hr.jar
|
||||
kotlinc-jvm -include-runtime -d $out_filename \
|
||||
test_offline_sense_voice_with_hr.kt \
|
||||
FeatureConfig.kt \
|
||||
HomophoneReplacerConfig.kt \
|
||||
OfflineRecognizer.kt \
|
||||
OfflineStream.kt \
|
||||
WaveReader.kt \
|
||||
faked-asset-manager.kt
|
||||
|
||||
ls -lh $out_filename
|
||||
java -Djava.library.path=../build/lib -jar $out_filename
|
||||
}
|
||||
|
||||
testOfflineSenseVoiceWithHr
|
||||
testOfflineSpeechDenoiser
|
||||
testOfflineSpeakerDiarization
|
||||
testSpeakerEmbeddingExtractor
|
||||
|
||||
35
kotlin-api-examples/test_offline_sense_voice_with_hr.kt
Normal file
35
kotlin-api-examples/test_offline_sense_voice_with_hr.kt
Normal file
@@ -0,0 +1,35 @@
|
||||
package com.k2fsa.sherpa.onnx
|
||||
|
||||
fun main() {
|
||||
val recognizer = createOfflineRecognizer()
|
||||
val waveFilename = "./test-hr.wav"
|
||||
|
||||
val objArray = WaveReader.readWaveFromFile(
|
||||
filename = waveFilename,
|
||||
)
|
||||
val samples: FloatArray = objArray[0] as FloatArray
|
||||
val sampleRate: Int = objArray[1] as Int
|
||||
|
||||
val stream = recognizer.createStream()
|
||||
stream.acceptWaveform(samples, sampleRate=sampleRate)
|
||||
recognizer.decode(stream)
|
||||
|
||||
val result = recognizer.getResult(stream)
|
||||
println(result)
|
||||
|
||||
stream.release()
|
||||
recognizer.release()
|
||||
}
|
||||
|
||||
fun createOfflineRecognizer(): OfflineRecognizer {
|
||||
val config = OfflineRecognizerConfig(
|
||||
featConfig = getFeatureConfig(sampleRate = 16000, featureDim = 80),
|
||||
modelConfig = getOfflineModelConfig(type = 15)!!,
|
||||
hr = HomophoneReplacerConfig(
|
||||
dictDir = "./dict",
|
||||
lexicon = "./lexicon.txt",
|
||||
ruleFsts = "./replace.fst"),
|
||||
)
|
||||
|
||||
return OfflineRecognizer(config = config)
|
||||
}
|
||||
Reference in New Issue
Block a user