代码拉取完成,页面将自动刷新
同步操作将从 diyanqi/GoogleTranslateMakeGrass 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
# mg = makeGrass
from googletrans import Translator
import os
import re
import sys
from pdfdocx import read_pdf
trans_dir = "D:\\Trans\\"
txt_dir = "D:\\Trans\\dai"
def cn2en(source):
translator = Translator(service_urls=['translate.google.cn'])
#print(source)
text = translator.translate(source,src='zh-cn',dest='en').text
return text
def en2cn(source):
translator = Translator(service_urls=['translate.google.cn'])
#print(source)
if source:
text = translator.translate(source,src='en',dest='zh-cn')
if text:
return text.text
def makeGrass(text,times=1):
for i in range(times):
temp = en2cn(text)
#print(temp)
if temp:
return temp
def listdir_rel(path, list_name): #传入存储的list
for file in os.listdir(path):
file_path = file
if os.path.isdir(file_path):
print(file_path)
else:
list_name.append(file_path)
def listdir(path, list_name): #传入存储的list
for file in os.listdir(path):
file_path = os.path.join(path, file)
if os.path.isdir(file_path):
print(file_path)
else:
list_name.append(file_path)
def is_ascii(s):
return all(ord(c) < 128 for c in s)
txt_list = []
listdir_rel(txt_dir, txt_list)
for i in txt_list:
if (i.endswith(".pdf")):
p_text = read_pdf(txt_dir + "\\" +i)
out = open(trans_dir + "\\" +i.replace(".pdf", ".txt"), 'w', encoding='utf-8')
out.write(p_text)
out.close()
file_list = []
listdir(trans_dir, file_list)
for i in file_list:
print(i)
out_file = open(i+"_cn.txt", "w", encoding='UTF-8')
in_file = open(i, "r", encoding='UTF-8')
cnt = 0
for strn in in_file.readlines():
## print("cnt" +str(cnt))
## cnt = cnt + 1
## print("len:" +str(len(strn)))
if len(strn.strip()) > 5 and is_ascii(strn):
if '.' in strn:
text = makeGrass(strn.strip().replace(".", ""))
else:
text = makeGrass(strn.strip())
else:
text = None
if text:
out_file.write(text)
out_file.write("\n")
in_file.close()
out_file.close()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。