创建热词词表

更新时间:2025-01-06 03:47:53

本文主要介绍如何通过API创建热词词表。

请求参数

功能名称

参数

类型

默认值

说明

功能名称

参数

类型

默认值

说明

词表名称

Name

string

-

必选,请设置热词词表的名称。

词表描述

Description

string

-

用于记录词表的具体信息。

热词权重

WordWeights

string

-

必选,词表里的词和对应的权重,为JSONMap格式字符串。例如:

{
 "苹果":2,
 "西瓜":2
}

keyString类型的热词,valueInt类型的热词对应的权重。

说明
  • 权重取值范围[-6,5]。

  • 取值大于0会增大该词语被识别的概率,小于0会减小该词语被识别的概率。

  • 取值为-6:表示尽量不要识别出该词语。

  • 取值为2:常用值。

  • 如果效果不明显,可以适当增加权重,但是当权重较大时可能会引起负面效果,导致其他词语识别不准确。

示例代码

Python
Java
Go
#!/usr/bin/env python
#coding=utf-8

import os
import json
import datetime
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.request import CommonRequest
from aliyunsdkcore.auth.credentials import AccessKeyCredential

def create_common_request(domain, version, protocolType, method, uri):
    request = CommonRequest()
    request.set_accept_format('json')
    request.set_domain(domain)
    request.set_version(version)
    request.set_protocol_type(protocolType)
    request.set_method(method)
    request.set_uri_pattern(uri)
    request.add_header('Content-Type', 'application/json')
    return request

def init_parameters():
    root = dict()
    root['Name'] = '输入您的词表名称'
    root['Description'] = '输入您的词表描述'

    words = dict()
    words['苹果'] = 3
    words['西瓜'] = 3
    root['WordWeights'] = words

    return root

body = init_parameters()
print(body)

# TODO  请通过环境变量设置您的 AccessKeyId 和 AccessKeySecret
credentials = AccessKeyCredential(os.environ['ALIBABA_CLOUD_ACCESS_KEY_ID'], os.environ['ALIBABA_CLOUD_ACCESS_KEY_SECRET'])
client = AcsClient(region_id='cn-beijing', credential=credentials)

request = create_common_request('tingwu.cn-beijing.aliyuncs.com', '2023-09-30', 'https', 'POST', '/openapi/tingwu/v2/resources/phrases')

request.set_content(json.dumps(body).encode('utf-8'))
response = client.do_action_with_exception(request)
print("response: \n" + json.dumps(json.loads(response), indent=4, ensure_ascii=False))
package com.alibaba.tingwu.client.demo.offlinetask;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.FormatType;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.aliyuncs.profile.DefaultProfile;
import org.junit.Test;

/**
 * @author tingwu2023
 * @desc 创建热词词表demo。
 */
public class CreateTranscriptionPhrasesTest {

    @Test
    public void summitTask() throws ClientException {
        CommonRequest request = createCommonRequest("tingwu.cn-beijing.aliyuncs.com", "2023-09-30", ProtocolType.HTTPS, MethodType.POST, "/openapi/tingwu/v2/resources/phrases");

        JSONObject root = new JSONObject();
        root.put("Name", "输入您的热词词表名称");
        root.put("Description", "输入您的热词词表描述");

        JSONObject wordWeights = new JSONObject();
        wordWeights.fluentPut("苹果", 3)
        .fluentPut("草莓", 3);
        root.put("WordWeights", wordWeights);

        System.out.println(root.toJSONString());
        request.setHttpContent(root.toJSONString().getBytes(), "utf-8", FormatType.JSON);

        // TODO 请通过环境变量设置您的AccessKeyId、AccessKeySecret
        DefaultProfile profile = DefaultProfile.getProfile("cn-beijing", System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"), System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"));
        IAcsClient client = new DefaultAcsClient(profile);
        CommonResponse response = client.getCommonResponse(request);
        System.out.println(response.getData());
    }
  
    public static CommonRequest createCommonRequest(String domain, String version, ProtocolType protocolType, MethodType method, String uri) {
        // 创建API请求并设置参数
        CommonRequest request = new CommonRequest();
        request.setSysDomain(domain);
        request.setSysVersion(version);
        request.setSysProtocol(protocolType);
        request.setSysMethod(method);
        request.setSysUriPattern(uri);
        request.setHttpContentType(FormatType.JSON);
        return request;
    }
}
package main

import (
	"encoding/json"
	"log"
	"os"

	"github.com/aliyun/alibaba-cloud-sdk-go/sdk"
	"github.com/aliyun/alibaba-cloud-sdk-go/sdk/requests"
)

const (
	apiDomain    = "tingwu.cn-beijing.aliyuncs.com"
	apiVersion   = "2023-09-30"
	accessKeyEnv = "ALIBABA_CLOUD_ACCESS_KEY_ID"
	secretKeyEnv = "ALIBABA_CLOUD_ACCESS_KEY_SECRET"
	region       = "cn-beijing"
)

// TaskBodyParam 是创建短语任务的参数结构体
type TaskBodyParam struct {
	Name        string         `json:"Name"`        // 词表名称
	Description string         `json:"Description"` // 词表描述
	WordWeights map[string]int `json:"WordWeights"` // 热词权重映射
}

func (t *TaskBodyParam) String() string {
	v, _ := json.Marshal(t)
	return string(v)
}

func CreateCommonRequest() *requests.CommonRequest {
	request := requests.NewCommonRequest()
	request.Method = "POST"
	request.Domain = apiDomain
	request.Version = apiVersion
	request.PathPattern = "/openapi/tingwu/v2/resources/phrases"
	request.SetContentType("application/json")
	request.SetScheme("https")

	return request
}

func CreatePhrase(in *TaskBodyParam) error {
	// 请通过环境变量设置您的AccessKeyId、AccessKeySecret
	akkey := os.Getenv(accessKeyEnv)
	aksecret := os.Getenv(secretKeyEnv)
	
	client, err := sdk.NewClientWithAccessKey(region, akkey, aksecret)
	if err != nil {
		log.Default().Fatalln(err)
		return err
	}

	log.Default().Println("request body:\n", in)

	body, err := json.Marshal(in)
	if err != nil {
		log.Default().Fatalln(err)
		return err
	}
	req := CreateCommonRequest()
	req.SetContent(body)

	response, err := client.ProcessCommonRequest(req)
	if err != nil {
		log.Default().Fatalln(err)
		return err
	}

	log.Default().Println("response body:\n" + string(response.GetHttpContentBytes()))

	return nil
}

func main() {
	wordweight := map[string]int{"苹果": 3, "西瓜": 3}
	param := &TaskBodyParam{
		Name:        "输入您的词表名称",
		Description: "输入您的词表描述",
		WordWeights: wordweight,
	}
	if err := CreatePhrase(param); err != nil {
		log.Println(err)
	}
}

示例输出

{
  "Code": "0",
  "Data": {
    "PhraseId": "4424074c1d02421bb88849c3fa11****",
    "Status": "SUCCEEDED"
  },
  "Message": "success",
  "RequestId": "30CAC8F3-903C-5F30-BD37-120B413E*****"
}

协议解析

具体字段定义如下。

参数名

类型

说明

参数名

类型

说明

PhraseId

string

热词的词表Id,用于后续的词表更新维护以及音视频转写的效果增强。

Status

string

任务状态

RequestId

string

RequestId用于排查问题使用。

  • 本页导读 (0)
  • 请求参数
  • 示例代码
  • 示例输出
  • 协议解析
AI助理

点击开启售前

在线咨询服务

你好,我是AI助理

可以解答问题、推荐解决方案等