Skip to content

Commit

Permalink
语音识别热词相关接口
Browse files Browse the repository at this point in the history
  • Loading branch information
wanjiewu committed Nov 17, 2024
1 parent b0d535d commit 4518f8f
Show file tree
Hide file tree
Showing 3 changed files with 384 additions and 0 deletions.
136 changes: 136 additions & 0 deletions ci_media.go
Original file line number Diff line number Diff line change
Expand Up @@ -4184,3 +4184,139 @@ func (s *CIService) CreateMultiGeneratePlayListJobs(ctx context.Context, opt *Cr
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}

type VocabularyWeight struct {
Vocabulary string `xml:"Vocabulary,omitempty"`
Weight int `xml:"Weight,omitempty"`
}

// CreateAsrVocabularyTableOptions TODO
type CreateAsrVocabularyTableOptions struct {
XMLName xml.Name `xml:"Request"`
TableName string `xml:"TableName,omitempty"`
TableDescription string `xml:"TableDescription,omitempty"`
VocabularyWeights []VocabularyWeight `xml:"VocabularyWeights,omitempty"`
VocabularyWeightStr string `xml:"VocabularyWeightStr,omitempty"`
}

// CreateAsrVocabularyTableResult TODO
type CreateAsrVocabularyTableResult struct {
XMLName xml.Name `xml:"Response"`
Code string `xml:"Code,omitempty"`
Message string `xml:"Message,omitempty"`
TableId string `xml:"TableId,omitempty"`
RequestId string `xml:"RequestId,omitempty"`
}

func (s *CIService) CreateAsrVocabularyTable(ctx context.Context, opt *CreateAsrVocabularyTableOptions) (*CreateAsrVocabularyTableResult, *Response, error) {
var res CreateAsrVocabularyTableResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/asrhotvocabtable",
method: http.MethodPost,
body: opt,
result: &res,
}
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}

// DeleteAsrVocabularyTable TODO
func (s *CIService) DeleteAsrVocabularyTable(ctx context.Context, tableId string) (*Response, error) {
sendOpt := sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/asrhotvocabtable/" + tableId,
method: http.MethodDelete,
}
resp, err := s.client.send(ctx, &sendOpt)
return resp, err
}

// CreateAsrVocabularyTableOptions TODO
type UpdateAsrVocabularyTableOptions struct {
XMLName xml.Name `xml:"Request"`
TableId string `xml:"TableId,omitempty"`
TableName string `xml:"TableName,omitempty"`
TableDescription string `xml:"TableDescription,omitempty"`
VocabularyWeights []VocabularyWeight `xml:"VocabularyWeights,omitempty"`
VocabularyWeightStr string `xml:"VocabularyWeightStr,omitempty"`
}

type UpdateAsrVocabularyTableResult CreateAsrVocabularyTableResult

// UpdateAsrVocabularyTable TODO
func (s *CIService) UpdateAsrVocabularyTable(ctx context.Context, opt *UpdateAsrVocabularyTableOptions) (*UpdateAsrVocabularyTableResult, *Response, error) {
var res UpdateAsrVocabularyTableResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/asrhotvocabtable",
method: http.MethodPut,
body: opt,
result: &res,
}
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}

type VocabularyTable struct {
TableId string `xml:"TableId,omitempty"`
TableName string `xml:"TableName,omitempty"`
TableDescription string `xml:"TableDescription,omitempty"`
VocabularyWeights []VocabularyWeight `xml:"VocabularyWeights,omitempty"`
VocabularyWeightStr string `xml:"VocabularyWeightStr,omitempty"`
CreateTime string `xml:"CreateTime,omitempty"`
UpdateTime string `xml:"UpdateTime,omitempty"`
}

