Skip to content

Commit 9ad6145

Browse files
authored
Add Swift API for adding punctuations to text. (k2-fsa#1132)
1 parent 08dca80 commit 9ad6145

File tree

5 files changed

+119
-0
lines changed

5 files changed

+119
-0
lines changed

.github/scripts/test-swift.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ echo "pwd: $PWD"
77
cd swift-api-examples
88
ls -lh
99

10+
./run-add-punctuations.sh
11+
rm ./add-punctuations
12+
rm -rf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12
13+
1014
./run-keyword-spotting-from-file.sh
1115
rm ./keyword-spotting-from-file
1216
rm -rf sherpa-onnx-kws-*

swift-api-examples/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ sherpa-onnx-paraformer-zh-2023-09-14
99
*.bak
1010
streaming-hlg-decode-file
1111
keyword-spotting-from-file
12+
add-punctuations

swift-api-examples/SherpaOnnx.swift

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,3 +957,52 @@ class SherpaOnnxKeywordSpotterWrapper {
957957
InputFinished(stream)
958958
}
959959
}
960+
961+
// Punctuation
962+
963+
func sherpaOnnxOfflinePunctuationModelConfig(
964+
ctTransformer: String,
965+
numThreads: Int = 1,
966+
debug: Int = 0,
967+
provider: String = "cpu"
968+
) -> SherpaOnnxOfflinePunctuationModelConfig {
969+
return SherpaOnnxOfflinePunctuationModelConfig(
970+
ct_transformer: toCPointer(ctTransformer),
971+
num_threads: Int32(numThreads),
972+
debug: Int32(debug),
973+
provider: toCPointer(provider)
974+
)
975+
}
976+
977+
func sherpaOnnxOfflinePunctuationConfig(
978+
model: SherpaOnnxOfflinePunctuationModelConfig
979+
) -> SherpaOnnxOfflinePunctuationConfig {
980+
return SherpaOnnxOfflinePunctuationConfig(
981+
model: model
982+
)
983+
}
984+
985+
class SherpaOnnxOfflinePunctuationWrapper {
986+
/// A pointer to the underlying counterpart in C
987+
let ptr: OpaquePointer!
988+
989+
/// Constructor taking a model config
990+
init(
991+
config: UnsafePointer<SherpaOnnxOfflinePunctuationConfig>!
992+
) {
993+
ptr = SherpaOnnxCreateOfflinePunctuation(config)
994+
}
995+
996+
deinit {
997+
if let ptr {
998+
SherpaOnnxDestroyOfflinePunctuation(ptr)
999+
}
1000+
}
1001+
1002+
func addPunct(text: String) -> String {
1003+
let cText = SherpaOfflinePunctuationAddPunct(ptr, toCPointer(text))
1004+
let ans = String(cString: cText!)
1005+
SherpaOfflinePunctuationFreeText(cText)
1006+
return ans
1007+
}
1008+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
func run() {
2+
let model = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx"
3+
let modelConfig = sherpaOnnxOfflinePunctuationModelConfig(
4+
ctTransformer: model,
5+
numThreads: 1,
6+
debug: 1,
7+
provider: "cpu"
8+
)
9+
var config = sherpaOnnxOfflinePunctuationConfig(model: modelConfig)
10+
11+
let punct = SherpaOnnxOfflinePunctuationWrapper(config: &config)
12+
13+
let textList = [
14+
"这是一个测试你好吗How are you我很好thank you are you ok谢谢你",
15+
"我们都是木头人不会说话不会动",
16+
"The African blogosphere is rapidly expanding bringing more voices online in the form of commentaries opinions analyses rants and poetry",
17+
]
18+
19+
for i in 0..<textList.count {
20+
let t = punct.addPunct(text: textList[i])
21+
print("\nresult is:\n\(t)")
22+
}
23+
24+
}
25+
26+
@main
27+
struct App {
28+
static func main() {
29+
run()
30+
}
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#!/usr/bin/env bash
2+
3+
set -ex
4+
5+
if [ ! -d ../build-swift-macos ]; then
6+
echo "Please run ../build-swift-macos.sh first!"
7+
exit 1
8+
fi
9+
10+
if [ ! -d ./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12 ]; then
11+
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
12+
tar xvf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
13+
rm sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12.tar.bz2
14+
fi
15+
16+
if [ ! -e ./add-punctuations ]; then
17+
# Note: We use -lc++ to link against libc++ instead of libstdc++
18+
swiftc \
19+
-lc++ \
20+
-I ../build-swift-macos/install/include \
21+
-import-objc-header ./SherpaOnnx-Bridging-Header.h \
22+
./add-punctuations.swift ./SherpaOnnx.swift \
23+
-L ../build-swift-macos/install/lib/ \
24+
-l sherpa-onnx \
25+
-l onnxruntime \
26+
-o ./add-punctuations
27+
28+
strip ./add-punctuations
29+
else
30+
echo "./add-punctuations exists - skip building"
31+
fi
32+
33+
export DYLD_LIBRARY_PATH=$PWD/../build-swift-macos/install/lib:$DYLD_LIBRARY_PATH
34+
./add-punctuations

0 commit comments

Comments
 (0)