-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathisi.go
180 lines (158 loc) · 5.17 KB
/
isi.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
//Package isi is alibaba Intelligent Speech Interaction go sdk
package isi
import (
"bytes"
"crypto/hmac"
"crypto/md5"
"crypto/sha1"
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
)
//Client isi client
type Client struct {
ak string
sk string
skbs []byte
}
//New retrurn new isi client
func New(ak, sk string) *Client {
return &Client{ak: ak, sk: sk, skbs: []byte(sk)}
}
func (c Client) gmt() string {
loc, err := time.LoadLocation("GMT")
if err != nil {
return ""
}
return time.Now().In(loc).Format("Mon, 02 Jan 2006 15:04:05 GMT")
}
func (c Client) md5base64(bs []byte) string {
if len(bs) == 0 {
return ""
}
h := md5.New()
h.Write(bs)
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func (c Client) sha1base64(txt string) string {
h := hmac.New(sha1.New, c.skbs)
h.Write([]byte(txt))
return base64.StdEncoding.EncodeToString(h.Sum(nil))
}
func (c Client) sendRequest(method, url string, body []byte, contentType string) []byte {
var (
gmt = c.gmt()
md5body = c.md5base64(body)
)
if strings.Contains(url, "https://nlsapi.aliyun.com/recognize") {
md5body = c.md5base64([]byte(md5body))
}
if contentType == "" {
contentType = "application/json"
}
sign := c.sha1base64(method + "\napplication/json\n" + md5body + "\n" + contentType + "\n" + gmt)
client := &http.Client{}
request, err := http.NewRequest(method, url, bytes.NewBuffer(body))
if err != nil {
log.Println(err)
}
request.Header.Set("Authorization", "Dataplus "+c.ak+":"+sign)
request.Header.Set("Content-type", contentType)
request.Header.Set("Accept", "application/json")
request.Header.Set("Date", gmt)
response, _ := client.Do(request)
respBody, _ := ioutil.ReadAll(response.Body)
// fmt.Println(string(respBody))
return respBody
}
//Get return get url http response body
func (c Client) Get(url string) []byte {
return c.sendRequest("GET", url, nil, "")
}
// GetVocabs get vocabs
// {"request_id":"669865051b154e588ba29dcba0e8b2ae","global_weight":1,"words":["香蕉","牛奶","哈密瓜"],"word_weights":null}
func (c Client) GetVocabs(id string) Vocab {
vocab := Vocab{}
json.Unmarshal(c.sendRequest("GET", "https://nlsapi.aliyun.com/asr/custom/vocabs/"+id, nil, ""), &vocab)
return vocab
}
// CreateVocabs return created vocabs id
// {"request_id":"d3b0f95d700d4bbd81db8090b34e670d","vocabulary_id":"85ea46c8a7c1456685b0df479d929477"}
func (c Client) CreateVocabs(v Vocab) string {
bs, _ := json.Marshal(&v)
ret := vocabRet{}
json.Unmarshal(c.sendRequest("POST", "https://nlsapi.aliyun.com/asr/custom/vocabs", bs, ""), &ret)
return ret.VocabularyID
}
// UpdateVocabs update id with vocab
// {"request_id":"96a9478fa4364db8a2b623eb2f50ddb2"}
// {"id":"856271ec9e9942c6b932fbdfc8af060e","request_id":"856271ec9e9942c6b932fbdfc8af060e","error_code":100307,"error_message":"\"VOCABULARY_NOT_FOUND(Vocabulary not exist! oid=1744412405438088, vid=85ea46c8a7c1456685b0df479d929472)\""}
func (c Client) UpdateVocabs(id string, v Vocab) bool {
bs, _ := json.Marshal(&v)
ret := vocabRet{}
json.Unmarshal(c.sendRequest("PUT", "https://nlsapi.aliyun.com/asr/custom/vocabs/"+id, bs, ""), &ret)
if ret.ErrorCode == 0 {
return true
}
log.Println(ret.ErrorMessage)
return false
}
// DeleteVocabs delete the id table
// {"request_id":"a6ad7f0436f4491b84601045affa9d82"}
// {"id":"9a3c74d2ffdb4b6d952de97c531dda90","request_id":"9a3c74d2ffdb4b6d952de97c531dda90","error_code":100307,"error_message":"\"VOCABULARY_NOT_FOUND(Vocabulary not exist! oid=1744412405438088, vid=85ea46c8a7c1456685b0df479d929472)\""}
func (c Client) DeleteVocabs(id string) bool {
ret := vocabRet{}
json.Unmarshal(c.sendRequest("DELETE", "https://nlsapi.aliyun.com/asr/custom/vocabs/"+id, nil, ""), &ret)
if ret.ErrorCode == 0 {
return true
}
log.Println(ret.ErrorMessage)
return false
}
// Recognize one sentence recognize
// {"result":"后天下午5点25分,整形外科需要手术账号36,携带传送到手术室。","request_id":"51e1005bf454476a91319d5bd311075e"}
func (c Client) Recognize(localPath string, models ...string) (string, error) {
model := ""
if len(models) > 0 {
model = models[0]
} else {
model = "customer-service"
}
bs, err := ioutil.ReadFile(localPath)
if err != nil {
return "", err
}
ret := vocabRet{}
// 这里不同文件要做不同的识别,但是我只用pcm,所以不管了。
contentType := "audio/pcm; samplerate=16000"
json.Unmarshal(c.sendRequest("POST", "https://nlsapi.aliyun.com/recognize?version=2.0&model="+model, bs, contentType), &ret)
if ret.ErrorCode == 0 {
return ret.Result, nil
}
return "", errors.New(ret.ErrorMessage)
}
// Transcriptions translate record file
func (c Client) Transcriptions(ossPath, vobID string) string {
tb := TransBody{}
tb.OssLink = ossPath
tb.AppKey = "nls-service-multi-domain"
tb.VocabularyID = vobID
bs, _ := json.Marshal(&tb)
id := transID{}
json.Unmarshal(c.sendRequest("POST", "https://nlsapi.aliyun.com/transcriptions", bs, ""), &id)
ret := transRet{}
for {
json.Unmarshal(c.sendRequest("GET", "https://nlsapi.aliyun.com/transcriptions/"+id.ID, nil, ""), &ret)
if ret.Status == "SUCCEED" {
break
}
time.Sleep(500 * time.Millisecond)
}
text := ret.Result[0].Text
return text
}