截图
源码
Translator.py
#!/usr/bin/python
# -*- coding: UTF-8 -*-
from copy import deepcopy
from distutils.log import Log
from email import utils
import json
import http.client #修改引用的模块
import hashlib
from msilib import Table
from multiprocessing.dummy import Array
from operator import index, truediv
from tokenize import group
from turtle import st #修改引用的模块
from urllib import parse
import random
from Log import Debug
# 百度注册开发者 并创建通用翻译 使用高级翻译app应用接口 获取 appid和secretKey
# 百度开发使用的api接口 翻译的句子会比 游客身份翻译的结果 更准确
appid = '20220829001324165' #你的appid 这里可以先用我的试用一下
secretKey = 'owSrQDeWHGPvI0U1BUm8' #你的密钥
singleTranslteMaxCount = 3 #单个单词翻译失败次数的上限
class WordInformation:
_reqCount = None
_from = None
_to = None
_text = None
_translateText = None
_nextWorld = None
def __init__(self, text:str,fromLanguage:str,toLanguage:str,nextWorld) -> None:
self._reqCount = 0
self._text = text
self._from = fromLanguage
self._to = toLanguage
self._nextWorld = nextWorld
def CanReq(self):
if self._reqCount > singleTranslteMaxCount:
return False
self._reqCount += 1
return True
def GetText(self):
return self._text
def GetTranslateText(self):
if None != self._translateText:
return self._translateText
return self._text
def GetNext(self):
return self._nextWorld
def Translater( worldInfo:WordInformation):
if worldInfo == None:
return
Debug.Log(f"{worldInfo.GetText()} 正在翻译...")
myurl = '/api/trans/vip/translate'
q = worldInfo.GetText()
fromLang = worldInfo._from
toLang = worldInfo._to
salt = random.randint(32768, 65536)
sign = appid+q+str(salt)+secretKey
m1 = hashlib.md5()
m1.update(sign.encode("utf-8"))
sign = m1.hexdigest()
myurl = myurl+'?appid='+appid+'&q='+parse.quote(q)+'&from='+fromLang+'&to='+toLang+'&salt='+str(salt)+'&sign='+sign
httpClient = http.client.HTTPConnection('api.fanyi.baidu.com')
httpClient.request('GET', myurl)
response = httpClient.getresponse()
#转码
html = response.read().decode('utf-8')
html = json.loads(html)
if httpClient:
httpClient.close()
if "trans_result" in html:
dst = html["trans_result"][0]["dst"]
worldInfo._translateText = dst
# Translater(worldInfo.GetNext())
# else:
# if worldInfo.CanReq():
# Translater(worldInfo)
# else:
# Translater(worldInfo.GetNext())
def GetWorldInfoArrByTextArr( texts:Array,fromLanguage:str,toLanguage:str ):
num = len(texts)
worlds = []
for i in range(num-1,0,-1):
if i == num - 1:
world = WordInformation(texts[i],fromLanguage,toLanguage,None)
worlds.append(world)
else:
world = WordInformation(texts[i],fromLanguage,toLanguage,worlds[len(worlds)-1])
worlds.append(world)
return worlds
def Translation( needTranslateTexts:Array,fromLanguage:str,toLanguage:str ):
worlds = GetWorldInfoArrByTextArr(needTranslateTexts,fromLanguage,toLanguage)
Debug.Runtime("翻译用时: ")
# 递推方式 next指针不为none 递归执行next
# Translater(worlds[len(worlds)-1])
# 迭代方式
for i in range(0,len(worlds)):
Translater(worlds[i])
if worlds[i].GetTranslateText() == None and worlds[i].CanReq():
i -= 1
Debug.Runtime("翻译用时: ")
worlds.reverse()
translateTexts = [ ]
for world in worlds:
translateTexts.append(world.GetTranslateText())
return translateTexts,worlds
Log.py
import sys
import time
import traceback
import Utils
DEBUG = True #if sys.gettrace() else False
class Debug:
__log = ''
__time = dict()
@staticmethod
def Log(textContent:str):
'''
输出日志 DEBUG模式下 同时输出编辑器显示
'''
times = time.time()
local_time = time.localtime(times)
tstr = time.strftime("%Y-%m-%d %H:%M:%S",local_time)
str1 = f"{tstr}\t{textContent}\n"
if DEBUG:
print(str1)
Debug.__log += str1
@staticmethod
def LogExcept():
'''
输出堆栈信息 一般用于捕获异常报错后调用
'''
Debug.Log(traceback.format_exc())
@staticmethod
def Runtime(str1):
'''
输出两次打印间程序的运行时间
成双成对的方式出现
第一次调用并不会打印任何信息
仅在第二次调用后 返回与第一调用间的间隔
'''
if(str1 in Debug.__time.keys()):
runtime = time.time() - Debug.__time[str1]
del Debug.__time[str1]
Debug.Log("%s%f秒"%(str1,runtime))
else:
Debug.__time[str1] = time.time()
@staticmethod
def Output():
Utils.writeInFile('./log.txt', Debug.__log)
Utils.py
'''
工具类
'''
import base64
import json # json相关
import os # 文件流相关
import zipfile # zip亚索文件
import shutil # 删除整个文件夹
def fromFile(url):
try:
with open(url, 'r', encoding='utf-8') as fp:
return fp.read()
finally:
fp.close()
def fromFile2Base64(url):
try:
with open(url, 'rb') as f1:
return str(base64.b64encode(f1.read()), encoding='utf-8')
finally:
f1.close()
def writeInFile(toFile, content):
try:
with open(toFile, 'w', encoding='utf-8') as fp:
fp.write(content)
finally:
fp.close()
def fromJsonAsDict(url):
return json.loads((fromFile(url)))
def writeDictInFile(url, dict1):
writeInFile(url, json.dumps(dict1, ensure_ascii=False, indent=2))
def revealInFileExplorer(targetDir):
try:
os.startfile(targetDir)
except:
os.system("explorer.exe %s" % targetDir)
def zipFile(src, dest):
'''
src: 目标文件位置 D:/123.txt
dest: 压缩后输出的zip路径 D:/123.zip
'''
with zipfile.ZipFile(dest, 'w',zipfile.ZIP_DEFLATED) as p:
p.write(src,os.path.split(src)[1])
p.close()
def zipFiles(src,dest):
'''
src: 目标文件夹位置 D:/hellowd
dest: 压缩后输出的zip路径 D:/hellowd.zip
'''
with zipfile.ZipFile(dest, 'w',zipfile.ZIP_DEFLATED) as pZip:
for folder, _, files in os.walk(src):
relative_url = folder.replace(src, '')
for file in files:
pZip.write(os.path.join(folder,file),os.path.join(relative_url,file))
pZip.close()
def removeFile(url):
if os.path.isdir(url):
shutil.rmtree(url)
else:
os.remove(url)
简单的使用案例
# 导入Translation
from Translator import Translation
zhTexts = ["为了解决商家的让利活动我压力很大。","为了解决商家的让利活动我压力很大。","消耗{0}体力","获取{0}钞票" ]
enTexts,enWorlds = Translation(zhTexts,'zh','en')
print(enTexts)
Python版本
python 3.99
可兼容版本 3.x
到此这篇关于Python实现批量翻译的示例代码的文章就介绍到这了,更多相关Python批量翻译内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!