-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspeech2text.go
201 lines (171 loc) · 4.62 KB
/
speech2text.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
// Speech to Text Demo with GCP
package main
import (
"context"
"flag"
"fmt"
"io"
"log"
"os"
"path/filepath"
"regexp"
"unicode/utf8"
speech "cloud.google.com/go/speech/apiv1"
speechpb "google.golang.org/genproto/googleapis/cloud/speech/v1"
)
const (
// ANSI Color
colorRed = "\033[0;31m"
colorGreen = "\033[0;32m"
colorBlue = "\033[0;34m"
colorNone = "\033[0m"
)
func main() {
// Usage
flag.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <audiofile> [[word1] [word2]...]\n", filepath.Base(os.Args[0]))
fmt.Fprintf(os.Stderr, "<audiofile>: 16khz 16bit little endian only")
fmt.Fprintf(os.Stderr, "[word1 word2..]: Search word(s)")
}
// Option Parse
flag.Parse()
if len(flag.Args()) == 0 {
log.Fatal("Specify a local audiofile.")
}
args := flag.Args()
audioFile := args[0]
words := args[1:]
// Context
ctx := context.Background()
// Make Speech Client
// TODO: explicitly point to your service account file
// with option.WithCredentialsFile(jsonPath)
client, err := speech.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
// Make Stream
stream, err := client.StreamingRecognize(ctx)
if err != nil {
log.Fatal(err)
}
// Get Transcript
transcript := GetTrans(stream, audioFile)
// Output entire transcript
if len(words) == 0 {
fmt.Printf("%sSearch word is nil, output entire transcript%s\n", colorGreen, colorNone)
fmt.Printf("%v\n", transcript)
return
}
// Default word neighbors
neighbors := 5
// Output result per word
for _, w := range words {
results, indices := parseSingle(transcript, w, neighbors)
results = formatString(results, w)
fmt.Printf("%sPos: Word <%v>%s\n", colorGreen, w, colorNone)
for i, v := range results {
fmt.Printf("%s%03d:%s %v\n", colorGreen, indices[i], colorNone, v)
}
}
return
}
type IGetTrans interface {
GetTrans(stream speechpb.Speech_StreamingRecognizeClient, audioFile string) string
}
func GetTrans(stream speechpb.Speech_StreamingRecognizeClient, audioFile string) string {
// Send configuration
// TODO: Support for formats other than 16kHz, 16bit
if err := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_StreamingConfig{
StreamingConfig: &speechpb.StreamingRecognitionConfig{
Config: &speechpb.RecognitionConfig{
Encoding: speechpb.RecognitionConfig_LINEAR16,
SampleRateHertz: 16000,
LanguageCode: "ja-JP",
},
},
},
}); err != nil {
log.Fatal(err)
}
// Open Audio File
f, err := os.Open(audioFile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
// Goroutine(like thread)
go func() {
buf := make([]byte, 1024) // Make slice
for {
n, err := f.Read(buf)
if n > 0 {
if err := stream.Send(&speechpb.StreamingRecognizeRequest{
StreamingRequest: &speechpb.StreamingRecognizeRequest_AudioContent{
AudioContent: buf[:n],
},
}); err != nil {
log.Printf("Could not send audio: %v", err)
}
}
if err == io.EOF {
if err := stream.CloseSend(); err != nil {
log.Fatalf("Could not close stream: %v", err)
}
return
}
if err != nil {
log.Printf("Could not read from %s: %v", audioFile, err)
continue
}
}
}() // Invoke
for {
resp, err := stream.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Fatalf("Cannot stream results: %v", err)
}
if err := resp.Error; err != nil {
log.Fatalf("Could not recognize: %v", err)
}
// Print the results.
for _, result := range resp.Results {
for _, alt := range result.Alternatives {
// With confidence
// fmt.Fprintf(os.Stdout, "\"%v\" (confidence=%3f)\n", alt.Transcript, alt.Confidence)
// Transcript only
// fmt.Fprintf(os.Stdout, "%v\n", alt.Transcript)
return alt.Transcript
}
}
}
return ""
}
// Full-text search with a single target word
func parseSingle(str string, target string, neighbors int) ([]string, []int) {
// Find Target
rWithNeighbors := regexp.MustCompile(
fmt.Sprintf(`.{0,%d}(%v).{0,%d}`, neighbors, target, neighbors))
ret := rWithNeighbors.FindAllString(str, -1)
// Match indices
byteIndices := rWithNeighbors.FindAllStringSubmatchIndex(str, -1)
var runeIndices []int
for i, _ := range ret {
// Indices is bytecount, re-count with utf8 chars
// - indices[firstPos, lastPos, groupFirstPos, groupLastPos]
runeIndices = append(runeIndices, utf8.RuneCountInString(str[0:byteIndices[i][2]]))
}
return ret, runeIndices
}
// ANSI color
func formatString(results []string, target string) []string {
rTarget := regexp.MustCompile(target)
for i, v := range results {
results[i] = rTarget.ReplaceAllString(v, colorRed+"$0"+colorNone)
}
return results
}