51 lines
2.1 KiB
Python
51 lines
2.1 KiB
Python
import json
|
||
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, models
|
||
|
||
def tenTrans(SourceText='',Source='zh',Target=''):
|
||
try:
|
||
# 实例化一个认证对象,入参需要传入腾讯云账户secretId,secretKey,此处还需注意密钥对的保密
|
||
# 密钥可前往https://console.cloud.tencent.com/cam/capi网站进行获取
|
||
cred = credential.Credential("AKIDZ1CFLH3dz8HJ7jicO2TVRAgFua7RXQ6m", "rVza2GMMExW2k5JSaZJyY3QrBLRJyQfu")
|
||
# 实例化一个http选项,可选的,没有特殊需求可以跳过
|
||
httpProfile = HttpProfile()
|
||
httpProfile.endpoint = "tmt.tencentcloudapi.com"
|
||
|
||
# 实例化一个client选项,可选的,没有特殊需求可以跳过
|
||
clientProfile = ClientProfile()
|
||
clientProfile.httpProfile = httpProfile
|
||
# 实例化要请求产品的client对象,clientProfile是可选的
|
||
client = tmt_client.TmtClient(cred, "ap-beijing", clientProfile)
|
||
|
||
# 实例化一个请求对象,每个接口都会对应一个request对象
|
||
req = models.TextTranslateRequest()
|
||
params = {
|
||
"SourceText": SourceText,
|
||
"Source": Source,
|
||
"Target": Target,
|
||
"ProjectId": 1314553376
|
||
}
|
||
req.from_json_string(json.dumps(params))
|
||
|
||
# 返回的resp是一个TextTranslateResponse的实例,与请求对象对应
|
||
resp = client.TextTranslate(req)
|
||
# 输出json格式的字符串回包
|
||
print(resp.to_json_string())
|
||
print(resp.TargetText)
|
||
return(resp.TargetText)
|
||
|
||
|
||
# {
|
||
# "Response": {
|
||
# "RequestId": "6c9f968b-4149-4256-a567-8af75f28e180",
|
||
# "Source": "zh",
|
||
# "Target": "en",
|
||
# "TargetText": "Hello"
|
||
# }
|
||
# }
|
||
|
||
except TencentCloudSDKException as err:
|
||
print(err) |