-
Notifications
You must be signed in to change notification settings - Fork 0
/
translator.py
155 lines (127 loc) · 5.7 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import os
import io
import pygame
import textwrap
import threading
from tkinter import *
from tkinter.ttk import *
from tkinter import ttk
from gtts import gTTS
from googletrans import Translator
from PyDictionary import PyDictionary
class Translator_App(object):
def __init__(self,master):
frame = Frame(master)
frame.grid()
tabControl = ttk.Notebook(root)
tabControl.configure(width=900, height=900)
self.translate_tab = ttk.Frame(tabControl)
tabControl.add(self.translate_tab, text= "Word Dictionary with Pronounciation")
tabControl.grid()
self.translate_tab.grid_propagate(0)
self.speak_it = BooleanVar()
self.languages = {
'Arabic': 'ar', 'Belarusian': 'be',
'Bulgarian': 'bg', 'Bosnian': 'bs',
'Czech': 'cs', 'Danish': 'da',
'German': 'de', 'Greek': 'el',
'English': 'en', 'Spanish': 'es',
'Persian': 'fa', 'Finnish': 'fi',
'French': 'fr', 'Irish': 'ga',
'Hebrew': 'he', 'Hindi': 'hi',
'Croatian': 'hr', 'Haitian': 'ht',
'Hungarian': 'hu', 'Armenian': 'hy',
'Indonesian': 'id', 'Italian': 'it',
'Japanese': 'ja', 'Korean': 'ko',
'Kurdish': 'ku', 'Latin': 'la',
'Lithuanian': 'lt', 'Latvian': 'lv',
'Dutch': 'nl', 'Norwegian': 'no',
'Polish': 'pl', 'Portuguese': 'pt',
'Romanian': 'ro', 'Russian': 'ru',
'Somalia': 'so', 'Albanian': 'sq',
'Serbian': 'sr', 'Swedish': 'sv',
'Swahili': 'sw', 'Turkish': 'tr',
'Vietnamese': 'vi'}
self.translate_page()
self.about_page()
def translate_page(self):
self.top_label = Label(self.translate_tab, text="Enter Word:")
self.top_label.grid(column=0, row=0)
self.word_entry = Entry(self.translate_tab, width=45)
self.word_entry.grid(column=0, row=1, columnspan=3, padx=5, pady=5)
self.language_label = Label(self.translate_tab, text="Language:")
self.language_label.grid(column=0,row=2,pady=5)
self.language_menu = ttk.Combobox(self.translate_tab, values=[*self.languages.keys()])
self.language_menu.grid(column=1, row=2)
self.language_menu.current(0)
self.translate_button = Button(self.translate_tab,text="Translate!", command=self.translate_function)
self.translate_button.grid(column=0, row=3, pady=15)
self.speak_check = Checkbutton(self.translate_tab, text="Say It!", variable=self.speak_it)
self.speak_check.grid(column=1, row=3)
self.word_frame = LabelFrame(self.translate_tab, text="Word:", width=300, height=50)
self.word_frame.grid(column=0, row=4, columnspan=3)
self.word_frame.grid_propagate(0)
self.word_result = Label(self.word_frame, text="")
self.word_result.grid()
self.noun_frame = LabelFrame(self.translate_tab, text="Noun:", width=300, height=100)
self.noun_frame.grid(column=0, row=5, columnspan=3)
self.noun_frame.grid_propagate(0)
self.noun_result = Label(self.noun_frame, text="")
self.noun_result.grid()
self.verb_frame = LabelFrame(self.translate_tab, text="Verb:", width=300, height=100)
self.verb_frame.grid(column=0, row=6, columnspan=3)
self.verb_frame.grid_propagate(0)
self.verb_result = Label(self.verb_frame, text="")
self.verb_result.grid()
def about_page(self):
pass
def translate_function(self):
word =self.word_entry.get()
language = self.languages.get(self.language_menu.get())
noun, verb = self.definition(word)
try:
noun = str(*noun[0])
verb = str(*verb[0])
except:
noun = str(noun[0])
verb = str(verb[0])
translated_word = self.translate_func(word,language).text
translated_noun = self.translate_func(noun,language).text
translated_verb = self.translate_func(verb,language).text
speak_thread = threading.Thread(target=self.speak, args=(translated_word, language))
self.word_result.configure(text=translated_word)
self.noun_result.configure(text=textwrap.fill(translated_noun, 50))
self.verb_result.configure(text=textwrap.fill(translated_verb, 50))
if self.speak_it.get():
speak_thread.start()
def definition(self,word):
nouns, verbs = [],[]
dictionary = PyDictionary()
word = self.translate_func(word, "en").text
my_meaning = dictionary.meaning(word)
try:
if my_meaning['Noun']:nouns.append(my_meaning["Noun"][0])
if my_meaning['Verb']:verbs.append(my_meaning["Verb"][0])
except:
nouns.append("No Data")
verbs.append("No Data")
return nouns, verbs
def translate_func(self,words,language):
translator = Translator(service_urls=["translate.google.com"])
translation = translator.translate(words, dest=language)
return translation
def speak(self,text,language):
with io.BytesIO() as file:
gTTS(text=text, lang = language).write_to_fp(file)
file.seek(0)
pygame.mixer.init()
pygame.mixer.music.load(file)
pygame.mixer.music.play()
while pygame.mixer.music.get_busy():
continue
if __name__ == "__main__":
root = Tk()
root.title("Translator")
root.geometry("900x900")
Translator_App(root)
root.mainloop()