赞
踩
价格和 API 申请参考:
测试方式:
Translator
类,这样就能复用 python 库 translate
的测试代码。Translator(from_lang="en",to_lang="zh").translate(sentence)
的形式。本篇为第一篇,只包含第三方库 translate 和 腾讯 API 的测评。
下一篇:【笔记】Python3|(二)用 Python 翻译文本的代码与测试结果(有道翻译 API 篇)
这个第三方库用的是 DeepL 的 API。
亲测请求了 10 次就用不了了。。。蚌埠住。
pip install --upgrade translate
from translate import Translator # pip install translate # 极其拉胯难用,因为有次数限制
# from tencent import Translator
####################################################################################################
#---------------------------------------- Translator ---------------------------------------------#
####################################################################################################
def isContainAlpha(str_check):
return str_check.lower() != str_check.upper()
def test_simple_translate():
print(Translator(from_lang="English",to_lang="Chinese").translate('Hello, world!'))
就翻译个这玩意要 2.89s
因为它翻译 3 次就挂了。
参考:
腾讯翻译API接口免费翻译额度:500万字符免费/每月;
超出免费额度价格:58元/百万字符,有字符资源包出售;
pip install --upgrade tencentcloud-sdk-python
需要运行的测试代码就不重复贴了,这是 Translator 类定义的代码:
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 SecretId = "" SecretKey = "" class Translator: def __init__(self, from_lang, to_lang): self.from_lang = from_lang self.to_lang = to_lang def translate(self, text): 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 = models.TextTranslateRequest() req.SourceText = text req.Source = self.from_lang req.Target = self.to_lang req.ProjectId = 0 resp = client.TextTranslate(req) return resp.TargetText except TencentCloudSDKException as err: return err if __name__ == '__main__': translator = Translator(from_lang="en", to_lang="zh") print(translator.translate("Hello, world!"))
输出打印时间:1.3s
比如下图的 fixtures,不过其他的常见词汇翻译挺好的,算是能用能看懂了!
翻译的文档的具体信息是通过查看 VScode 右下角得到的。
后续其他篇目敬请期待,若有更新会在本文结尾附上链接。
下一篇:【笔记】Python3|(二)用 Python 翻译文本的代码与测试结果(有道翻译 API 篇)
本账号所有文章均为原创,欢迎转载,请注明文章出处:https://blog.csdn.net/qq_46106285/article/details/138141104。百度和各类采集站皆不可信,搜索请谨慎鉴别。技术类文章一般都有时效性,本人习惯不定期对自己的博文进行修正和更新,因此请访问出处以查看本文的最新版本。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。