Committed by
GitHub
add Tokens []string, Timestamps []float32, Lang string, Emotion string, Event string (#1277)
正在显示
2 个修改的文件
包含
37 行增加
和
6 行删除
| @@ -3,12 +3,13 @@ package main | @@ -3,12 +3,13 @@ package main | ||
| 3 | import ( | 3 | import ( |
| 4 | "bytes" | 4 | "bytes" |
| 5 | "encoding/binary" | 5 | "encoding/binary" |
| 6 | - sherpa "github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx" | ||
| 7 | - flag "github.com/spf13/pflag" | ||
| 8 | - "github.com/youpy/go-wav" | ||
| 9 | "log" | 6 | "log" |
| 10 | "os" | 7 | "os" |
| 11 | "strings" | 8 | "strings" |
| 9 | + | ||
| 10 | + sherpa "github.com/k2-fsa/sherpa-onnx-go/sherpa_onnx" | ||
| 11 | + flag "github.com/spf13/pflag" | ||
| 12 | + "github.com/youpy/go-wav" | ||
| 12 | ) | 13 | ) |
| 13 | 14 | ||
| 14 | func main() { | 15 | func main() { |
| @@ -80,7 +81,16 @@ func main() { | @@ -80,7 +81,16 @@ func main() { | ||
| 80 | log.Println("Decoding done!") | 81 | log.Println("Decoding done!") |
| 81 | result := stream.GetResult() | 82 | result := stream.GetResult() |
| 82 | 83 | ||
| 83 | - log.Println(strings.ToLower(result.Text)) | 84 | + log.Println("Text: " + strings.ToLower(result.Text)) |
| 85 | + log.Println("Emotion: " + result.Emotion) | ||
| 86 | + log.Println("Lang: " + result.Lang) | ||
| 87 | + log.Println("Event: " + result.Event) | ||
| 88 | + for _, v := range result.Timestamps { | ||
| 89 | + log.Printf("Timestamp: %+v\n", v) | ||
| 90 | + } | ||
| 91 | + for _, v := range result.Tokens { | ||
| 92 | + log.Println("Token: " + v) | ||
| 93 | + } | ||
| 84 | log.Printf("Wave duration: %v seconds", float32(len(samples))/float32(sampleRate)) | 94 | log.Printf("Wave duration: %v seconds", float32(len(samples))/float32(sampleRate)) |
| 85 | } | 95 | } |
| 86 | 96 |
| @@ -440,7 +440,12 @@ type OfflineStream struct { | @@ -440,7 +440,12 @@ type OfflineStream struct { | ||
| 440 | 440 | ||
| 441 | // It contains recognition result of an offline stream. | 441 | // It contains recognition result of an offline stream. |
| 442 | type OfflineRecognizerResult struct { | 442 | type OfflineRecognizerResult struct { |
| 443 | - Text string | 443 | + Text string |
| 444 | + Tokens []string | ||
| 445 | + Timestamps []float32 | ||
| 446 | + Lang string | ||
| 447 | + Emotion string | ||
| 448 | + Event string | ||
| 444 | } | 449 | } |
| 445 | 450 | ||
| 446 | // Frees the internal pointer of the recognition to avoid memory leak. | 451 | // Frees the internal pointer of the recognition to avoid memory leak. |
| @@ -593,7 +598,23 @@ func (s *OfflineStream) GetResult() *OfflineRecognizerResult { | @@ -593,7 +598,23 @@ func (s *OfflineStream) GetResult() *OfflineRecognizerResult { | ||
| 593 | defer C.SherpaOnnxDestroyOfflineRecognizerResult(p) | 598 | defer C.SherpaOnnxDestroyOfflineRecognizerResult(p) |
| 594 | result := &OfflineRecognizerResult{} | 599 | result := &OfflineRecognizerResult{} |
| 595 | result.Text = C.GoString(p.text) | 600 | result.Text = C.GoString(p.text) |
| 596 | - | 601 | + result.Lang = C.GoString(p.lang) |
| 602 | + result.Emotion = C.GoString(p.emotion) | ||
| 603 | + result.Event = C.GoString(p.event) | ||
| 604 | + n := int(p.count) | ||
| 605 | + result.Tokens = make([]string, n) | ||
| 606 | + tokens := (*[1 << 28]*C.char)(unsafe.Pointer(p.tokens_arr))[:n:n] | ||
| 607 | + for i := 0; i < n; i++ { | ||
| 608 | + result.Tokens[i] = C.GoString(tokens[i]) | ||
| 609 | + } | ||
| 610 | + if p.timestamps == nil { | ||
| 611 | + return result | ||
| 612 | + } | ||
| 613 | + result.Timestamps = make([]float32, n) | ||
| 614 | + timestamps := (*[1 << 28]C.float)(unsafe.Pointer(p.timestamps))[:n:n] | ||
| 615 | + for i := 0; i < n; i++ { | ||
| 616 | + result.Timestamps[i] = float32(timestamps[i]) | ||
| 617 | + } | ||
| 597 | return result | 618 | return result |
| 598 | } | 619 | } |
| 599 | 620 |
-
请 注册 或 登录 后发表评论