-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.go
110 lines (94 loc) · 2.73 KB
/
test.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
package main
import (
"fmt"
"io/ioutil"
"path/filepath"
"time"
"github.com/bitly/go-simplejson"
spch "github.com/tuputech/tupu-go-sdk/recognition/speech/speechsync"
)
func main() {
// step1. get your secretID
secretID := "your secretID"
privateKeyPath := "rsa_private_key.pem"
// step2. create speech handler
speechHandler, err := spch.NewSyncHandler(privateKeyPath)
if err != nil {
fmt.Println("-------- ERROR ----------")
return
}
// step3. get recognition result
// test demo1
testSpeechAPIWithURL(secretID, speechHandler)
// test demo2
testSpeechAPIWithPath(secretID, speechHandler)
// test demo3
testSpeechAPIWithBinary(secretID, speechHandler)
}
func testSpeechAPIWithBinary(secretID string, speechHandler *spch.SyncHandler) {
//Using local file or binary data
filePath := "your speech filePath"
fileBytes, e2 := ioutil.ReadFile(filePath)
if e2 != nil {
fmt.Printf("Could not load voice: %v", e2)
return
}
// key is your fileName, value is the speech binary data
speechSlice := map[string][]byte{
filepath.Base(filePath): fileBytes,
}
printResult(speechHandler.PerformWithBinary(secretID, speechSlice))
}
func testSpeechAPIWithPath(secretID string, speechHandler *spch.SyncHandler) {
// step1. get speech file path
speechPaths := []string{
"your speech filePath",
}
// step2. get result of speech recognition API
printResult(speechHandler.PerformWithPath(secretID, speechPaths))
}
func testSpeechAPIWithURL(secretID string, speechHandler *spch.SyncHandler) {
// step1. get speech file url
speechURLs := []string{
"your speech url",
}
printResult(speechHandler.PerformWithURL(secretID, speechURLs))
}
func printResult(result string, statusCode int, err error) {
fmt.Printf("result: %s\n", result)
if err != nil {
fmt.Printf("Failed: %v\n", err)
return
}
fmt.Println("-------- v1.0 --------")
// fmt.Println(result)
fmt.Printf("Status-Code: %v\n-----\n", statusCode)
// Example of parsing json string using simplejson
var (
rlt, e = simplejson.NewJson([]byte(result))
task map[string]interface{}
code, message string
timestamp int64
)
if e != nil {
fmt.Println("[ERROR] params error")
return
}
// Get the value corresponding to the key in json
code, e = rlt.Get("code").String()
message, e = rlt.Get("message").String()
timestamp, e = rlt.Get("timestamp").Int64()
timestamp = int64(float64(timestamp) / 1000)
task, e = rlt.Map()
// parse vulgar speech
// task, e = rlt.Get("5c8213b9bc807806aab0a574").Map()
// if e != nil {
// fmt.Println("decode error")
// return
// }
fmt.Printf("- Code: %v %v\n- Time: %v\n", code, message, time.Unix(timestamp, 0))
for k, v := range task {
fmt.Printf("- Task: [%v]\n%v\n", k, v)
}
fmt.Println("----------------------")
}