Skip to content

Instantly share code, notes, and snippets.

@siuoly
Created April 26, 2022 23:06
Show Gist options
  • Save siuoly/190c51e60624c7ccbc037f5d9443b790 to your computer and use it in GitHub Desktop.
Save siuoly/190c51e60624c7ccbc037f5d9443b790 to your computer and use it in GitHub Desktop.
Translate the paper txt, needing preprocessing that pdf to word fileformat.
#!/bin/python
###### 英文文本:source.txt , 中文文本:target.txt 逐行對照,放入translated.txt
def main():
sFile = "./source.txt"
tFile = "./target.txt"
with open( sFile, 'r') as f:
source = list( f.readlines() )
source = [ i for i in source if i.strip()]
with open( tFile, 'r') as f:
target = list( f.readlines() )
target = [ i for i in target if i.strip()]
oFile = "translated.txt"
with open( oFile , 'w') as f:
for s,t in zip( source,target):
f.write( f"{s.strip()} ({t.strip()})\n\n " )
main()
#########################################################
def main2():
from sys import argv
from googletrans import Translator
t = Translator()
def translate(text):
return t.translate( text, dest="zh-TW" ).text
readfile = "./paper.txt"
writefile = "./translated.txt"
with open( readfile, 'r') as f:
lines = f.readlines()
import time
newfile = open( writefile , 'w')
for line in lines:
if line.strip() == '':
newfile.write('\n')
continue
time.sleep(3)
line = line[:-1] + '(' + translate(line) + ')\n\n'
newfile.write( line )
print( line )
newfile.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment