Skip to content

Instantly share code, notes, and snippets.

@jiaxianhua
Created April 4, 2019 01:16
Show Gist options
  • Save jiaxianhua/9efb41a8f85a77ffbd21953854c02673 to your computer and use it in GitHub Desktop.
Save jiaxianhua/9efb41a8f85a77ffbd21953854c02673 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import os
import time
from googletrans import Translator
translator = Translator()
def translate(src):
time.sleep(5 )
dest = translator.translate(src, src='en', dest='zh-CN')
return dest.text
def open_md(filename):
md_list = []
SPLIT_LENGTH = 10
with open(filename, mode='r') as md_file:
md_list = md_file.readlines()
md_list = [''.join(md_list[i: i+SPLIT_LENGTH]) for i in range(0, len(md_list), SPLIT_LENGTH)]
return md_list
def transform_md(md_list):
transform_list = []
i = 0
for md in md_list:
dest = translate(md.rstrip('\\'))
transform_list.append(dest)
i += 1
print(f'{i}/{len(md_list)}')
return transform_list
def save_md(filename, transform_list):
with open(filename, mode='w') as md_file:
md_file.write('\n\n'.join(transform_list))
def read_mds(dir_path):
md_file_list = []
for root, dirs, files in os.walk(dir_path):
for name in filter(lambda filename: os.path.splitext(filename)[1] == '.md' , files):
md = os.path.join(root, name)
md_file_list.append(md)
return md_file_list
if __name__ == '__main__':
md_file_list = read_mds('.')
for md_file in md_file_list:
print(md_file)
md_list = open_md(md_file)
transform_list = transform_md(md_list)
save_md(md_file, transform_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment