文本机翻算法

jhx是判定该行文本需要翻译

solveline是翻译前文本处理

lsum是翻译结构,如下:

[0,脚本,’’]
[1,原文,’’]
[2,原文,译文]

0是不需要翻译,1是等待翻译,2是翻译完成。

翻译时出现某些未被翻译的句子,则需要重新遍历找出并翻译。

lsum = []
for line in readdiralllines(src,srccoding):
    if jhx(line):
        lsum.append([1,solveline(line),''])
    else:
        lsum.append([0,solveline(line),''])
def translines(lsum,maxtext,translate,tst=0):
    # completed

百度翻译模板:

    def translate(valueText, target, sourceLang = "jp"):
        appId = myappId
        key = mykey
        salt = datetime.datetime.now().strftime('%m%d%H%M%S')
        quoteStr = parse.quote(valueText)
        md5 = _md5.md5()
        md5.update((appId + valueText + salt + key).encode())
        sign = md5.hexdigest()
        url = 'http://fanyi-api.baidu.com/api/trans/vip/translate?q=' + quoteStr + \
              '&from=' + sourceLang + '&to=' + target + '&appid=' + \
              appId + '&salt=' + salt + '&sign=' + sign

        # print(url)
        try:
            resultPage = request.urlopen(url)  # 调用百度翻译API进行批量翻译
        except HTTPError as e:
            print('The server couldn\'t fulfill the request.')
            print('Error code: ', e.code)
            return valueText
        except URLError as e:
            print('We failed to reach a server.')
            print('Reason: ', e.reason)
            return valueText
        except Exception as e:
            print('translate error.')
            print(e)
            return valueText

        # 取得翻译的结果,翻译的结果是json格式
        resultJason = resultPage.read().decode('utf-8')
        # print(resultJason)
        js = None
        try:
            # 将json格式的结果转换成Python的字典结构
            js = json.loads(resultJason)
            # print(js)
        except Exception as e:
            print('loads Json error.')
            print(e)
            return valueText

        key = u"trans_result"

        if key in js:
            dst = ""
            for line in js["trans_result"]:
                dst = dst + line["dst"] + "\n"  # 取得翻译后的文本结果
                # print(dst)
            return dst
        else:
            return valueText  # 如果翻译出错,则输出原来的文本

彩云翻译模板

    def singleTrans(source,token):
        url = "http://api.interpreter.caiyunai.com/v1/translator"

        payload = {
            "source": source,
            "trans_type": "auto2zh",
            "request_id": "demo",
            "detect": True,
        }

        headers = {
            'content-type': "application/json",
            'x-authorization': "token " + token,
        }

        # response = requests.request("POST", url, data=json.dumps(payload), headers=headers)
        response = requests.request("POST", url, json=payload, headers=headers)

        return json.loads(response.text)['target']
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值