【Microsoft Azure 的1024种玩法】七十三.通过调用Azure Cognitive Services Translator API 快速将单个句子从简体中文翻译为英文

【简介】

Azure Cognitive Services Translato 是一种基于云的神经网络机器翻译服务,同时也是 REST API 的 Azure 认知服务系列的一部分。
本篇文章将会主要分享的内容为“如何通过调用Azure Cognitive Services Translator API 快速将单个句子从简体中文翻译为英文”

【前期文章】

【Microsoft Azure 的1024种玩法】一.一分钟快速上手搭建宝塔管理面板
【Microsoft Azure 的1024种玩法】二.基于Azure云平台的安全攻防靶场系统构建
【Microsoft Azure 的1024种玩法】三.基于Azure云平台构建Discuz论坛
【Microsoft Azure 的1024种玩法】四. 利用Azure Virtual machines 打造个人专属云盘,速度吊打某云盘
【Microsoft Azure 的1024种玩法】五.基于Azure Cloud Shell 一站式创建Linux VM
【Microsoft Azure 的1024种玩法】六.使用Azure Cloud Shell对Linux VirtualMachines 进行生命周期管理
【Microsoft Azure 的1024种玩法】七.Azure云端搭建部署属于自己的维基百科
【Microsoft Azure 的1024种玩法】八. 基于Azure云端轻松打造一款好用的私有云笔记
【Microsoft Azure 的1024种玩法】九. Microsoft Azure云端轻松构建部署PostgreSQL数据库
【Microsoft Azure 的1024种玩法】十. 基于Azure App Service 快速上手部署 ASP.NET Web 应用
【Microsoft Azure 的1024种玩法】十一.手动对Azure磁盘进行IOPS性能压力测试
【Microsoft Azure 的1024种玩法】十二. 使用Auzre cloud 安装搭建docker+vulhub靶场
【Microsoft Azure 的1024种玩法】十三.Azure cloud|带你快速搭建DVWA靶场漏洞环境
【Microsoft Azure 的1024种玩法】十四.Azure DNS 在线域名委托解析操作指南
【Microsoft Azure 的1024种玩法】十五.通过Web浏览器对Auzre VM 服务器运维管理
【Microsoft Azure 的1024种玩法】十六.Cobaltstrike内网渗透工具在Azure Cloud中的部署使用
【Microsoft Azure 的1024种玩法】 十七.在Microsoft Azure上动手搭建专属私有文件同步服务]
【Microsoft Azure 的1024种玩法】 十八.快速创建Microsoft Azure SQL数据库初体验
【Microsoft Azure 的1024种玩法】 十九.使用Azure备份服务对虚拟机快速备份
【Microsoft Azure 的1024种玩法】 二十.Azure Multi-Factor Authentication多因素验证最佳实战
【Microsoft Azure 的1024种玩法】 二十一.利用Azure Blob Service 快速实现静态网站托管
【Microsoft Azure 的1024种玩法】 二十二.中国区Azure Cloud 多重身份验证最佳实践
【Microsoft Azure 的1024种玩法】 二十三.快速上手Azure Content Delivery Network 内容分发网络
【Microsoft Azure 的1024种玩法】二十四.通过Azure Front Door 的 Web 应用程序防火墙来对 OWASP TOP 10 威胁进行防御
【Microsoft Azure 的1024种玩法】 二十五.使用Azure CDN对Azure Blob 静态托管站点进行加速
【Microsoft Azure 的1024种玩法】二十六. 在Azure VM中手动部署Windows Admin Center管理平台
【Microsoft Azure 的1024种玩法】二十七. Azure Virtual Desktop虚拟桌面之快速创建配置(一)
【Microsoft Azure 的1024种玩法】二十八. 基于Azure Cloud搭建IPS入侵防御系统实现安全流量实时分析
【Microsoft Azure 的1024种玩法】二十九.基于Azure VM快速实现网络入侵检测 (IDS) 及网络安全监视 (NSM)
【Microsoft Azure 的1024种玩法】三十. 使用Azure Data Studio之快速上手连接管理Azure SQL 数据库(一)
【Microsoft Azure 的1024种玩法】三十一.使用Azure Logic App 创建基于计划定期执行邮件发送的自动化工作流
【Microsoft Azure 的1024种玩法】三十二. 利用 AzCopy来对Azure Blob Storage中的数据进行复制迁移
【Microsoft Azure 的1024种玩法】三十三.十分钟快速部署 Azure Kubernetes Service 群集
【Microsoft Azure 的1024种玩法】三十四.将本地数据文件快速迁移到Azure Blob云存储最佳实践
【Microsoft Azure 的1024种玩法】三十五.在Azure Virtual machines中手动部署Java Web
【Microsoft Azure 的1024种玩法】三十六.通过网络文件系统NFS协议快速将Blob 存储挂在Azure Virtual machines中
【Microsoft Azure 的1024种玩法】三十七.通过Azure Virtual Machines实现远程连接本地内网服务器(内网穿透)
【Microsoft Azure 的1024种玩法】三十八.通过Azure Function App函数应用实现对Azure Virtual Machines定时开关机
【Microsoft Azure 的1024种玩法】三十九.使用Auzre Media Services实现在线实时视频直播
【Microsoft Azure 的1024种玩法】四十.快速将阿里云DNS解析平滑迁移至Azure Zone实战操作
【Microsoft Azure 的1024种玩法】四十一. 将Windows Admin Center 网关注册到 Azure
【Microsoft Azure 的1024种玩法】四十二. 通过Windows Admin Center快速创建Azure Virtual Machines
【Microsoft Azure 的1024种玩法】四十三. 基于Azure云平台构建RTMP直播服务
【Microsoft Azure 的1024种玩法】四十五.在Azure中快速创建Azure AD Domain Services
【Microsoft Azure 的1024种玩法】四十六.将 Azure VM 实例添加到Azure AD Domain Service中
【Microsoft Azure 的1024种玩法】四十七.通过Azure Virtual machines搭建FTP应用服务
【Microsoft Azure 的1024种玩法】四十八.基于Azure Virtual Machines快速搭建SQL Server应用
【Microsoft Azure 的1024种玩法】四十九.在Azure中使用Azure VirtualMachines 搭建Microsoft SharePoint 2016
【Microsoft Azure 的1024种玩法】五十. 通过Azure文件共享将Azure File 挂载到本地机器实现文件实时共享
【Microsoft Azure 的1024种玩法】五十一.在Azure Virtual Machines 上装载 SMB Azure 文件共享
【Microsoft Azure 的1024种玩法】五十二.基于Azure Application Insight对Web应用进行实时链路跟踪
【Microsoft Azure 的1024种玩法】五十三.针对Azure Application Insight监控的应用程序指标配置不同维度的自动化报警通知
【Microsoft Azure 的1024种玩法】五十四. 十分钟快速上手创建部署Azure speech服务
【Microsoft Azure 的1024种玩法】五十五.Azure speech service之通过JavaScript快速实现文本转换为语音
【Microsoft Azure 的1024种玩法】五十六.使用Node.js对Azure blob进行生命周期管理
【Microsoft Azure 的1024种玩法】五十七.通过自定义映像批量创建Azure Virtual machines实现集群业务快速部署
【Microsoft Azure 的1024种玩法】五十八.基于Azure云平台快速构建开源任务管理工具
【Microsoft Azure 的1024种玩法】五十九.基于Azure云平台快速搭建GitLab应用实现代码托管
【Microsoft Azure 的1024种玩法】六十.通过Azure Virtual Machines快速搭建个人Ghost博客系统
【Microsoft Azure 的1024种玩法】六十一.通过Microsoft Azure Virtual Machines快速搭建消息队列RabbitMQ
【Microsoft Azure 的1024种玩法】六十二.利用Azure Private DNS 实现虚拟网络中域名的管理解析
【Microsoft Azure 的1024种玩法】六十三.通过全局 VNet 对等互连实现同一区域不同网段的虚拟网络实时打通
【Microsoft Azure 的1024种玩法】六十四.通过Azure Pass平台服务应用专用链接与虚拟网络进行全局VNet 对等互连实现数据安全防护
【Microsoft Azure 的1024种玩法】六十五.基于CloudShell在Azure Ubuntu VM中快速安装构建Jenkins应用
【Microsoft Azure 的1024种玩法】六十六.在Jenkins应用中通过Azure VM扩展集代理程式外挂程式执行Azure资源服务创建
【Microsoft Azure 的1024种玩法】六十七.通过Data Migration Assistant 将本地自建SQL Server 数据库脱机迁移至Azure SQL Database
【Microsoft Azure 的1024种玩法】六十八.基于Azure云平台快速搭建Docker容器
【Microsoft Azure 的1024种玩法】六十九.通过SSMS将本地自建SQL Server 数据库脱机迁移至Azure SQL Database
【Microsoft Azure 的1024种玩法】七十.通过Azure Portal将本地自建SQL Server 数据库在线导入至Azure SQL Database中
【Microsoft Azure 的1024种玩法】七十一.基于Azure Virtual Machines快速上手搭建Typecho博客系统
【Microsoft Azure 的1024种玩法】七十二.使用NodeJS编写调用Azure Cognitive Services Translator API代码获取其支持翻译的语言列表
【Microsoft Azure 的1024种玩法】七十三.通过调用Azure Cognitive Services Translator API 快速将单个句子从简体中文翻译为英文

