Tencent_机器翻译_文本翻译

这里是引用

# 
import json
import cv2
import requests
import uuid
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException

from tencentcloud.tmt.v20180321 import tmt_client
from tencentcloud.tmt.v20180321 import models as tmt_models

import numpy as np
import base64
import matplotlib.pyplot as plt
%matplotlib inline
import warnings
warnings.filterwarnings("ignore")
SecretId="xxxxxxxxxxxxxxxxxxxxxx"
SecretKey="xxxxxxxxxxxxxxxxxxxxxxxxxxxx"
"""
文本翻译
https://console.cloud.tencent.com/api
/explorer?Product=tmt&Version=2018-03-21&Action=TextTranslate&SignVersion=
"""
try: 
    cred = credential.Credential(SecretId, SecretKey) 
    httpProfile = HttpProfile()
    httpProfile.endpoint = "tmt.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = tmt_client.TmtClient(cred, "ap-beijing", clientProfile) 
    SourceText="""
    
    Long a focal point of science fiction, AI is no longer a curious theme to ponder for the
    distant future – it's here. The best artificial intelligence stocks to buy already use 
    processes like machine learning and neural networks on a daily basis. In fact, if you took
    away AI capabilities from the following companies, their profitability would instantly
    plummet – and five of them are trillion-dollar businesses, underscoring the new technology's 
    importance to 2021's economy. The growth of artificial intelligence is also enviable. 
    Leading market intelligence firm IDC projects the AI industry to surge 16.4% in 2021, 
    reaching a size of $554 billion by 2024.
    """
    req = tmt_models.TextTranslateRequest()
    params = {
        "SourceText": SourceText,
        "Source": "auto",
        "Target": "zh",
        "ProjectId": 0
    }
    req.from_json_string(json.dumps(params))

    resp = client.TextTranslate(req).to_json_string() 
    resp=json.loads(resp)
    print(resp) 

except TencentCloudSDKException as err: 
    print(err) 
{'TargetText': '\n\n长期以来,人工智能一直是科幻小说的焦点,不再是一个值得思考的奇怪主题\n遥远的未来-就在这里。值得买入的最好的人工智能股票已经在使用\n像机器学习和神经网络这样的日常过程。事实上,如果你把\n离开以下公司的人工智能能力,他们的盈利能力将立即\n暴跌-其中五项是万亿美元的业务,突显了这项新技术的\n对2021年经济的重要性。人工智能的增长也令人羡慕。\n领先的市场情报公司IDC预计,人工智能行业在2021年将激增16.4%,\n到2024年达到5540亿美元的规模。\n', 'Source': 'en', 'Target': 'zh', 'RequestId': '3724fa34-a46b-4cad-9fb5-9d266759747a'}
"""
Action	是	String	公共参数,本接口取值:TextTranslate。
Version	是	String	公共参数,本接口取值:2018-03-21。
Region	是	String	公共参数,详见产品支持的 地域列表。
SourceText	是	String	待翻译的文本,文本统一使用utf-8格式编码,
非utf-8格式编码字符会翻译失败,请传入有效文本,html标记等非常规翻译文本可能会翻译失败
。单次请求的文本长度需要低于2000。
Source	是	String	源语言,支持:
auto:自动识别(识别为一种语言)
zh:简体中文
zh-TW:繁体中文
en:英语
ja:日语
ko:韩语
fr:法语
es:西班牙语
it:意大利语
de:德语
tr:土耳其语
ru:俄语
pt:葡萄牙语
vi:越南语
id:印尼语
th:泰语
ms:马来西亚语
ar:阿拉伯语
hi:印地语
Target	是	String	目标语言,各源语言的目标语言支持列表如下

zh(简体中文):en(英语)、ja(日语)、ko(韩语)、fr(法语)、es(西班牙语)、it(意大利语)、de(德语)、tr(土耳其语)、ru(俄语)、pt(葡萄牙语)、vi(越南语)、id(印尼语)、th(泰语)、ms(马来语)
zh-TW(繁体中文):en(英语)、ja(日语)、ko(韩语)、fr(法语)、es(西班牙语)、it(意大利语)、de(德语)、tr(土耳其语)、ru(俄语)、pt(葡萄牙语)、vi(越南语)、id(印尼语)、th(泰语)、ms(马来语)
en(英语):zh(中文)、ja(日语)、ko(韩语)、fr(法语)、es(西班牙语)、it(意大利语)、de(德语)、tr(土耳其语)、ru(俄语)、pt(葡萄牙语)、vi(越南语)、id(印尼语)、th(泰语)、ms(马来语)、ar(阿拉伯语)、hi(印地语)
ja(日语):zh(中文)、en(英语)、ko(韩语)
ko(韩语):zh(中文)、en(英语)、ja(日语)
fr(法语):zh(中文)、en(英语)、es(西班牙语)、it(意大利语)、de(德语)、tr(土耳其语)、ru(俄语)、pt(葡萄牙语)
es(西班牙语):zh(中文)、en(英语)、fr(法语)、it(意大利语)、de(德语)、tr(土耳其语)、ru(俄语)、pt(葡萄牙语)
it(意大利语):zh(中文)、en(英语)、fr(法语)、es(西班牙语)、de(德语)、tr(土耳其语)、ru(俄语)、pt(葡萄牙语)
de(德语):zh(中文)、en(英语)、fr(法语)、es(西班牙语)、it(意大利语)、tr(土耳其语)、ru(俄语)、pt(葡萄牙语)
tr(土耳其语):zh(中文)、en(英语)、fr(法语)、es(西班牙语)、it(意大利语)、de(德语)、ru(俄语)、pt(葡萄牙语)
ru(俄语):zh(中文)、en(英语)、fr(法语)、es(西班牙语)、it(意大利语)、de(德语)、tr(土耳其语)、pt(葡萄牙语)
pt(葡萄牙语):zh(中文)、en(英语)、fr(法语)、es(西班牙语)、it(意大利语)、de(德语)、tr(土耳其语)、ru(俄语)
vi(越南语):zh(中文)、en(英语)
id(印尼语):zh(中文)、en(英语)
th(泰语):zh(中文)、en(英语)
ms(马来语):zh(中文)、en(英语)
ar(阿拉伯语):en(英语)
hi(印地语):en(英语)
ProjectId	是	Integer	项目ID,可以根据控制台-账号中心-项目管理中的配置填写,如无配置请填写默认项目ID:0
UntranslatedText	否	String	用来标记不希望被翻译的文本内容,如句子中的特殊符号、人名、地名等;每次请求只支持配置一个
不被翻译的单词;仅支持配置人名、地名等名词,不要配置动词或短语,否则会影响翻译结果。
"""
"""
https://console.cloud.tencent.com/api
/explorer?Product=tmt&Version=2018-03-21&Action=TextTranslateBatch&SignVersion=
批量文本翻译
"""

