-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
41 lines (33 loc) · 1.37 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# coding=utf-8
from baiduTransAPI import *
from sys import argv
from os.path import exists
import time
import unicodedata
def formateDajia(sentence):
sentence = unicodedata.normalize('NFKC', sentence)
sentence = sentence.replace("。"," ")
sentence = sentence.replace(",", " ")
return sentence.strip()
if __name__ == '__main__':
script, from_file, to_file = argv
print(f"Reading from {from_file} the result will be saved to {to_file}")
if exists(to_file):
print(f"The file {to_file} already exits, do you want to overwrite?")
print("Ready, hit RETURN to overwrite, CTRL-C to abort.")
input()
appid = '20190504000293991' # 填写你的appid
secretKey = 'OA6n_6a40XvPCojHHdht' # 填写你的密钥
baidu = BaiduTranslate(appid,secretKey)
with open(to_file,'w') as file_translated:
with open(from_file) as f:
read_data = f.read()
lines = read_data.split('\n\n')
for line in lines:
qs = line.split("\n")
if(len(qs)>3):
qs[2] = qs[2] + " " + qs[3]
time.sleep(1)#you can change this if you can use higher version
translated_q = baidu.translate(qs[2])
formated_q = formateDajia(translated_q)
file_translated.write(qs[0]+'\n'+qs[1]+'\n'+formated_q+'\n'+qs[2]+'\n\n')