-
Notifications
You must be signed in to change notification settings - Fork 0
/
trasnlate_and_add.py
46 lines (31 loc) · 1.3 KB
/
trasnlate_and_add.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
import json
from deep_translator import GoogleTranslator
import threading
from googletrans import Translator
class TranslationEntry:
def __init__(self, word, number, translation):
self.word = word
self.number = number
self.translation = translation
def translate_word(word, number, translator, results):
translation = GoogleTranslator(source='en', target='uz').translate(word)
entry = TranslationEntry(word, number, translation)
results.append(entry)
def main():
translator = Translator()
print('Translating...')
with open('output.json', 'r', encoding='utf-8') as json_file:
data = json.load(json_file)
translated_entries = []
threads = []
for word, number in data['word_count'].items():
thread = threading.Thread(target=translate_word, args=(word, number, translator, translated_entries))
threads.append(thread)
thread.start()
for thread in threads:
thread.join() # Wait for all threads to finish
output_data = [{'word': entry.word, 'number': entry.number, 'translate': entry.translation} for entry in translated_entries]
with open('output.json', 'w', encoding='utf-8') as json_file:
json.dump(output_data, json_file, ensure_ascii=False, indent=4)
if __name__ == "__main__":
main()