You need to enable JavaScript to run this app.
导航
文本语种检测API
最近更新时间:2023.06.30 10:33:52首次发布时间:2021.02.23 10:42:10

接口描述

文本语种检测服务目前支持140种语言的识别检测,可识别文本所属语言种类,返回最可靠的结果。广泛应用于通用文本、商务沟通、搜索、广告、游戏等场景语种识别任务。

接口说明

  • 请求方法
    POST
  • 请求体
字段类型是否为必须项说明备注
TextList[String]待检测的文本列表列表长度不超过16
  • 返回体
字段类型说明备注
ResponseMetadataResponseMetadata通用字段
DetectedLanguageList[DetectedLanguage]
  • Language:: String
    检测结果
  • Confidence:: Double
    置信度
置信度范围为0-1;置信度越高,检测结果越可靠
  • 样例
    • 请求体
    {
        "TextList": [
            "Hello world"
        ]
    }
    
    • 返回体
    {
        "DetectedLanguageList": [
            {
                "Language": "en",
                "Confidence": 1
            }
        ],
        "ResponseMetadata": {
            "RequestId": "202004092241580100140440770840151B",
            "Action": "LangDetect",
            "Version": "2020-06-01",
            "Service": "translate",
            "Region": "cn-north-1",
            "Error": null
        }
    }
    

示例代码

  • 使用 Python

依赖 volc-sdk-python

import json

from volcengine.ApiInfo import ApiInfo
from volcengine.Credentials import Credentials
from volcengine.ServiceInfo import ServiceInfo
from volcengine.base.Service import Service

k_access_key = '密钥管理中的 AccessKey ID' # https://console.volcengine.com/iam/keymanage/
k_secret_key = '密钥管理中的 AccessKey Secret'
k_timeout = 5  # second
k_service_info = \
    ServiceInfo('open.volcengineapi.com',
                {'Content-Type': 'application/json'},
                Credentials(k_access_key, k_secret_key, 'translate', 'cn-north-1'),
                5,
                5)
k_query = {
    'Action': 'LangDetect',
    'Version': '2020-06-01'
}
k_api_info = {
    'langdetect': ApiInfo('POST', '/', k_query, {}, {})
}
service = Service(k_service_info, k_api_info)
body = {
    'TextList': ['Hello world', 'こんにちは世界'],
}
print(service.json('langdetect', {}, json.dumps(body)))
  • 使用 Go
package main

import (
    "fmt"
    "net/http"
    "net/url"
    "time"

    "github.com/volcengine/volc-sdk-golang/base"
)

const (
    kAccessKey      = "密钥管理中的 AccessKey ID" // https://console.volcengine.com/iam/keymanage/
    kSecretKey      = "密钥管理中的 AccessKey Secret"
    kServiceVersion = "2020-06-01"
)

var (
    ServiceInfo = &base.ServiceInfo{
        Timeout: 5 * time.Second,
        Host:    "open.volcengineapi.com",
        Header: http.Header{
            "Accept": []string{"application/json"},
        },
        Credentials: base.Credentials{Region: base.RegionCnNorth1, Service: "translate"},
    }
    ApiInfoList = map[string]*base.ApiInfo{
        "LangDetect": {
            Method: http.MethodPost,
            Path:   "/",
            Query: url.Values{
                "Action":  []string{"LangDetect"},
                "Version": []string{kServiceVersion},
            },
        },
    }
)

func newClient() *base.Client {
    client := base.NewClient(ServiceInfo, ApiInfoList)
    client.SetAccessKey(kAccessKey)
    client.SetSecretKey(kSecretKey)
    return client
}

func main() {
    client := newClient()
    resp, code, err := client.Json("LangDetect", nil, `{"TextList":["你好世界"]}`)
    if err != nil {
        panic(err)
    }
    fmt.Printf("%d %s\n", code, string(resp))
}