-
Couldn't load subscription status.
- Fork 948
feat: add punctuation C++ API #2509
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
Conversation
WalkthroughAdds a C++ wrapper (sherpa_onnx::cxx) for offline punctuation, introducing config structs and an OfflinePunctuation class with a factory Create, AddPunctuation operation, and Destroy, bridging to existing C APIs and handling allocation/freeing of C strings. Changes
Sequence Diagram(s)sequenceDiagram
participant App as Application
participant CXX as cxx::OfflinePunctuation
participant CAPI as C API (sherpa-onnx)
rect rgb(240,245,255)
note over App,CXX: Construction
App->>CXX: OfflinePunctuation::Create(config)
CXX->>CAPI: SherpaOnnxCreateOfflinePunctuation(c_config)
CAPI-->>CXX: SherpaOnnxOfflinePunctuation*
CXX-->>App: OfflinePunctuation (wrapper)
end
rect rgb(240,255,240)
note over App,CXX: Inference
App->>CXX: AddPunctuation(text)
CXX->>CAPI: SherpaOfflinePunctuationAddPunct(ptr, text)
CAPI-->>CXX: char* punctuated_text
CXX-->>CAPI: SherpaOfflinePunctuationFreeText(punctuated_text)
CXX-->>App: std::string result
end
rect rgb(255,245,240)
note over App,CXX: Teardown
App->>CXX: Destroy(ptr)
CXX->>CAPI: SherpaOnnxDestroyOfflinePunctuation(ptr)
end
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Possibly related PRs
Poem
Tip 🔌 Remote MCP (Model Context Protocol) integration is now available!Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats. ✨ Finishing Touches
🧪 Generate unit tests
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
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.
Actionable comments posted: 0
🧹 Nitpick comments (3)
sherpa-onnx/c-api/cxx-api.h (1)
680-689: Export macro consistency: annotate config structs with SHERPA_ONNX_APIMost Offline config structs in this header are exported (e.g., OfflineTransducerModelConfig). For consistency and to avoid any potential Windows DLL export surprises, annotate the new punctuation config structs with SHERPA_ONNX_API.
Apply:
-struct OfflinePunctuationModelConfig { +struct SHERPA_ONNX_API OfflinePunctuationModelConfig { std::string ct_transformer; int32_t num_threads = 1; bool debug = false; std::string provider = "cpu"; }; -struct OfflinePunctuationConfig { +struct SHERPA_ONNX_API OfflinePunctuationConfig { OfflinePunctuationModelConfig model; };sherpa-onnx/c-api/cxx-api.cc (2)
842-844: Use the parameter in Destroy() for consistencyOther wrappers pass the provided pointer to the C destroy function. Here, p_ is used instead of the parameter. It works (MoveOnly passes p_), but using the parameter improves clarity and consistency.
-void OfflinePunctuation::Destroy(const SherpaOnnxOfflinePunctuation *p) const { - SherpaOnnxDestroyOfflinePunctuation(p_); -} +void OfflinePunctuation::Destroy(const SherpaOnnxOfflinePunctuation *p) const { + SherpaOnnxDestroyOfflinePunctuation(p); +}
846-851: Defensive check: handle potential null from C API before constructing std::stringWhile the current C implementation always allocates and returns a non-null char*, guarding against null avoids UB from constructing std::string(nullptr) and aligns with patterns elsewhere that tolerate null returns.
std::string OfflinePunctuation::AddPunctuation(const std::string &text) const { - const char *result = SherpaOfflinePunctuationAddPunct(p_, text.c_str()); - std::string ans(result); - SherpaOfflinePunctuationFreeText(result); - return ans; + const char *result = SherpaOfflinePunctuationAddPunct(p_, text.c_str()); + if (!result) { + return std::string{}; + } + std::string ans(result); + SherpaOfflinePunctuationFreeText(result); + return ans; }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
💡 Knowledge Base configuration:
- MCP integration is disabled by default for public repositories
- Jira integration is disabled by default for public repositories
- Linear integration is disabled by default for public repositories
You can enable these sources in your CodeRabbit configuration.
📒 Files selected for processing (2)
sherpa-onnx/c-api/cxx-api.cc(1 hunks)sherpa-onnx/c-api/cxx-api.h(1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
sherpa-onnx/c-api/cxx-api.h (2)
sherpa-onnx/c-api/cxx-api.cc (19)
OfflinePunctuation(839-840)Create(49-111)Create(49-50)Create(287-293)Create(287-288)Create(393-441)Create(393-393)Create(491-533)Create(491-491)Create(606-620)Create(606-607)Create(647-650)Create(647-647)Create(686-712)Create(686-687)Create(776-783)Create(776-779)AddPunctuation(846-851)AddPunctuation(846-846)sherpa-onnx/csrc/offline-punctuation.cc (1)
sherpa_onnx(15-53)
sherpa-onnx/c-api/cxx-api.cc (2)
sherpa-onnx/c-api/c-api.cc (8)
SherpaOnnxCreateOfflinePunctuation(1898-1922)SherpaOnnxCreateOfflinePunctuation(1898-1899)SherpaOnnxDestroyOfflinePunctuation(1924-1927)SherpaOnnxDestroyOfflinePunctuation(1924-1925)SherpaOfflinePunctuationAddPunct(1929-1938)SherpaOfflinePunctuationAddPunct(1929-1930)SherpaOfflinePunctuationFreeText(1940-1940)SherpaOfflinePunctuationFreeText(1940-1940)scripts/go/sherpa_onnx.go (1)
OfflinePunctuation(1761-1763)
🔇 Additional comments (2)
sherpa-onnx/c-api/cxx-api.h (1)
691-701: API shape looks good and aligns with existing wrappersThe MoveOnly-based OfflinePunctuation wrapper (Create/Destroy/AddPunctuation) matches established patterns in this header. No issues from an API design standpoint.
sherpa-onnx/c-api/cxx-api.cc (1)
828-837: Create(): good zero-init and faithful config mappingZero-initializing SherpaOnnxOfflinePunctuationConfig and forwarding fields (ct_transformer/num_threads/debug/provider) matches the C API’s expectations. LGTM.
描述
添加了punctuation的C++ API包装类,提供C++接口。
修改内容
cxx-api.h中添加了OfflinePunctuation类声明cxx-api.cc中实现了OfflinePunctuation类的方法Create、Destroy和AddPunctuation方法修改类型
测试
检查清单
Summary by CodeRabbit