Fangjun Kuang
Committed by GitHub

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

@@ -7,6 +7,10 @@ echo "pwd: $PWD" @@ -7,6 +7,10 @@ echo "pwd: $PWD"
7 cd swift-api-examples 7 cd swift-api-examples
8 ls -lh 8 ls -lh
9 9
  10 +./run-add-punctuations.sh
  11 +rm ./add-punctuations
  12 +rm -rf sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12
  13 +
10 ./run-keyword-spotting-from-file.sh 14 ./run-keyword-spotting-from-file.sh
11 rm ./keyword-spotting-from-file 15 rm ./keyword-spotting-from-file
12 rm -rf sherpa-onnx-kws-* 16 rm -rf sherpa-onnx-kws-*
@@ -9,3 +9,4 @@ sherpa-onnx-paraformer-zh-2023-09-14 @@ -9,3 +9,4 @@ sherpa-onnx-paraformer-zh-2023-09-14
9 *.bak 9 *.bak
10 streaming-hlg-decode-file 10 streaming-hlg-decode-file
11 keyword-spotting-from-file 11 keyword-spotting-from-file
  12 +add-punctuations
@@ -957,3 +957,52 @@ class SherpaOnnxKeywordSpotterWrapper { @@ -957,3 +957,52 @@ class SherpaOnnxKeywordSpotterWrapper {
957 InputFinished(stream) 957 InputFinished(stream)
958 } 958 }
959 } 959 }
  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 +}
  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 +}
  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