-
Notifications
You must be signed in to change notification settings - Fork 0
/
all.py
71 lines (52 loc) · 2.08 KB
/
all.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
import json
import re
from deep_translator import GoogleTranslator
from googletrans import Translator
import nltk
# Download the necessary data for nltk if not already downloaded
nltk.download('words')
class WordTranslation:
def __init__(self, word, repeat, translate, is_english):
self.word = word
self.repeat = repeat
self.translate = translate
self.is_english = is_english
def translate_to_uzbek(text):
translator = Translator()
translated = GoogleTranslator(source='en', target='uz').translate(text)
return translated
def is_english_word(word):
english_words = set(nltk.corpus.words.words())
return word in english_words
def process_text_file(input_file):
word_count = {}
with open(input_file, 'r') as file:
for line in file:
# Remove specified characters and convert to lowercase
line = re.sub(r'[,"-><?!]', '', line.lower())
words = line.split()
for word in words:
# Skip empty words
if not word:
continue
if re.search(r'\d', word): # Exclude words with digits
continue
is_english = is_english_word(word)
if word in word_count:
word_count[word]["repeat"] += 1
else:
word_count[word] = {
"repeat": 1,
"translate": translate_to_uzbek(word),
"is_english": is_english
}
translations = []
for word, info in word_count.items():
word_translation = WordTranslation(word, info["repeat"], info["translate"], info["is_english"])
translations.append(word_translation.__dict__)
# Write the translations to a JSON file
with open('word_translations.json', 'w', encoding='utf-8') as json_file:
json.dump(translations, json_file, ensure_ascii=False, indent=4)
if __name__ == "__main__":
input_file = "hello.txt" # Replace with the path to your .txt file
process_text_file(input_file)