Add Kotlin and Java API for online punctuation models (#1936)

This commit is contained in:
Fangjun Kuang
2025-02-27 16:52:36 +08:00
committed by GitHub
parent 815ebac8f9
commit f5dfcf8d2f
16 changed files with 474 additions and 13 deletions

View File

@@ -0,0 +1,61 @@
package com.k2fsa.sherpa.onnx
import android.content.res.AssetManager
data class OnlinePunctuationModelConfig(
var cnnBilstm: String = "",
var bpeVocab: String = "",
var numThreads: Int = 1,
var debug: Boolean = false,
var provider: String = "cpu",
)
data class OnlinePunctuationConfig(
var model: OnlinePunctuationModelConfig,
)
class OnlinePunctuation(
assetManager: AssetManager? = null,
config: OnlinePunctuationConfig,
) {
private var ptr: Long
init {
ptr = if (assetManager != null) {
newFromAsset(assetManager, config)
} else {
newFromFile(config)
}
}
protected fun finalize() {
if (ptr != 0L) {
delete(ptr)
ptr = 0
}
}
fun release() = finalize()
fun addPunctuation(text: String) = addPunctuation(ptr, text)
private external fun delete(ptr: Long)
private external fun addPunctuation(ptr: Long, text: String): String
private external fun newFromAsset(
assetManager: AssetManager,
config: OnlinePunctuationConfig,
): Long
private external fun newFromFile(
config: OnlinePunctuationConfig,
): Long
companion object {
init {
System.loadLibrary("sherpa-onnx-jni")
}
}
}