-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEng_chi_text_file.py
41 lines (30 loc) · 1.02 KB
/
Eng_chi_text_file.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
41
import jieba
import itertools as it
import translators as ts
print("Please input name of text file:")
text_file = input()
def permutechisen(s, char):
if not s:
return [s]
binary = it.product(['', char], repeat=len(s) - 1)
zipped = (it.zip_longest(s, comb, fillvalue='') for comb in binary)
return [''.join(it.chain.from_iterable(x)) for x in zipped]
def trans(list1):
mylist = []
for i in range(len(list1)):
chi_eng_google = ts.google(list1[i], 'zh-CN', 'en')
mylist.append(chi_eng_google)
result = str(" ".join(mylist)) + '\n'
return result
def permutechieng(seg_list):
for i in range(len(permutechisen(seg_list, ' '))):
fa.write(trans(permutechisen(seg_list, ' ')[i].split()))
fr = open(text_file, 'r')
fa = open('generate.txt', 'a')
for line in fr:
eng_chi_google = ts.google(line, 'en', 'zh-CN')
print(eng_chi_google)
seg_sentence = list(jieba.cut(eng_chi_google, cut_all=False))
permutechieng(seg_sentence)
fa.write('\n')
fa.write('\n')