【操作步骤】

一.在VS Code 初始化项目环境

1.使用如下命令初始化创建的项目

npm init -y

具体操作,如下图所示

【Microsoft Azure 的1024种玩法】七十三.通过调用Azure Cognitive Services Translator API 快速将单个句子从简体中文翻译为英文_第1张图片
2.初始化完毕以后,如下图所示,会生成一个名为“package.json"的文件
【Microsoft Azure 的1024种玩法】七十三.通过调用Azure Cognitive Services Translator API 快速将单个句子从简体中文翻译为英文_第2张图片3.新建一个main.js的文件用于配置调用API接口
【Microsoft Azure 的1024种玩法】七十三.通过调用Azure Cognitive Services Translator API 快速将单个句子从简体中文翻译为英文_第3张图片

4.使用npm安装Request依赖库
【Microsoft Azure 的1024种玩法】七十三.通过调用Azure Cognitive Services Translator API 快速将单个句子从简体中文翻译为英文_第4张图片

二.通过Nodejs调用Azure Cognitive Services Translator 的文本翻译API

1.如下所示为Azure Cognitive Services Translator 的文本翻译API请求参数说明

参数 说明
api-version 客户端所请求的 API 的版本。 值必须是 3.0。
to 指定输出文本的语言。 目标语言必须是 translation 范围中包含的支持的语言之一。 例如,若要翻译为德语,请使用 to=de。

