Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion scripts/go/sherpa_onnx.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,10 @@ type OnlineRecognizerConfig struct {

// It contains the recognition result for a online stream.
type OnlineRecognizerResult struct {
Text string
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:"-"`

}

// The online recognizer class. It wraps a pointer from C.
Expand Down Expand Up @@ -404,8 +407,24 @@ func (recognizer *OnlineRecognizer) DecodeStreams(s []*OnlineStream) {
func (recognizer *OnlineRecognizer) GetResult(s *OnlineStream) *OnlineRecognizerResult {
p := C.SherpaOnnxGetOnlineStreamResult(recognizer.impl, s.impl)
defer C.SherpaOnnxDestroyOnlineRecognizerResult(p)
n := int(p.count)
result := &OnlineRecognizerResult{}
result.Text = C.GoString(p.text)
result.Json = C.GoString(p.json)
if n > 0 {
result.Tokens = make([]string, n)
tokens := unsafe.Slice(p.tokens_arr, n)
for i := 0; i < n; i++ {
result.Tokens[i] = C.GoString(tokens[i])
}
}
if p.timestamps != nil && n > 0 {
result.Timestamps = make([]float32, n)
timestamps := unsafe.Slice(p.timestamps, n)
for i := 0; i < n; i++ {
result.Timestamps[i] = float32(timestamps[i])
}
Comment on lines +424 to +426

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)

}

return result
}
Expand Down