-
Notifications
You must be signed in to change notification settings - Fork 0
/
_04_practice_speaking.py
139 lines (110 loc) · 3.88 KB
/
_04_practice_speaking.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
import os
from pykakasi import kakasi
import warnings
import tkinter as tk
from tkinter import scrolledtext
warnings.filterwarnings("ignore", category=DeprecationWarning)
kks = kakasi()
kks.setMode("J", "H") # Japanese to Kana
conv = kks.getConverter()
def speak(voice_file):
if not os.path.exists(voice_file):
print(f"You have navigated outside the conversation dialog. Please try again")
return
if os.name == 'nt': # For Windows
os.system(f'start {voice_file}')
elif os.name == 'posix': # For MacOS and Linux
if os.uname().sysname == 'Darwin': # MacOS
os.system(f'afplay {voice_file}')
else: # Linux
os.system(f'ffplay -nodisp -autoexit {voice_file}')
def divide_list(lst, delimiter):
try:
# Find the index of the delimiter
idx = lst.index(delimiter)
# Slice the list into two parts based on the index of the delimiter
return lst[:idx], lst[idx+1:]
except ValueError:
# If the delimiter is not in the list, return the original list and an empty list
return lst, []
with open('conversation_scripts.txt', 'r', encoding='utf-8') as file:
lines = file.readlines()
lst = lines
delimiter = '*****\n'
en_conv, ja_conv = divide_list(lst, delimiter)
num = 0
def update_text(widget, text):
widget.config(state=tk.NORMAL)
widget.delete('1.0', tk.END)
widget.insert(tk.INSERT, text)
widget.config(state=tk.DISABLED)
def clear_japanese_texts():
update_text(japanese_text, '')
update_text(kana_text, '')
def speak_text_en(seq):
try:
update_text(english_text, en_conv[seq])
root.update() # Ensure the GUI updates before speaking
speak(voice_file=f'openai_en_voice_{seq}.mp3')
except IndexError:
print("Index out of range. Please try again.")
def speak_text_ja(seq):
try:
update_text(japanese_text, ja_conv[seq])
kana = conv.do(ja_conv[seq])
update_text(kana_text, 'Kana: {}'.format(kana))
root.update() # Ensure the GUI updates before speaking
speak(voice_file=f'elevenlab_ja_voice_{seq}.mp3')
except IndexError:
print("Index out of range. Please try again.")
def go_forward():
global num
num += 1
clear_japanese_texts() # Clear Japanese text boxes when 'Next' is clicked
speak_text_en(seq=num)
def go_back():
global num
num -= 1
speak_text_en(seq=num)
def end_program():
root.destroy()
def japanese():
global num
speak_text_ja(seq=num)
def helper():
help_text = '''
*********************************************
press the "Play English" button to start or play English speech
press the "Next" button to proceed
press the "Previous" button to go back
press the "Quit" button to end the program
press the "Play Japanese" button to play Japanese speech with text
*********************************************
'''
print(help_text)
def english():
global num
speak_text_en(seq=num)
# GUI setup
root = tk.Tk()
root.title("Japanese Conversation Practice")
# Text areas for displaying conversation
english_text = scrolledtext.ScrolledText(root, wrap=tk.WORD, state=tk.DISABLED, width=80, height=5)
english_text.pack()
japanese_text = scrolledtext.ScrolledText(root, wrap=tk.WORD, state=tk.DISABLED, width=80, height=5)
japanese_text.pack()
kana_text = scrolledtext.ScrolledText(root, wrap=tk.WORD, state=tk.DISABLED, width=80, height=5)
kana_text.pack()
# Adding buttons
btn_english = tk.Button(root, text="Play English", command=english)
btn_english.pack()
btn_next = tk.Button(root, text="Next", command=go_forward)
btn_next.pack()
btn_previous = tk.Button(root, text="Previous", command=go_back)
btn_previous.pack()
btn_japanese = tk.Button(root, text="Play Japanese", command=japanese)
btn_japanese.pack()
btn_quit = tk.Button(root, text="Quit", command=end_program)
btn_quit.pack()
helper()
root.mainloop()