-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.py
55 lines (43 loc) · 1.48 KB
/
new.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
import json
import threading
import time
from deep_translator import GoogleTranslator
from googletrans import Translator
# def print_and_update():
# number = 0
# while True:
# print(number)
# number += 1
# time.sleep(1)
#
# # Create a thread to run the function
# thread = threading.Thread(target=print_and_update)
#
# # Start the thread
# thread.start()
class TranslationEntry:
def __init__(self, word, number, translation):
self.word = word
self.number = number
self.translation = translation
def translate_json(json_data):
translator = Translator()
translated_entries = []
wow = 0
for word, number in json_data['word_count'].items():
wow += 1
print(wow)
# print_and_update()
translation = GoogleTranslator(source='en', target='uz').translate(word)
translated_entries.append(TranslationEntry(word, number, translation))
return translated_entries
# Load the JSON file
with open('output.json', 'r', encoding='utf-8') as json_file:
data = json.load(json_file)
# Translate the JSON data and create TranslationEntry instances
translated_entries = translate_json(data)
# Save the translated data to a new JSON file
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)