翻译go项目代码英文注释


#encoding=UTF-8
#!/usr/bin/python
# encoding: utf-8
#filename: baidu-translate-gofile.py
#author: gaohaixiang
#writetime:202112012055

# 注释
# 需要填写自己百度翻译的 appid 和 secretKey

import http.client
import hashlib
import json
import urllib
import random
import re
import time
import os

# 单次翻译大小为6000字节以内
def baidu_translate(content,filename):
    # 存放翻译错误的文件
    ff = open("翻译错误文件.txt","a+",encoding="UTF8")
    #print(content,"翻译函数内部打印content")
    appid = '**************'    # 需要填写自己的百度翻译的 appid
    secretKey = '**************'    # 需要填写自己的百度翻译的 secretKey
    httpClient = None
    myurl = '/api/trans/vip/translate'
    q = content
    fromLang = 'en'  # 源语言
    toLang = 'zh'  # 翻译后的语言
    salt = random.randint(32768, 65536)
    sign = appid + q + str(salt) + secretKey
    sign = hashlib.md5(sign.encode()).hexdigest()
    myurl = myurl + '?appid=' + appid + '&q=' + urllib.parse.quote(
        q) + '&from=' + fromLang + '&to=' + toLang + '&salt=' + str(
        salt) + '&sign=' + sign
    #print(myurl,"翻译函数内部打印myurl")

    try:
        httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
        httpClient.request('GET', myurl)
        # response是HTTPResponse对象
        response = httpClient.getresponse()
        jsonResponse = response.read().decode("utf-8")  # 获得返回的结果,结果为json格式
        js = json.loads(jsonResponse)  # 将json格式的结果转换字典结构
        #print(js,"翻译函数内部打印全部输出数据")
        dst = str(js["trans_result"][0]["dst"])  # 取得翻译后的文本结果
        #print(dst,"函数内部打印dst")  # 打印结果
        return dst
    except Exception as e:
        print(e,"内部错误打印e")
        ff.writelines(filename)
        ff.writelines("\n")
    finally:
        if httpClient:
            httpClient.close()
    ff.close()

def openfile(fileinput,newfile,filename):
    list = []  ## 空列表
    # count=1
    fl = open(fileinput,"r",encoding="UTF8")
    lines = fl.readlines()
    fl.close()

    ff = open(newfile,"w",encoding="UTF8")

# with open(fileinput, "r",encoding="UTF8") as fl:
    for line in lines:
        if line.startswith("//") or re.findall("//",line):
            #print(str(line))
            dst = baidu_translate(line.split("//")[1],filename)
            # list.append(dst)
            time.sleep(1.1)  # 不要频繁访问百度翻译
            # ff.writelines(line)
            # # ff.writelines(line.split("//")[0]+dst+"\n")
            translateString = "//" + " " + str(dst)
            #ff.writelines(str(line.split("//")[0]) + translateString + "\n")

            if str(dst) == "err:":
                ff.writelines(lines)
            else:
                ff.writelines(str(line.split("//")[0]) + translateString + "\n")


        else:
            #print(line)
            ff.writelines(line)

    ff.close()

def ListFile(dirpath):
    ff = open("文件夹及文件名称","w",encoding="UTF8")
    for dirpath, dirnames, filenames in os.walk(dirpath):
        #print('文件路径', dirpath)
        #print('文件夹名字', dirnames)
        ff.writelines(dirpath+"\\"+"\n")
        for filename in filenames:
            #print('文件名', filename)
            ff.writelines(dirpath+"\\"+filename+"\n")
    ff.close()

def ReadFile():
    file = open("所有go文件.txt","w",encoding="UTF8")
    ff = open("文件夹及文件名称", "r", encoding="UTF8")
    ffs = ff.readlines()
    for lists in ffs:
        #print(lists)
        if lists.endswith("\.go") or re.findall("\.go",lists):
            #print(lists)
            file.writelines(lists)
    ff.close()
    file.close()

def ReadDate(pathfile):
    ff = open(pathfile,"r",encoding="UTF8")
    ffs = ff.readlines()
    for lists in ffs:
        print(lists)
    ff.close()

