Repository navigation
feat: add punctuation C++ API #2510
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
csukuangfj
merged 6 commits into
k2-fsa:master
from
Aruxxxi:feature/add-punctuation-cpp-api
Aug 19, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c052ba7
feat: add punctuation C++ API
4134ab2
fix: use parameter p instead of p_ in Destroy method
0de3192
how to use punctuation cpp api
44255d9
remove unused headers
abb746f
add to CMakeLists.txt and update punctuation example
9281e48
correct CMakeLists typo
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| // cxx-api-examples/punctuation-cxx-api.cc | ||
| // Copyright (c) 2025 Xiaomi Corporation | ||
|
|
||
| // To use punctuation model: | ||
| // 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 | ||
|
|
||
| #include <iostream> | ||
| #include <string> | ||
|
|
||
| #include "sherpa-onnx/c-api/cxx-api.h" | ||
|
|
||
| int32_t main() { | ||
| using namespace sherpa_onnx::cxx; // NOLINT | ||
|
|
||
| OfflinePunctuationConfig punctuation_config; | ||
| punctuation_config.model.ct_transformer = "./sherpa-onnx-punct-ct-transformer-zh-en-vocab272727-2024-04-12/model.onnx"; | ||
| punctuation_config.model.num_threads = 1; | ||
| punctuation_config.model.debug = false; | ||
| punctuation_config.model.provider = "cpu"; | ||
|
|
||
| OfflinePunctuation punct = OfflinePunctuation::Create(punctuation_config); | ||
| if (!punct.Get()) { | ||
| std::cerr << "Failed to create punctuation model. Please check your config\n"; | ||
| return -1; | ||
| } | ||
|
|
||
| std::string text = "你好吗how are you Fantasitic 谢谢我很好你怎么样呢"; | ||
| std::string text_with_punct = punct.AddPunctuation(text); | ||
| std::cout << "Original text: " << text << std::endl; | ||
| std::cout << "With punctuation: " << text_with_punct << std::endl; | ||
|
|
||
| return 0; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -821,4 +821,33 @@ bool FileExists(const std::string &filename) { | |||||
| return SherpaOnnxFileExists(filename.c_str()); | ||||||
| } | ||||||
|
|
||||||
| // ============================================================ | ||||||
| // For Offline Punctuation | ||||||
| // ============================================================ | ||||||
| OfflinePunctuation OfflinePunctuation::Create(const OfflinePunctuationConfig &config) { | ||||||
| struct SherpaOnnxOfflinePunctuationConfig c; | ||||||
| memset(&c, 0, sizeof(c)); | ||||||
| c.model.ct_transformer = config.model.ct_transformer.c_str(); | ||||||
| c.model.num_threads = config.model.num_threads; | ||||||
| c.model.debug = config.model.debug; | ||||||
| c.model.provider = config.model.provider.c_str(); | ||||||
|
|
||||||
| const SherpaOnnxOfflinePunctuation *punct = SherpaOnnxCreateOfflinePunctuation(&c); | ||||||
| return OfflinePunctuation(punct); | ||||||
| } | ||||||
|
|
||||||
| OfflinePunctuation::OfflinePunctuation(const SherpaOnnxOfflinePunctuation *p) | ||||||
| : MoveOnly<OfflinePunctuation, SherpaOnnxOfflinePunctuation>(p) {} | ||||||
|
|
||||||
| void OfflinePunctuation::Destroy(const SherpaOnnxOfflinePunctuation *p) const { | ||||||
|
||||||
| void OfflinePunctuation::Destroy(const SherpaOnnxOfflinePunctuation *p) const { | |
| void OfflinePunctuation::Destroy() const { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use standard-conforming
int main();int32_t main()may fail without .C++ requires
mainto returnint. Usingint32_tcan be non-portable and requires<cstdint>to be included. Simplest fix: useint.📝 Committable suggestion
🤖 Prompt for AI Agents