Add Swift API for adding punctuations to text. (#1132)
This commit is contained in:
4
.github/scripts/test-swift.sh
vendored
4
.github/scripts/test-swift.sh
vendored
@@ -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-*
|
||||
|
||||
1
swift-api-examples/.gitignore
vendored
1
swift-api-examples/.gitignore
vendored
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
31
swift-api-examples/add-punctuations.swift
Normal file
31
swift-api-examples/add-punctuations.swift
Normal file
@@ -0,0 +1,31 @@
|
||||
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()
|
||||
}
|
||||
}
|
||||
34
swift-api-examples/run-add-punctuations.sh
Executable file
34
swift-api-examples/run-add-punctuations.sh
Executable file
@@ -0,0 +1,34 @@
|
||||
#!/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
|
||||
Reference in New Issue
Block a user