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

# Load the vocabulary data
df = pd.read_csv("word1_v2.csv")
vocab_dict = dict()
for line in df.iterrows():
    vocab_dict[line[1]['meaning']] = (line[1]['vocab'], line[1]['pos'])

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

# Initial configuration
if 'question_data' not in st.session_state:
    st.session_state.question_data = generate_question()
    st.session_state.answered = False

thai_word, correct_answer, correct_part_of_speech, options = st.session_state.question_data

# Display question
st.write(f"คำว่า '{thai_word}' (ชนิดคำ: {correct_part_of_speech}) คือคำว่าอะไร?")

# Display answer options as radio buttons
selected_answer = st.radio("เลือกคำตอบของคุณ:", options)

# Check answer and show the result
if not st.session_state.answered and st.button("ตรวจคำตอบ"):
    st.session_state.answered = True
    if selected_answer == correct_answer:
        st.success("ถูกต้อง!")
    else:
        st.error(f"ผิด! คำตอบที่ถูกต้องคือ: {correct_answer}")

# 'Next question' button to load a new question
if st.session_state.answered and st.button("คำถามถัดไป"):
    st.session_state.question_data = generate_question()
    st.session_state.answered = False