腾讯翻译君API使用笔记

最近一直用腾讯翻译君翻译部分文字,觉得这个系统挺好用的,感觉准确度不错,但是复制的文字总是每一行认为是一段,于是写了个小脚本来解决。顺便找到了腾讯翻译君现成的Python,用起来很开心。下面记录一下:

1. 去除连字符的小脚本

就是简单的几句,最基本的python。几乎不用注释,建立一个文件,fin.txt,把要翻译的内容放在里边,保存,就可以了。

 # -*- coding: utf-8 -*-
##Used for translate article, to avoid the meaning changing because of enter
#翻译论文时去除换行和行末连字符
def drop_enter(fin):
    with open(fin,) as f: #'r', encoding='UTF-8'
        paragraph = ''
        for line in f:
            if line != '':
                line= line.strip()
                if line[-1] == '-':
                    line = line[:-1]
                else:
                    line += ' '
                paragraph += line
    return paragraph

#paragraph = drop_enter("fin.txt")
#print paragraph

2.使用API提高翻译速度和效率

官方python api的github地址在这,pip安装非常方便和简单。https://github.com/TencentCloud/tencentcloud-sdk-python
登陆腾讯云后,会有一个api的云代码编辑,相当方便,对我这种不熟悉api的人,只填写了几个参数,就获得了代码,还支持多种语言,真心赞一个。https://console.cloud.tencent.com/api/explorer?Product=tmt&Version=2018-03-21&Action=TextTranslate
如果用我的代码,只需一个SecretId 和SecretKey就可以了,也很方便获得,两个值填在括号中两个双引号中即可cred = credential.Credential("", "")。https://console.cloud.tencent.com/cam/capi
然后就可以愉快地翻译小段文字了,少了几次鼠标点击,减少鼠标手的负担,哈哈。我把代码放在了github上。
https://github.com/zd200572/translate-article-tools

发帖时间: IT 归档位置:

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注