-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKoreanToEnglishCode.py
52 lines (47 loc) · 1.51 KB
/
KoreanToEnglishCode.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
42
43
44
45
46
47
48
49
50
51
52
import sys
import re
from googletrans import Translator
def isHangul(text):
#Check the Python Version
pyVer3 = sys.version_info >= (3, 0)
if pyVer3 : # for Ver 3 or later
encText = text
else: # for Ver 2.x
if type(text) is not unicode:
encText = text.decode('utf-8')
else:
encText = text
hanCount = len(re.findall(u'[\u3130-\u318F\uAC00-\uD7A3]+', encText))
return hanCount > 0
if __name__ == '__main__':
translator = Translator()
i = 0
filename = input("Input file name:")
#filename = 'example.cpp'
f = open('./code/'+filename, 'r',encoding='UTF-8')
w_f = open('./code/Translated_'+filename, 'w',encoding='UTF-8')
while True:
line = f.readline()
if not line: break
splitedline=line.split(' ')
while i < len(splitedline):
#print(splitedline[i])
if splitedline[i] == '':
splitedline[i] = ' '
else :
splitedline[i] = splitedline[i]+' '
if isHangul(splitedline[i]) == True:
temp = ''.join(splitedline[i:])
splitedline = ''.join(splitedline[0:i])
tr_results = translator.translate(temp)
splitedline = splitedline + tr_results.text
#print(splitedline)
break
i += 1
i = 0
list2str = ''.join(splitedline)
w_f.write(list2str + '\n')
print(list2str)
f.close()
w_f.close()
print('DONE!')