-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
112 lines (86 loc) · 2.27 KB
/
main.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
from sound import audio
import time
def readtext(path):
with open(path, 'r') as f:
lines = f.readlines()
while '\n' in lines:
lines.remove('\n')
while '\t' in lines:
lines.remove('\t')
for i in range(0, len(lines)):
lines[i] = lines[i].lower().replace('\n', ' ').replace('\t', '')
return lines
def translate_file(lines: list):
letters = []
translation = ""
letters_translation = {
'a': ".-",
'b': "-...",
'c': "-.-.",
'd': "-..",
'e': ".",
'f': "..-.",
'g': "--.",
'h': "....",
'i': "..",
'j': ".---",
'k': "-.-",
'l': ".-..",
'm': "--",
'n': "-.",
'o': "---",
'p': ".--.",
'q': "--.-",
'r': ".-.",
's': "...",
't': "-",
'u': "..-",
'v': "...-",
'w': ".--",
'x': "-..-",
'y': "-.--",
'z': "--..",
'/': "/"
}
for line in lines:
for i in list(line):
letters.append(i)
for i in range(0, len(letters)):
if letters[i] == ' ':
letters[i] = '/'
for letter in letters:
translation = translation + letters_translation[letter]
translation = translation + ' '
return translation
def translate_user_input(lines):
lines = list(lines)
translation = translate_file(lines)
return translation
def sound_translation(translation: str):
sound_duration = {
'.': 0.3,
'-': 0.9,
' ': 0.0,
'/': 0.0
}
word_space = False
for i in range(0, len(translation)-1):
if translation[i] == '/':
time.sleep(0.5)
continue
print(translation[i])
audio(sound_duration[translation[i]])
def main():
translation = ''
if int(input("[1]Translate file\n[2]Enter text\nSelect an option:")) == 1:
path = input("Write the file path: ")
translation = translate_file(readtext(path))
print(translation)
sound_translation(translation)
else:
lines = input("Enter the text: ").lower()
translation = translate_user_input(lines)
sound_translation(translation)
print(translation)
if __name__ == '__main__':
main()