用google translate大文件.

问题:

google translate对于大文件不支持,咋办?

 

思路:自己写个函数把的文件拆成小文件,再用google translate!

 

code:

 

from googletrans import Translator
import sys
translator = Translator()
fname=sys.argv[1]
foname=fname + '-cn.txt'
cont=open(fname).read()
fdes = open(foname,'w')
N=5000
i=0
L=len(cont)
while i<L:
    print (1.0*i/L)
    i2 = min(i+N, L)
    subCont = cont[i:i2]
    s=(translator.translate(subCont, src='en', dest='zh-cn'))
    try:
        fdes.write(str(s.text))
    except:
        pass
    i = i+N
fdes.close()
    

 

Powered by Jekyll and Theme by solid

本站总访问量