// DescribeAsrVocabularyTableResult TODO
type DescribeAsrVocabularyTableResult struct {
XMLName xml.Name `xml:"Response"`
RequestId string `xml:"RequestId,omitempty"`
TableId string `xml:"TableId,omitempty"`
TableName string `xml:"TableName,omitempty"`
TableDescription string `xml:"TableDescription,omitempty"`
VocabularyWeights []VocabularyWeight `xml:"VocabularyWeights,omitempty"`
VocabularyWeightStr string `xml:"VocabularyWeightStr,omitempty"`
CreateTime string `xml:"CreateTime,omitempty"`
UpdateTime string `xml:"UpdateTime,omitempty"`
}

// DescribeAsrVocabularyTable 查询指定的语音识别热词表
func (s *CIService) DescribeAsrVocabularyTable(ctx context.Context, tableId string) (*DescribeAsrVocabularyTableResult, *Response, error) {
var res DescribeAsrVocabularyTableResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/asrhotvocabtable/" + tableId,
method: http.MethodGet,
result: &res,
}
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}

// DescribeAsrVocabularyTablesOptions TODO
type DescribeAsrVocabularyTablesOptions struct {
Offset int `url:"offset,omitempty"`
Limit int `url:"limit,omitempty"`
}

type DescribeAsrVocabularyTablesResult struct {
XMLName xml.Name `xml:"Response"`
RequestId string `xml:"RequestId,omitempty"`
TotalCount string `xml:"TotalCount,omitempty"`
VocabularyTable []VocabularyTable `xml:"VocabularyTable,omitempty"`
}

// DescribeAsrVocabularyTables 查询语音识别热词表列表
func (s *CIService) DescribeAsrVocabularyTables(ctx context.Context, opt *DescribeAsrVocabularyTablesOptions) (*DescribeAsrVocabularyTablesResult, *Response, error) {
var res DescribeAsrVocabularyTablesResult
sendOpt := sendOptions{
baseURL: s.client.BaseURL.CIURL,
uri: "/asrhotvocabtable",
optQuery: opt,
method: http.MethodGet,
result: &res,
}
resp, err := s.client.send(ctx, &sendOpt)
return &res, resp, err
}
100 changes: 100 additions & 0 deletions ci_media_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3728,3 +3728,103 @@ func TestCIService_CreateMultiGeneratePlayListJobs(t *testing.T) {
t.Fatalf("CI.CreateMultiGeneratePlayListJobs returned errors: %v", err)
}
}

func TestCIService_CreateAsrVocabularyTable(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/asrhotvocabtable", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
})

opt := &CreateAsrVocabularyTableOptions{
TableName: "test",
TableDescription: "test",
VocabularyWeights: []VocabularyWeight{
{
Vocabulary: "test",
Weight: 10,
},
},
VocabularyWeightStr: "",
}
_, _, err := client.CI.CreateAsrVocabularyTable(context.Background(), opt)
if err != nil {
t.Fatalf("CI.CreateAsrVocabularyTable returned error: %v", err)
}
}

func TestCIService_DeleteAsrVocabularyTable(t *testing.T) {
setup()
defer teardown()

tableId := "123"
mux.HandleFunc(fmt.Sprintf("/asrhotvocabtable/%s", tableId), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodDelete)
})

_, err := client.CI.DeleteAsrVocabularyTable(context.Background(), tableId)
if err != nil {
t.Fatalf("CI.DeleteAsrVocabularyTable returned error: %v", err)
}
}

func TestCIService_UpdateAsrVocabularyTable(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/asrhotvocabtable", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPut)
})

opt := &UpdateAsrVocabularyTableOptions{
TableId: "123",
TableName: "test",
TableDescription: "test",
VocabularyWeights: []VocabularyWeight{
{
Vocabulary: "test",
Weight: 10,
},
},
VocabularyWeightStr: "",
}
_, _, err := client.CI.UpdateAsrVocabularyTable(context.Background(), opt)
if err != nil {
t.Fatalf("CI.UpdateAsrVocabularyTable returned error: %v", err)
}
}

func TestCIService_DescribeAsrVocabularyTable(t *testing.T) {
setup()
defer teardown()

tableId := "123"
mux.HandleFunc(fmt.Sprintf("/asrhotvocabtable/%s", tableId), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
})

