五分钟学会彩云小译 API 2

五分钟学会彩云小译 API 2

API spec

Contents

1 能用彩云小译 API 做什么?

1.1 维基百科内容翻译

1.2 石墨文档翻译

2 API 应用

2.1 申请访问令牌

2.2 接口说明

2.3 python 示例

3 支持的语言

4 关于语种自动识别

5 想要更多?

能用彩云小译 API 做什么?

彩云小译 API 可以用来把信息翻译到指定的语言,我们可以支持文本、网页、PDF/Word文档、语音、视频等多种形式。

您可以使用它来构建你的应用程序、网站、工具或任何需要多语言支持的解决方案。例如:彩云小译 API 是维基百科自动翻译的技术提供者,也为钉钉、石墨文档、财新传媒等一线机构和众多开发者提供技术支持。

维基百科内容翻译

石墨文档翻译

API 应用

下面的一分钟介绍,我们制作一个命令行工具,可以让你翻译简短的一句话。

申请访问令牌

请先至彩云科技开放平台注册账号,申请开通小译 Token,并获取到 secret_key

每月翻译100万字之内都是免费的,如果您每月超过100万字,我们会按照 20元 / 100万字 的费率收费。(字数按照翻译原文字符计算,包含空格和标点)

接口说明

文本翻译 API 地址

"http://api.interpreter.caiyunai.com/v1/translator"

参数说明

字段名

类型

含义

必填

备注

source

string/list

源文本

true

支持字符串,也支持字符串的 list

trans_type

string

翻译方向

true

见下方支持的语言

detect

boolean

是否自动识别源语种

false

具体见下方说明

salt

string

uuid string

true

根据 salt 来防重放,所以要求是 uuid

current_time

string

当前UTC时间戳(秒)

true

timestamp

sign

string

签名

true

见下方签名方法

签名方法

sign=sha256(YOUR_TOKEN+salt+current_time+YOUR_SECRET_KEY+source_input);

source_input: 如果 source 是 字符串,且长度<= 20,则就是 source,如果长度> 20, 则取字符串的前10个字符+后10个字符(source[:10]+str(len(source))+source[-10:]); 如果 source 是 list, 则以list的第一个 item 为基础,如果长度 <=20,则为source[0],否则为 source[0][:10]+str(len(source[0]))+source[0][-10:];

secret_key: 用户在开放平台获取的 secret_key;

header 说明

headers = {

'content-type': "application/json",

'x-authorization': "token " + YOUR_TOKEN,

}

python 示例

请把 YOUR_TOKEN 设置为你的 token,将 YOUR_SECRET_KEY 设置为你的 secret_key, 并发请求的速度会是逐个请求的数倍,如有可能,请将长度类似的 20-40 个句子打包请求,下面是实例代码。

import requests

import json

import hashlib

import time

import uuid

def truncate(source):

if source is None:

return None

if isinstance(source, str):

source = source.decode('utf-8')

size = len(source)

return source if size <= 20 else source[:10] + str(size) + source[-10:]

def encrypt(sign_str):

hash_algorithm = hashlib.sha256()

hash_algorithm.update(sign_str.encode('utf-8'))

return hash_algorithm.hexdigest()

def tranlate(source, direction):

url = "http://api.interpreter.caiyunai.com/v1/translator"

token = YOUR_TOKEN

secret_key = YOUR_SECRET_KEY

current_time = str(int(time.time()))

salt = str(uuid.uuid1())

source_input = source

if isinstance(source, list):

source_input = source[0]

sign_str = token + salt + current_time + secret_key + truncate(source_input)

payload = {

"source" : source,

"trans_type" : direction,

"detect": True,

"salt": salt,

"current_time": current_time,

"sign": encrypt(sign_str),

}

headers = {

'content-type': "application/json",

'x-authorization': "token " + token,

}

response = requests.request("POST", url, data=json.dumps(payload), headers=headers)

print response.text

return json.loads(response.text)['target']

#source = "彩云小译は最高の翻訳サービスです"

source = ["Lingocloud is the best translation service.","彩云小译は最高の翻訳サービスです"]

target = tranlate(source, "auto2zh")

print target

输出为:

['小译翻译是最好的翻译服务。', '彩云小译是最好的翻译服务']

支持的语言

目前支持的语言和翻译方向如下表所示

ToFrom

中文 zh

英语 en

日文 ja

中文 zh

-

zh2en

zh2ja

英语 en

en2zh

-

-

日文 ja

ja2zh

-

-

关于语种自动识别

即在调用接口时,增加 detect=True 参数。

当 detect=True 时,trans_type 由之前的 4 种:zh2en、en2zh、zh2ja、ja2zh,增加为 7 种:zh2en、en2zh、zh2ja、ja2zh、auto2zh、auto2en、auto2ja。

当 trans_type 为 auto2zh 时:会将 en 和 ja 翻译为 zh,其他语种原样返回;

当 trans_type 为 auto2en 时:会将 zh 翻译为 en,其他语种原样返回;

当 trans_type 为 auto2ja 时:会将 zh 翻译为 ja,其他语种原样返回。

使用 auto2xx 可以自动识别源语言的语种。2019年底将支持到15种以上语言,200种以上语言方向。

想要更多?

去应用商店下载彩云小译App!

访问 http://fanyi.caiyunapp.com ,体验在线版本。

想要更多功能或者反馈使用体验,请写信给 api@caiyunapp.com 联系!

QQ交流反馈群:

相关推荐

正在犹豫飞鬼道还是焚香,大家给点建议呗
惠品折官网好吗网购
移动掌厅我的道具在哪里(移动掌厅)
鼠标一直自己移动怎么办 光标自己移动的解决方案
CSS基础—网页布局(做出你心爱的网页)
《归来》电影基本介绍 归来电影剧情 归来主演人物→买购APP