File size: 1,584 Bytes
ac006b7
 
 
 
 
1bf19e9
 
ac006b7
 
 
1bf19e9
 
ac006b7
 
1bf19e9
ac006b7
1bf19e9
 
 
 
ac006b7
 
1bf19e9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import random
import pandas as pd

# Load the vocabulary data
df = pd.read_csv("toeic_vocal_1.csv")
vocab_dict = {row['vocab']: row['meaning'] for _, row in df.iterrows()}

# Function to generate a new question
def generate_question():
    thai_word, correct_answer = random.choice(list(vocab_dict.items()))
    wrong_answers = random.sample([v for v in vocab_dict.values() if v != correct_answer], 4)
    options = wrong_answers + [correct_answer]
    random.shuffle(options)
    return thai_word, correct_answer, options

# Initialize session state
if 'question' not in st.session_state:
    st.session_state['question'] = generate_question()
    st.session_state['answered'] = False

# Display question
thai_word, correct_answer, options = st.session_state['question']
st.header("ฝึกคำศัพท์ภาษาอังกฤษ")
st.write(f"คำว่า '{thai_word}' คือคำว่าอะไร ?")

# Display answer options as buttons
if st.session_state['answered'] == False:
    for option in options:
        if st.button(option):
            if option == correct_answer:
                st.success("ถูกต้อง!")
            else:
                st.error("ผิด!")
                st.info(f"คำตอบที่ถูกต้องคือ: {correct_answer}")
            st.session_state['answered'] = True

# Next button for a new question
if st.session_state['answered']:
    if st.button("Next"):
        st.session_state['question'] = generate_question()
        st.session_state['answered'] = False