_, _, err := client.CI.DescribeAsrVocabularyTable(context.Background(), tableId)
if err != nil {
t.Fatalf("CI.DescribeAsrVocabularyTable returned error: %v", err)
}
}

func TestCIService_DescribeAsrVocabularyTables(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/asrhotvocabtable", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
})

opt := &DescribeAsrVocabularyTablesOptions{
Offset: 0,
Limit: 100,
}

_, _, err := client.CI.DescribeAsrVocabularyTables(context.Background(), opt)
if err != nil {
t.Fatalf("CI.DescribeAsrVocabularyTables returned error: %v", err)
}
}
148 changes: 148 additions & 0 deletions example/CI/workflow_and_job/vocabulary_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package main

import (
"context"
"fmt"
"net/http"
"net/url"
"os"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed")
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

func getClient() *cos.Client {
u, _ := url.Parse("https://test-1234567890.cos.ap-chongqing.myqcloud.com")
cu, _ := url.Parse("https://test-1234567890.ci.ap-chongqing.myqcloud.com")
b := &cos.BaseURL{BucketURL: u, CIURL: cu}
c := cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: true,
// Notice when put a large file and set need the request body, might happend out of memory error.
RequestBody: true,
ResponseHeader: true,
ResponseBody: true,
},
},
})
return c
}

func CreateAsrVocabularyTable() {
// 创建语音识别词表
c := getClient()
opt := &cos.CreateAsrVocabularyTableOptions{
TableName: "test",
TableDescription: "test",
VocabularyWeights: []cos.VocabularyWeight{
{
Vocabulary: "test",
Weight: 10,
},
},
}
_, _, err := c.CI.CreateAsrVocabularyTable(context.Background(), opt)
if err != nil {
panic(err)
}
// fmt.Printf("%+v\n", res)
}

func DescribeAsrVocabularyTables() {
// 查询语音识别词表
c := getClient()
opt := &cos.DescribeAsrVocabularyTablesOptions{
Offset: 0,
Limit: 10,
}
_, _, err := c.CI.DescribeAsrVocabularyTables(context.Background(), opt)
if err != nil {
panic(err)
}
// fmt.Printf("%+v\n", res)
}

func DeleteAsrVocabularyTable() {
// 查询语音识别词表
c := getClient()

tableId := "c0398427aa1911eebe3c446a2eb5fd98"

_, err := c.CI.DeleteAsrVocabularyTable(context.Background(), tableId)
if err != nil {
panic(err)
}
// fmt.Printf("%+v\n", res)
}

func DescribeAsrVocabularyTable() {
// 查询语音识别词表
c := getClient()

tableId := "fc6bd0ce320d11ef8484525400aec391"

res, _, err := c.CI.DescribeAsrVocabularyTable(context.Background(), tableId)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", res)
}

func UpdateAsrVocabularyTable() {
// 查询语音识别词表
c := getClient()

tableId := "fc6bd0ce320d11ef8484525400aec391"
opt := &cos.UpdateAsrVocabularyTableOptions{
TableName: "test",
TableDescription: "test",
TableId: tableId,
VocabularyWeights: []cos.VocabularyWeight{
{
Vocabulary: "test",
Weight: 10,
},
},
}
_, _, err := c.CI.UpdateAsrVocabularyTable(context.Background(), opt)
if err != nil {
panic(err)
}
// fmt.Printf("%+v\n", res)

res, _, err := c.CI.DescribeAsrVocabularyTable(context.Background(), tableId)
if err != nil {
panic(err)
}
fmt.Printf("%+v\n", res)
}

func main() {
// CreateAsrVocabularyTable()
// DescribeAsrVocabularyTables()
// DeleteAsrVocabularyTable()
// DescribeAsrVocabularyTable()
UpdateAsrVocabularyTable()
}

0 comments on commit 4518f8f

Please sign in to comment.