try: 
    cred = credential.Credential(SecretId, SecretKey) 
    httpProfile = HttpProfile()
    httpProfile.endpoint = "tmt.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = tmt_client.TmtClient(cred, "ap-beijing", clientProfile) 

    req = tmt_models.TextTranslateBatchRequest()
    SourceTextList=["""
    
    Long a focal point of science fiction, AI is no longer a curious theme to ponder for the
    distant future – it's here. The best artificial intelligence stocks to buy already use 
    processes like machine learning and neural networks on a daily basis. In fact, if you took
    away AI capabilities from the following companies, their profitability would instantly
    plummet – and five of them are trillion-dollar businesses, underscoring the new technology's 
    importance to 2021's economy. The growth of artificial intelligence is also enviable. 
    Leading market intelligence firm IDC projects the AI industry to surge 16.4% in 2021, 
    reaching a size of $554 billion by 2024.
    ""","""
    "It is in writing that, unless they give themselves in, the Taliban will arrest and prosecute, 
    interrogate and punish family members on behalf of those individuals."
    He warned that anyone on the Taliban's blacklist was in severe danger, 
    and that there could be mass executions.
    Foreign powers are continuing efforts to get their nationals out of Afghanistan. 
    A Nato official said on Friday that more than 18,000 people have been evacuated in the last five days from Kabul airport.
    Some 6,000 more, among them former interpreters for foreign armed forces, 
    are on standby to be flown out late on Thursday or early Friday.
    
    """]
    
    
    params = {
        "Source": "auto",
        "Target": "zh",
        "ProjectId": 0,
        "SourceTextList": SourceTextList
    }
    req.from_json_string(json.dumps(params))

    resp = client.TextTranslateBatch(req).to_json_string() 
    resp=json.loads(resp)
    print(resp)  


except TencentCloudSDKException as err: 
    print(err) 
{'Source': 'en', 'Target': 'zh', 'TargetTextList': ['\n\n长期以来,人工智能一直是科幻小说的焦点,不再是一个值得思考的奇怪主题\n遥远的未来-就在这里。值得买入的最好的人工智能股票已经在使用\n像机器学习和神经网络这样的日常过程。事实上,如果你把\n离开以下公司的人工智能能力,他们的盈利能力将立即\n暴跌-其中五项是万亿美元的业务,突显了这项新技术的\n对2021年经济的重要性。人工智能的增长也令人羡慕。\n领先的市场情报公司IDC预计,人工智能行业在2021年将激增16.4%,\n到2024年达到5540亿美元的规模。\n', '\n“书面规定,除非他们投降,否则塔利班将逮捕并起诉,\n代表这些人审问和惩罚家庭成员。“\n他警告说,塔利班黑名单上的任何人都处于严重危险之中,\n可能会有大规模的处决。\n外国势力正在继续努力让他们的国民离开阿富汗。\n一名北约官员周五表示,在过去的五天里,已有超过1.8万人从喀布尔机场撤离。\n大约6000多人,其中包括前外国武装部队的翻译,\n都处于待命状态,将在周四晚些时候或周五早些时候起飞。\n\n'], 'RequestId': '94bf5898-d370-431b-b601-a666295496aa'}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值