Fangjun Kuang
Committed by GitHub

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

... ... @@ -7,6 +7,10 @@ echo "pwd: $PWD"
cd swift-api-examples
ls -lh
./run-add-punctuations.sh
rm ./add-punctuations
rm -rf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12
./run-keyword-spotting-from-file.sh
rm ./keyword-spotting-from-file
rm -rf sherpa-onnx-kws-*
... ...
... ... @@ -9,3 +9,4 @@ sherpa-onnx-paraformer-zh-2023-09-14
*.bak
streaming-hlg-decode-file
keyword-spotting-from-file
add-punctuations
... ...
... ... @@ -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
}
}
... ...
func run() {
let model = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx"
let modelConfig = sherpaOnnxOfflinePunctuationModelConfig(
ctTransformer: model,
numThreads: 1,
debug: 1,
provider: "cpu"
)
var config = sherpaOnnxOfflinePunctuationConfig(model: modelConfig)
let punct = SherpaOnnxOfflinePunctuationWrapper(config: &config)
let textList = [
"这是一个测试你好吗How are you我很好thank you are you ok谢谢你",
"我们都是木头人不会说话不会动",
"The African blogosphere is rapidly expanding bringing more voices online in the form of commentaries opinions analyses rants and poetry",
]
for i in 0..<textList.count {
let t = punct.addPunct(text: textList[i])
print("\nresult is:\n\(t)")
}
}
@main
struct App {
static func main() {
run()
}
}
... ...
#!/usr/bin/env bash
set -ex
if [ ! -d ../build-swift-macos ]; then
echo "Please run ../build-swift-macos.sh first!"
exit 1
fi
if [ ! -d ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12 ]; then
curl -SL -O https://github.com/k2-fsa/sherpa-onnx/releases/download/punctuation-models/sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
fi
if [ ! -e ./add-punctuations ]; then
# Note: We use -lc++ to link against libc++ instead of libstdc++
swiftc \
-lc++ \
-I ../build-swift-macos/install/include \
-import-objc-header ./SherpaOnnx-Bridging-Header.h \
./add-punctuations.swift ./SherpaOnnx.swift \
-L ../build-swift-macos/install/lib/ \
-l sherpa-onnx \
-l onnxruntime \
-o ./add-punctuations
strip ./add-punctuations
else
echo "./add-punctuations exists - skip building"
fi
export DYLD_LIBRARY_PATH=$PWD/../build-swift-macos/install/lib:$DYLD_LIBRARY_PATH
./add-punctuations
... ...