【Microsoft Azure 的1024种玩法】七十二.使用NodeJS手动编写调用Azure Cognitive Services Translator API代码获取其支持翻译的语言集
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |
【简介】
Azure Cognitive Services Translato 是一种基于云的神经网络机器翻译服务,同时也是 REST API 的 Azure 认知服务系列的一部分。
本篇文章将会主要介绍到我们如何使用NodeJS手动编写调用Azure Cognitive Services Translator API代码获取其支持翻译的语言集
【操作步骤】
一.在VS Code 中初始化项目环境
1.使用如下命令初始化创建的项目
npm init -y
具体操作,如下图所示
2.初始化完毕以后,如下图所示,会生成一个名为“package.json"的文件
3.我们新建一个用于配置调用API接口的index.js文件
4.使用npm安装Request依赖库
二.编写获取支持翻译语言列表的API代码
1.如下所示为Azure Cognitive Services Translator 支持翻译的语言集的API请求参数说明
参数 | 说明 |
api-version | 客户端所请求的 API 的版本 |
scope | 逗号分隔的名称列表,用于定义要返回的语言组 |
2.我们在VScode中编写如下代码来调用Azure Cognitive Services Translator API,其中 request中为1中的Azure Cognitive Services Translator API请求参数
const request = require('request')
const fs = require('fs')
const getList = () => {
request('https://api.cognitive.microsofttranslator.com/languages?api-version=3.0', function (err, res, body) {
if (err !== null) {
console.log('error is:', err)
} else {
fs.writeFileSync('./languages.json', body, {encoding: 'utf-8'})
console.log('语言支持列表已写入:./languages.json')
}
})
}
// 获取支持翻译语言列表
getList()
3.调用代码编写完毕后,我们使用node index.js 执行获取Azure Cognitive Services Translator所支持的翻译语言列表,执行完毕以后,我们可以在"languages.json"中看到具体支持的翻译语言列表
node index.js
4.如下图所示,我们获取到的语言支持列表文件
三.响应正文分析及列出支持翻译的语言集
1.通过API的方式调用成功的响应是一个 JSON 对象,其中包含请求的每个组的一个属性,如下图所示:
其中每个属性的值有不同的定义:
- translation 属性
属性的值是 (键, 值) 对的字典。 每个键是一个 BCP 47 语言标记。 键标识文本的翻译目标语言或源语言。
{
"translation": {
"af": {
"name": "Afrikaans",
"nativeName": "Afrikaans",
"dir": "ltr"
},
"am": {
"name": "Amharic",
"nativeName": "አማርኛ",
"dir": "ltr"
},
"ar": {
"name": "Arabic",
"nativeName": "العربية",
"dir": "rtl"
},
}
}
- transliteration 属性
transliteration 属性的值是 (键, 值) 对的字典。 每个键是一个 BCP 47 语言标记。
键标识某种语言,该语言的文本可以从一个脚本转换为另一个脚本。
{
"transliteration": {
...
"ja": {
"name": "Japanese",
"nativeName": "日本語",
"scripts": [
{
"code": "Jpan",
"name": "Japanese",
"nativeName": "日本語",
"dir": "ltr",
"toScripts": [
{
"code": "Latn",
"name": "Latin",
"nativeName": "ラテン語",
"dir": "ltr"
}
]
},
{
"code": "Latn",
"name": "Latin",
"nativeName": "ラテン語",
"dir": "ltr",
"toScripts": [
{
"code": "Jpan",
"name": "Japanese",
"nativeName": "日本語",
"dir": "ltr"
}
]
}
]
},
...
}
}
- dictionary 属性
dictionary 属性的值是 (键, 值) 对的字典。 每个键是一个 BCP 47 语言标记。 键标识支持替代翻译和回译的语言。
"es": {
"name": "Spanish",
"nativeName": "Español",
"dir": "ltr",
"translations": [
{
"name": "English",
"nativeName": "English",
"dir": "ltr",
"code": "en"
}
]
},
2.最终通过Azure Cognitive Services Translator 的API接口所获取到支持翻译的语言列表如下所示
{
"translation": {
"af": {
"name": "Afrikaans",
"nativeName": "Afrikaans",
"dir": "ltr"
},
"am": {
"name": "Amharic",
"nativeName": "አማርኛ",
"dir": "ltr"
},
"ar": {
"name": "Arabic",
"nativeName": "العربية",
"dir": "rtl"
},
"as": {
"name": "Assamese",
"nativeName": "অসমীয়া",
"dir": "ltr"
},
"az": {
"name": "Azerbaijani",
"nativeName": "Azərbaycan",
"dir": "ltr"
},
"ba": {
"name": "Bashkir",
"nativeName": "Bashkir",
"dir": "ltr"
},
"th": {
"name": "Thai",
"nativeName": "ไทย",
"scripts": [
{
"code": "Thai",
"name": "Thai",
"nativeName": "ภาษาไทย",
"dir": "ltr",
"toScripts": [
{
"code": "Latn",
"name": "Latin",
"nativeName": "ละติน",
"dir": "ltr"
}
]
}
]
},
"tt": {
"name": "Tatar",
"nativeName": "Татар",
"scripts": [
{
"code": "Cyrl",
"name": "Cyrillic",
"nativeName": "Cyrillic",
"dir": "ltr",
"toScripts": [
{
"code": "Latn",
"name": "Latin",
"nativeName": "Latin",
"dir": "ltr"
}
]
},
{
"code": "Latn",
"name": "Latin",
"nativeName": "Latin",
"dir": "ltr",
"toScripts": [
{
"code": "Cyrl",
"name": "Cyrillic",
"nativeName": "Cyrillic",
"dir": "ltr"
}
]
}
]
},
"uk": {
"name": "Ukrainian",
"nativeName": "Українська",
"scripts": [
{
"code": "Cyrl",
"name": "Cyrillic",
"nativeName": "Кирилиця",
"dir": "ltr",
"toScripts": [
{
"code": "Latn",
"name": "Latin",
"nativeName": "латиниця",
"dir": "ltr"
}
]
},
{
"code": "Latn",
"name": "Latin",
"nativeName": "латиниця",
"dir": "ltr",
"toScripts": [
{
"code": "Cyrl",
"name": "Cyrillic",
"nativeName": "Кирилиця",
"dir": "ltr"
}
]
}
]
},
"ur": {
"name": "Urdu",
"nativeName": "اردو",
"scripts": [
{
"code": "Arab",
"name": "Arabic",
"nativeName": "Arabic",
"dir": "rtl",
"toScripts": [
{
"code": "Latn",
"name": "Latin",
"nativeName": "Latin",
"dir": "ltr"
}
]
},
{
"code": "Latn",
"name": "Latin",
"nativeName": "Latin",
"dir": "ltr",
"toScripts": [
{
"code": "Arab",
"name": "Arabic",
"nativeName": "Arabic",
"dir": "rtl"
}
]
}
]
},
"zh-Hans": {
"name": "Chinese Simplified",
"nativeName": "中文 (简体)",
"scripts": [
{
"code": "Hans",
"name": "Simplified",
"nativeName": "简体汉语",
"dir": "ltr",
"toScripts": [
{
"code": "Latn",
"name": "Latin",
"nativeName": "拉丁语",
"dir": "ltr"
},
{
"code": "Hant",
"name": "Hat",
"nativeName": "传统",
"dir": "ltr"
}
]
},
{
"code": "Latn",
"name": "Latin",
"nativeName": "拉丁语",
"dir": "ltr",
"toScripts": [
{
"code": "Hans",
"name": "Han",
"nativeName": "简体汉语",
"dir": "ltr"
},
{
"code": "Hant",
"name": "Hat",
"nativeName": "传统",
"dir": "ltr"
}
]
}
]
},
"zh-Hant": {
"name": "Chinese Traditional",
"nativeName": "繁體中文 (繁體)",
"scripts": [
{
"code": "Hant",
"name": "Traditional",
"nativeName": "傳統",
"dir": "ltr",
"toScripts": [
{
"code": "Latn",
"name": "Latin",
"nativeName": "拉丁语",
"dir": "ltr"
},
{
"code": "Hans",
"name": "Han",
"nativeName": "简体汉语",
"dir": "ltr"
}
]
},
{
"code": "Latn",
"name": "Latin",
"nativeName": "拉丁文",
"dir": "ltr",
"toScripts": [
{
"code": "Hans",
"name": "Han",
"nativeName": "简体汉语",
"dir": "ltr"
},
{
"code": "Hant",
"name": "Hat",
"nativeName": "传统",
"dir": "ltr"
}
]
}
]
}
},
"dictionary": {
"af": {
"name": "Afrikaans",
"nativeName": "Afrikaans",
"dir": "ltr",
"translations": [
{
"name": "English",
"nativeName": "English",
"dir": "ltr",
"code": "en"
}
]
},
"ar": {
"name": "Arabic",
"nativeName": "العربية",
"dir": "rtl",
"translations": [
{
"name": "English",
"nativeName": "English",
"dir": "ltr",
"code": "en"
}
]
},
"bg": {
"name": "Bulgarian",
"nativeName": "Български",
"dir": "ltr",
"translations": [
{
"name": "English",
"nativeName": "English",
"dir": "ltr",
"code": "en"
}
]
},
"bn": {
"name": "Bangla",
"nativeName": "বাংলা",
"dir": "ltr",
"translations": [
{
"name": "English",
"nativeName": "English",
"dir": "ltr",
"code": "en"
}
]
},
"bs": {
"name": "Bosnian",
"nativeName": "Bosnian",
"dir": "ltr",
"translations": [
{
"name": "English",
"nativeName": "English",
"dir": "ltr",
"code": "en"
}
]
},
"zh-Hans": {
"name": "Chinese Simplified",
"nativeName": "中文 (简体)",
"dir": "ltr",
"translations": [
{
"name": "English",
"nativeName": "English",
"dir": "ltr",
"code": "en"
}
]
}
}
}
阿里云国内75折 回扣 微信号:monov8 |
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6 |