Skip to content

Commit

Permalink
Add Swift API for adding punctuations to text. (#1132)
Browse files Browse the repository at this point in the history
  • Loading branch information
csukuangfj authored Jul 15, 2024
1 parent 11cfd33 commit b2c283f
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/scripts/test-swift.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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-*
Expand Down
1 change: 1 addition & 0 deletions swift-api-examples/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ sherpa-onnx-paraformer-zh-2023-09-14
*.bak
streaming-hlg-decode-file
keyword-spotting-from-file
add-punctuations
49 changes: 49 additions & 0 deletions swift-api-examples/SherpaOnnx.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 31 additions & 0 deletions swift-api-examples/add-punctuations.swift
Original file line number Diff line number Diff line change
@@ -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 changes: 34 additions & 0 deletions swift-api-examples/run-add-punctuations.sh
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit b2c283f

Please sign in to comment.