-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
72 lines (62 loc) · 2.4 KB
/
translator.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, json, collections, time
from pprint import pprint
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
import config
chromedriver = config.chromedriver_path
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
file = open(config.out_path, "w")
cfg_nb = {"nb_words": 0, "nb_left": 0}
def printNumberOfWords(obj):
for key, value in obj.iteritems():
if isinstance(value, dict):
printNumberOfWords(value)
else:
cfg_nb['nb_words'] += 1
def introspect(obj, nb_tabs, file):
for idx, (key, value) in enumerate(obj.iteritems()):
# s = '\t"%s": ' % key
s = '%s"%s": ' % (nb_tabs * '\t', key.encode('utf-8'))
if isinstance(value, dict):
s += '{\n'
nb_tabs = nb_tabs + 1
file.write(s)
introspect(value, nb_tabs, file)
nb_tabs = nb_tabs - 1
if idx == len(obj) - 1:
file.write('%s}\n' % (nb_tabs * '\t'))
else:
file.write('%s},\n' % (nb_tabs * '\t'))
else:
utf8Value = value.encode('utf-8')
driver.get("https://translate.google.fr/#%s/%s/%s" % (config.in_language_code, config.out_language_code, utf8Value))
time.sleep(1)
result = driver.find_element_by_id('result_box').text.encode('utf-8')
if utf8Value[0].istitle():
result = result[0].upper() + result[1:]
cfg_nb['nb_left'] -= 1
print "%s -> %s" % (utf8Value, result)
print "%s words remaining..." % cfg_nb['nb_left']
if idx == len(obj) - 1:
s += '"%s"\n' % result.replace('"', '\\"')
else:
s += '"%s",\n' % result.replace('"', '\\"')
file.write(s)
# else:
# print value.encode('utf-8')
with open(config.in_path) as data_file:
# with open('en.json') as data_file:
file.write('{\n')
data = json.load(data_file, object_pairs_hook=collections.OrderedDict)
printNumberOfWords(data)
print "Number of words to translate: %s" % cfg_nb['nb_words']
cfg_nb['nb_left'] = cfg_nb['nb_words']
introspect(data, 1, file)
file.write('}\n')
driver.close()
file.close()