Spaces:
Runtime error
Runtime error
Update modules/phonics.py
Browse files- modules/phonics.py +20 -2
modules/phonics.py
CHANGED
@@ -1,17 +1,35 @@
|
|
1 |
import gradio as gr
|
2 |
from utils.helpers import load_content
|
|
|
3 |
|
4 |
def phonics_component():
|
5 |
content = load_content()
|
|
|
6 |
|
7 |
def practice_phonics(sound, user_input):
|
|
|
|
|
|
|
8 |
for level in ["beginner", "intermediate"]:
|
9 |
examples = next((p["examples"] for p in content["phonics"][level]
|
10 |
if p["sound"] == sound), [])
|
11 |
if examples:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
if user_input.lower().strip() in [ex.lower() for ex in examples]:
|
13 |
-
return "Correct! β"
|
14 |
-
|
|
|
|
|
|
|
15 |
return "Sound not found"
|
16 |
|
17 |
with gr.Column() as phonics:
|
|
|
1 |
import gradio as gr
|
2 |
from utils.helpers import load_content
|
3 |
+
from .translation_model import TranslationModel
|
4 |
|
5 |
def phonics_component():
|
6 |
content = load_content()
|
7 |
+
translator = TranslationModel()
|
8 |
|
9 |
def practice_phonics(sound, user_input):
|
10 |
+
if not user_input.strip():
|
11 |
+
return "Please enter a word"
|
12 |
+
|
13 |
for level in ["beginner", "intermediate"]:
|
14 |
examples = next((p["examples"] for p in content["phonics"][level]
|
15 |
if p["sound"] == sound), [])
|
16 |
if examples:
|
17 |
+
# Translate user input to English to verify meaning
|
18 |
+
translation = translator.translate(user_input.lower().strip(), "af", "en")
|
19 |
+
|
20 |
+
# Check if the word contains the sound
|
21 |
+
has_sound = sound.lower() in user_input.lower()
|
22 |
+
|
23 |
+
# Check if it's a valid Afrikaans word by translating back
|
24 |
+
back_translation = translator.translate(translation, "en", "af")
|
25 |
+
is_valid = back_translation.lower().strip() == user_input.lower().strip()
|
26 |
+
|
27 |
if user_input.lower().strip() in [ex.lower() for ex in examples]:
|
28 |
+
return f"Correct! β '{user_input}' contains the sound '{sound}'"
|
29 |
+
elif has_sound and is_valid:
|
30 |
+
return f"Good job! '{user_input}' is a valid word with the '{sound}' sound"
|
31 |
+
else:
|
32 |
+
return f"Try again. Examples: {', '.join(examples)}"
|
33 |
return "Sound not found"
|
34 |
|
35 |
with gr.Column() as phonics:
|