2.我们在VScode中编写如下代码来调用Azure Cognitive Services Translator 文本翻译 API,其中请求正文为下图中的 {“Text”:}部分,我们可以在其后面定义我们要转换的文本内容

const request = require('request')
let dataString = '[ { "Text": "我爱你" },{"Text":"欢迎使用Azure翻译"} ]'
let headers = {
    'Ocp-Apim-Subscription-Key': '22db94816ab3433baa6475f515c37eaf',
    'Ocp-Apim-Subscription-Region': 'eastus',
    'Content-Type': 'application/json'
}
let options = {
    url: 'https://api.cognitive.microsofttranslator.com/translate',
    method: 'POST',
    headers: headers,
    body: dataString,
    qs: {
        'api-version': '3.0',
        'to': 'de'
    }
}
const toLanguages = () => {
    request(options, function (error, response, body) {
        console.log(body)
    })
}

// 翻译api实例;qs中的to参数为转换为的语言,api-version必须为3.0;此接口可以自动检测传入语言,不需要指定源语言类型
toLanguages()

【Microsoft Azure 的1024种玩法】七十三.通过调用Azure Cognitive Services Translator API 快速将单个句子从简体中文翻译为英文_第5张图片

3.调用API 代码编写完毕以后,我们通过执行“node+定义的js文件名”的方式来调用Azure Cognitive Services Translator 的API即可获取

node main.js 

在这里插入图片描述

4.如下返回相应的主要内容

[{"detectedLanguage":{"language":"zh-Hans","score":1.0},"translations":[{"text":"I love you","to":"en"}]},
{"detectedLanguage":{"language":"zh-Hans","score":0.86},"translations":[{"text":"Welcome to Azure Translator","to":"en"}]}]

成功的响应是一个 JSON 数组,其中的每个结果对应于输入数组中的一个字符串。 结果对象包括以下属性:

1.detectedLanguage:一个对象,它通过以下属性描述检测到的语言:
1)language:一个字符串,表示检测到的语言的代码。
2)score:一个浮点值,表示结果的置信度。 分数介于 0 和 1 之间,较低的分数表示较低的置信度。

2.translations:翻译结果的数组。 数组的大小与通过 to 查询参数指定的目标语言的数目匹配。 数组中的每个元素包括:

1)to:一个字符串,表示目标语言的语言代码。

2)text:一个字符串,提供翻译的文本。

3.transliteration:一个对象,在 toScript 参数指定的脚本中提供翻译的文本。

1)script:一个字符串,指定目标脚本。

2)text:一个字符串,在目标脚本中提供翻译的文本。

根据如上对API返回的主要内容所述,我们已经成功调用Azure Cognitive Services Translator API实现了将单个句子从简体中文翻译为英文

【Microsoft Azure 的1024种玩法】七十三.通过调用Azure Cognitive Services Translator API 快速将单个句子从简体中文翻译为英文_第6张图片

你可能感兴趣的:(【Microsoft,Azure,的1024种玩法】,azure,microsoft)