mirror of
https://github.com/k2-fsa/sherpa-onnx.git
synced 2026-11-10 21:11:03 +08:00
Add OfflineDiacritization to kotlin-api (#3949)
This commit is contained in:
1 parent
46e1ab7c99
commit
f007b559b7
4 files changed
+119
No files matched your search
@@ -0,0 +1 @@
|
||||
../sherpa-onnx/kotlin-api/OfflineDiacritization.kt
|
||||
@@ -430,6 +430,26 @@ function testOfflinePunctuation() {
|
||||
java -Djava.library.path=../build/lib -jar $out_filename
|
||||
}
|
||||
|
||||
function testOfflineDiacritization() {
|
||||
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
|
||||
rm -rf catt_eo_model_onnx
|
||||
unzip -o eo_model_onnx.zip -d catt_eo_model_onnx
|
||||
rm eo_model_onnx.zip
|
||||
fi
|
||||
|
||||
out_filename=test_offline_diacritization.jar
|
||||
kotlinc-jvm -include-runtime -d $out_filename \
|
||||
./test_offline_diacritization.kt \
|
||||
./OfflineDiacritization.kt \
|
||||
faked-asset-manager.kt \
|
||||
faked-log.kt
|
||||
|
||||
ls -lh $out_filename
|
||||
|
||||
java -Djava.library.path=../build/lib -jar $out_filename
|
||||
}
|
||||
|
||||
function testOnlinePunctuation() {
|
||||
if [ ! -f ./sherpa-onnx-online-punct-en-2024-08-06/model.int8.onnx ]; then
|
||||
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-online-punct-en-2024-08-06.tar.bz2
|
||||
@@ -807,6 +827,7 @@ testAudioTagging
|
||||
testSpokenLanguageIdentification
|
||||
testOfflineAsr
|
||||
testOfflinePunctuation
|
||||
testOfflineDiacritization
|
||||
testOnlinePunctuation
|
||||
testInverseTextNormalizationOfflineAsr
|
||||
testInverseTextNormalizationOnlineAsr
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.k2fsa.sherpa.onnx
|
||||
|
||||
fun main() {
|
||||
testDiacritization()
|
||||
}
|
||||
|
||||
fun testDiacritization() {
|
||||
// please download the model from
|
||||
// https://github.com/abjadai/catt/releases/download/v2/eo_model_onnx.zip
|
||||
val config = OfflineDiacritizationConfig(
|
||||
model = OfflineDiacritizationModelConfig(
|
||||
cattEncoder = "./catt_eo_model_onnx/encoder.onnx",
|
||||
cattDecoder = "./catt_eo_model_onnx/decoder.onnx",
|
||||
numThreads = 1,
|
||||
debug = true,
|
||||
provider = "cpu",
|
||||
)
|
||||
)
|
||||
val diacrt = OfflineDiacritization(config = config)
|
||||
val sentences = arrayOf(
|
||||
"وقالت مجلة نيوزويك الأمريكية التحديث الجديد ل إنستجرام يمكن أن يساهم في إيقاف وكشف الحسابات المزورة بسهولة شديدة",
|
||||
"اللغة العربية من أقدم اللغات السامية",
|
||||
)
|
||||
println("---")
|
||||
for (text in sentences) {
|
||||
val out = diacrt.addDiacritics(text)
|
||||
println("Input: $text")
|
||||
println("Output: $out")
|
||||
println("---")
|
||||
}
|
||||
|
||||
diacrt.release()
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.k2fsa.sherpa.onnx
|
||||
|
||||
import android.content.res.AssetManager
|
||||
|
||||
data class OfflineDiacritizationModelConfig(
|
||||
var cattEncoder: String = "",
|
||||
var cattDecoder: String = "",
|
||||
var numThreads: Int = 1,
|
||||
var debug: Boolean = false,
|
||||
var provider: String = "cpu",
|
||||
)
|
||||
|
||||
|
||||
data class OfflineDiacritizationConfig(
|
||||
var model: OfflineDiacritizationModelConfig,
|
||||
)
|
||||
|
||||
class OfflineDiacritization(
|
||||
assetManager: AssetManager? = null,
|
||||
config: OfflineDiacritizationConfig,
|
||||
) {
|
||||
private var ptr: Long
|
||||
|
||||
init {
|
||||
ptr = if (assetManager != null) {
|
||||
newFromAsset(assetManager, config)
|
||||
} else {
|
||||
newFromFile(config)
|
||||
}
|
||||
require(ptr != 0L) {
|
||||
"Invalid OfflineDiacritizationConfig: failed to create native OfflineDiacritization"
|
||||
}
|
||||
}
|
||||
|
||||
protected fun finalize() {
|
||||
if (ptr != 0L) {
|
||||
delete(ptr)
|
||||
ptr = 0
|
||||
}
|
||||
}
|
||||
|
||||
fun release() = finalize()
|
||||
|
||||
fun addDiacritics(text: String) = addDiacritics(ptr, text)
|
||||
|
||||
private external fun delete(ptr: Long)
|
||||
|
||||
private external fun addDiacritics(ptr: Long, text: String): String
|
||||
|
||||
private external fun newFromAsset(
|
||||
assetManager: AssetManager,
|
||||
config: OfflineDiacritizationConfig,
|
||||
): Long
|
||||
|
||||
private external fun newFromFile(
|
||||
config: OfflineDiacritizationConfig,
|
||||
): Long
|
||||
|
||||
companion object {
|
||||
init {
|
||||
System.loadLibrary("sherpa-onnx-jni")
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user