OnlinePunctuation.kt
1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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")
}
}
}