Skip to content

Add missing fields of OnlineRecongizerResult to Go API. - #3395

Merged
csukuangfj merged 1 commit into
k2-fsa:masterfrom
csukuangfj:fix-go
Mar 24, 2026
Merged

csukuangfj merged 1 commit into
k2-fsa:masterfrom
csukuangfj:fix-go

Conversation

@csukuangfj

@csukuangfj csukuangfj commented Mar 24, 2026 •

Copy link
Copy Markdown
Collaborator

Fixes #3393

cc @zuiaibaicaiya

Summary by CodeRabbit

  • New Features
    • Online recognizer now returns additional result details including recognized tokens, timing information, and JSON-formatted output alongside the recognized text.

@dosubot dosubot Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Mar 24, 2026
@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request enhances the Go API for sherpa-onnx by exposing additional details from the speech recognition results. Previously, the OnlineRecognizerResult struct only contained the recognized text. With this change, it now includes tokens, timestamps, and a JSON representation of the result, providing more comprehensive information to Go applications using the library.

Highlights

  • Go API Enhancement: Added new fields Tokens (slice of strings), Timestamps (slice of float32), and Json (string) to the OnlineRecognizerResult struct in the Go API.
  • C API Integration: Implemented the necessary logic within the GetResult method to retrieve and populate these newly added fields from the underlying C API, ensuring the Go struct accurately reflects the full recognition result.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

@coderabbitai

coderabbitai Bot commented Mar 24, 2026 •

Copy link
Copy Markdown

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Extended the OnlineRecognizerResult struct in Go to include Tokens, Timestamps, and Json fields. The GetResult method now populates these fields by converting C arrays to Go slices with appropriate nil checks.

Changes

Cohort / File(s) Summary
Online Recognizer Result Enhancement
scripts/go/sherpa_onnx.go
Extended OnlineRecognizerResult struct with three new fields: Tokens []string, Timestamps []float32, and Json string. Updated OnlineRecognizer.GetResult to populate these fields from corresponding C data structures with null/count safety checks.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

🐰 The timestamps were lost in the Go,
A mystery of transcription flow,
But now the tokens dance with their time,
Each character marks its rhyme,
Real-time wisdom, perfectly sublime! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The PR title accurately describes the main change: adding missing fields (Tokens, Timestamps, Json) to the OnlineRecognizerResult struct in the Go API.
Linked Issues check ✅ Passed The changes directly address issue #3393 by adding Tokens and Timestamps fields to OnlineRecognizerResult and populating them in GetResult, restoring timestamp support for Go real-time transcription to match other language bindings.
Out of Scope Changes check ✅ Passed All changes are focused on extending OnlineRecognizerResult with the three missing fields (Tokens, Timestamps, Json) and updating GetResult to populate them, which is directly scoped to the linked issue requirement.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with 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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@csukuangfj
csukuangfj merged commit 4a1ba8d into k2-fsa:master Mar 24, 2026
1 check was pending
@csukuangfj
csukuangfj deleted the fix-go branch March 24, 2026 02:41

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds the Tokens, Timestamps, and Json fields to the OnlineRecognizerResult struct in the Go API, aligning it with the underlying C++ implementation. The changes correctly populate these new fields from the C result. My review includes two suggestions for improvement: one to make the Json field ignored during JSON marshalling to avoid redundancy, and another to use the idiomatic copy function for better performance and readability when handling timestamps.

Comment thread scripts/go/sherpa_onnx.go
Text string
Tokens []string
Timestamps []float32
Json string

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The Json field appears to hold a pre-formatted JSON string from the C library which represents the other fields in the struct. When this OnlineRecognizerResult Go struct is marshalled to JSON, this can lead to a confusing and redundant output, with a JSON string embedded within another JSON object. To avoid this, consider adding a json struct tag to exclude this field from marshalling.

Suggested change
Json string
Json string `json:"-"`

Comment thread scripts/go/sherpa_onnx.go
Comment on lines +424 to +426
for i := 0; i < n; i++ {
result.Timestamps[i] = float32(timestamps[i])
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This loop for copying timestamps can be replaced by the built-in copy function. It's more idiomatic Go and can be more efficient for copying slice data. The explicit cast to float32 is also redundant as C.float is an alias for float32 in cgo.

		copy(result.Timestamps, timestamps)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

golang实时转写时间戳缺失

1 participant