File size: 2,270 Bytes
47a06b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03d7947
47a06b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d4127c3
 
47a06b5
 
 
 
d4127c3
47a06b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Import the required module for text
# to speech conversion
from gtts import gTTS

# This module is imported so that we can
# play the converted audio
import os
import re
import streamlit as st
import numpy as np

def tts(text):
    # Language in which you want to convert
    language = 'en'

    # Passing the text and language to the engine,
    # here we have marked slow=False. Which tells
    # the module that the converted audio should
    # have a high speed
    speech = gTTS(text=text, lang=language, slow=False)

    # Saving the converted audio in a mp3 file named
    # welcome
    speech.save("sample.mp3")

def get_words():
    with open("words.txt", 'r') as f:
        words = f.read()
    # print(words)
    words = re.split('،|\.|/|:| |,|\n',words)

    words = [word.strip().lower() for word in words if word.strip() != ""]
    # rest = [word for word in words if "." in word]
    # words = [word for word in words if "." not in word]

    # print(len(words))
    return words

def genrate_word(words):
    word = np.random.choice(words, size=1)[0]
    print("genrating ", word)
    with open("word.txt", "w") as f:
        f.write(word)
    tts(word)
    return word

def update_audio():
    audio_file = open('sample.mp3', 'rb')
    audio_bytes = audio_file.read()
    st.audio(audio_bytes, format='audio/mp3')

def update_frontend():
    words = get_words()
    genrate_word(words)
    update_audio()

def get_main_word():
    with open("word.txt", "r") as f:
        return f.read()

st.title("Spelling checker for English learning")

if st.sidebar.button("Regenerate"):
    update_frontend()
    user_word = None
    st.session_state["text"] = ""


if st.sidebar.button("Show"):
    update_audio()

user_word = st.text_input("word", value="", key="text")

if st.sidebar.button("check spelling") or user_word:
    main_word  = get_main_word()
    if main_word == user_word.lower():
        st.balloons()
        st.success('Correct spelling! Go get 8/100 score', icon="✅")
    else:
        st.snow()
        st.error('Try harder', icon="🚨")
 
if st.sidebar.button("Show word"):
    main_word  = get_main_word()
    st.info('Word is '+main_word, icon="ℹ️")
    
st.text(str(len(get_words()))+" words are used in this demo, Good luck!")