Spaces:
Sleeping
Sleeping
PongsakornSET
commited on
Commit
•
1bf19e9
1
Parent(s):
a268184
Update app.py
Browse files
app.py
CHANGED
@@ -3,47 +3,40 @@ import random
|
|
3 |
import pandas as pd
|
4 |
|
5 |
# Load the vocabulary data
|
6 |
-
df = pd.read_csv("
|
7 |
-
vocab_dict =
|
8 |
-
for line in df.iterrows():
|
9 |
-
vocab_dict[line[1]['meaning']] = (line[1]['word'])
|
10 |
|
11 |
# Function to generate a new question
|
12 |
def generate_question():
|
13 |
-
thai_word, correct_answer = random.choice(vocab_dict.items())
|
14 |
-
|
15 |
-
# Generate wrong answers
|
16 |
-
wrong_answers = random.sample([v[0] for v in vocab_dict.values()], 4)
|
17 |
-
while correct_answer in wrong_answers:
|
18 |
-
wrong_answers = random.sample([v[0] for v in vocab_dict.values()], 4)
|
19 |
-
|
20 |
options = wrong_answers + [correct_answer]
|
21 |
random.shuffle(options)
|
22 |
-
|
23 |
-
return thai_word, correct_answer, correct_part_of_speech, options
|
24 |
|
25 |
-
#
|
26 |
-
if '
|
27 |
-
st.session_state
|
28 |
-
st.session_state
|
29 |
-
|
30 |
-
thai_word, correct_answer, options = st.session_state.question_data
|
31 |
|
32 |
# Display question
|
33 |
-
st.
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
|
|
|
|
|
3 |
import pandas as pd
|
4 |
|
5 |
# Load the vocabulary data
|
6 |
+
df = pd.read_csv("toeic_vocal_1.csv")
|
7 |
+
vocab_dict = {row['vocab']: row['meaning'] for _, row in df.iterrows()}
|
|
|
|
|
8 |
|
9 |
# Function to generate a new question
|
10 |
def generate_question():
|
11 |
+
thai_word, correct_answer = random.choice(list(vocab_dict.items()))
|
12 |
+
wrong_answers = random.sample([v for v in vocab_dict.values() if v != correct_answer], 4)
|
|
|
|
|
|
|
|
|
|
|
13 |
options = wrong_answers + [correct_answer]
|
14 |
random.shuffle(options)
|
15 |
+
return thai_word, correct_answer, options
|
|
|
16 |
|
17 |
+
# Initialize session state
|
18 |
+
if 'question' not in st.session_state:
|
19 |
+
st.session_state['question'] = generate_question()
|
20 |
+
st.session_state['answered'] = False
|
|
|
|
|
21 |
|
22 |
# Display question
|
23 |
+
thai_word, correct_answer, options = st.session_state['question']
|
24 |
+
st.header("ฝึกคำศัพท์ภาษาอังกฤษ")
|
25 |
+
st.write(f"คำว่า '{thai_word}' คือคำว่าอะไร ?")
|
26 |
+
|
27 |
+
# Display answer options as buttons
|
28 |
+
if st.session_state['answered'] == False:
|
29 |
+
for option in options:
|
30 |
+
if st.button(option):
|
31 |
+
if option == correct_answer:
|
32 |
+
st.success("ถูกต้อง!")
|
33 |
+
else:
|
34 |
+
st.error("ผิด!")
|
35 |
+
st.info(f"คำตอบที่ถูกต้องคือ: {correct_answer}")
|
36 |
+
st.session_state['answered'] = True
|
37 |
+
|
38 |
+
# Next button for a new question
|
39 |
+
if st.session_state['answered']:
|
40 |
+
if st.button("Next"):
|
41 |
+
st.session_state['question'] = generate_question()
|
42 |
+
st.session_state['answered'] = False
|