sherpa-onnx-offline-punctuation.cc
2.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// sherpa-onnx/csrc/sherpa-onnx-offline-punctuation.cc
//
// Copyright (c) 2022-2024 Xiaomi Corporation
#include <stdio.h>
#include <chrono> // NOLINT
#include "sherpa-onnx/csrc/offline-punctuation.h"
#include "sherpa-onnx/csrc/parse-options.h"
int main(int32_t argc, char *argv[]) {
const char *kUsageMessage = R"usage(
Add punctuations to the input text.
The input text can contain both Chinese and English words.
Usage:
wget 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
./bin/sherpa-onnx-offline-punctuation \
--ct-transformer=./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx
"你好吗how are you Fantasitic 谢谢我很好你怎么样呢"
The output text should look like below:
)usage";
sherpa_onnx::ParseOptions po(kUsageMessage);
sherpa_onnx::OfflinePunctuationConfig config;
config.Register(&po);
po.Read(argc, argv);
if (po.NumArgs() != 1) {
fprintf(stderr,
"Error: Please provide only 1 position argument containing the "
"input text.\n\n");
po.PrintUsage();
exit(EXIT_FAILURE);
}
fprintf(stderr, "%s\n", config.ToString().c_str());
if (!config.Validate()) {
fprintf(stderr, "Errors in config!\n");
return -1;
}
fprintf(stderr, "Creating OfflinePunctuation ...\n");
sherpa_onnx::OfflinePunctuation punct(config);
fprintf(stderr, "Started\n");
const auto begin = std::chrono::steady_clock::now();
std::string text = po.GetArg(1);
std::string text_with_punct = punct.AddPunctuation(text);
fprintf(stderr, "Done\n");
const auto end = std::chrono::steady_clock::now();
float elapsed_seconds =
std::chrono::duration_cast<std::chrono::milliseconds>(end - begin)
.count() /
1000.;
fprintf(stderr, "Num threads: %d\n", config.model.num_threads);
fprintf(stderr, "Elapsed seconds: %.3f s\n", elapsed_seconds);
fprintf(stderr, "Input text: %s\n", text.c_str());
fprintf(stderr, "Output text: %s\n", text_with_punct.c_str());
}