Add Swift API for adding punctuations to text. (#1132)

This commit is contained in:
Fangjun Kuang
2024-07-15 15:30:40 +08:00
committed by GitHub
parent 11cfd33b10
commit b2c283fa2b
5 changed files with 119 additions and 0 deletions

View File

@@ -957,3 +957,52 @@ class SherpaOnnxKeywordSpotterWrapper {
InputFinished(stream)
}
}
// Punctuation
func sherpaOnnxOfflinePunctuationModelConfig(
ctTransformer: String,
numThreads: Int = 1,
debug: Int = 0,
provider: String = "cpu"
) -> SherpaOnnxOfflinePunctuationModelConfig {
return SherpaOnnxOfflinePunctuationModelConfig(
ct_transformer: toCPointer(ctTransformer),
num_threads: Int32(numThreads),
debug: Int32(debug),
provider: toCPointer(provider)
)
}
func sherpaOnnxOfflinePunctuationConfig(
model: SherpaOnnxOfflinePunctuationModelConfig
) -> SherpaOnnxOfflinePunctuationConfig {
return SherpaOnnxOfflinePunctuationConfig(
model: model
)
}
class SherpaOnnxOfflinePunctuationWrapper {
/// A pointer to the underlying counterpart in C
let ptr: OpaquePointer!
/// Constructor taking a model config
init(
config: UnsafePointer<SherpaOnnxOfflinePunctuationConfig>!
) {
ptr = SherpaOnnxCreateOfflinePunctuation(config)
}
deinit {
if let ptr {
SherpaOnnxDestroyOfflinePunctuation(ptr)
}
}
func addPunct(text: String) -> String {
let cText = SherpaOfflinePunctuationAddPunct(ptr, toCPointer(text))
let ans = String(cString: cText!)
SherpaOfflinePunctuationFreeText(cText)
return ans
}
}