def handleLines(filename):
    # 读取文件,并将文件的所有注释均放置于变量中
    ff = open(filename,"r",encoding="UTF8")
    ffs = ff.readlines()
    ff.close()
    a = 0
    traselateLine = """"""
    traselateLineList = []
    for liness in ffs:
        lines = str(liness)
        a = a+1
        if lines.startswith("//"):
            line = "***%s***" % a + lines.strip("\n") + " ABCDEFG "
            traselateLine = traselateLine + line
        elif re.findall("//",lines):
            line = "***%s***" % a + "//  "+lines.split("//")[1].strip("\n") + " ABCDEFG "
            traselateLine = traselateLine + line
        else:
            pass
            #print("没有注释")

        if len(traselateLine) > 5000:
            traselateLineList.append(traselateLine)
            traselateLine = """"""

        if a == len(ffs):
            traselateLineList.append(traselateLine)
    return traselateLineList

def lineDataAppend(traselateLineList,filename):
    # 将每行的数据存放到裂变
    traselateLineListTotal = [] #存放总翻译后的每行的数据
    for traselateLineLists in traselateLineList:
        dst = ''
        # 若被翻译的行为空,则跳过
        if len(traselateLineLists) > 1:
            dst = baidu_translate(traselateLineLists,filename)
        else:
            pass
        #print(dst)
        traselateLaterLinesList = str(dst).split("ABCDEFG")
        print(len(traselateLaterLinesList))
        time.sleep(1.1)
        for linesLines in traselateLaterLinesList:
            traselateLineListTotal.append(linesLines)

    print(len(traselateLineListTotal))
    #print(traselateLineListTotal)
    return traselateLineListTotal

def fileDataWrite(filename,traselateLineListTotal):
    # 打开文件,读取相应的位置,然后匹配到 traselateLineListTotal 的数据,
    # 然后再写入该文件来完成整个的翻译
    ff = open(filename,"r",encoding="UTF8")
    ffs = ff.readlines()
    ff.close()
    file = open(filename,"w",encoding="UTF8")
    a = 0
    for liness in ffs:
        lines = str(liness)
        a = a+1
        if lines.startswith("//") or re.findall("//", lines):
            for traselateLineLines in traselateLineListTotal:
                traselateLineLineLists = traselateLineLines.split("***")
                if len(traselateLineLineLists) > 2:
                    #print(traselateLineLineLists)
                    traselateLineLineNumber = traselateLineLineLists[1]
                    traselateLineLineListss = traselateLineLineLists[2].split("//")
                    if len(traselateLineLineListss) > 1:
                        if str(a) == traselateLineLineNumber:
                            print(traselateLineLineNumber, "数字比较", a)
                            translateString = "//" + " " + traselateLineLineListss[1]
                            file.writelines(str(lines.split("//")[0]) + translateString + "\n")
                    else:
                        if str(a) == traselateLineLineNumber:
                            print(traselateLineLineNumber, "数字比较", a)
                            translateString = "//" + " " + traselateLineLineListss[0]
                            file.writelines(str(lines.split("//")[0]) + translateString + "\n")
        else:
            #print(lines)
            file.writelines(lines)
    file.close()

if __name__ == '__main__':
    statrttime = time.time()


    # file = "kubelet.go"
    # openfile(file,file)  # 输入文件路径

    # 要翻译的文件所处的目录
    dirpath = "D:\pycharmFile\json-iterator"
    #查找所有的文件
    ListFile(dirpath)
    #查找所有的go文件
    ReadFile()



    # # 文档翻译处理
    ff = open("所有go文件.txt","r",encoding="UTF8")
    ffs = ff.readlines()
    ff.close()
    # 存放已翻译的文件
    file = open("已翻译文件.txt","w",encoding="UTF8")
    for filenames in ffs:
        print(filenames)
        file.writelines(filenames)
        filename = filenames.strip("\n")
        #filename = "kubelet.go"
        # 读取文件,并将文件的所有注释均放置于变量中
        traselateLineList = handleLines(filename)
        # 将每行的数据存放到列表
        traselateLineListTotal = lineDataAppend(traselateLineList,filename)
        # 打开文件,读取相应的位置,然后匹配到 traselateLineListTotal 的数据,
        fileDataWrite(filename, traselateLineListTotal)
    file.close()

    endtime = time.time()
    print(endtime - statrttime)















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值