PongsakornSET commited on
Commit
1bf19e9
1 Parent(s): a268184

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +29 -36
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("toeic_vocab_test_1.csv")
7
- vocab_dict = 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
- # Initial configuration
26
- if 'question_data' not in st.session_state:
27
- st.session_state.question_data = generate_question()
28
- st.session_state.answered = False
29
-
30
- thai_word, correct_answer, options = st.session_state.question_data
31
 
32
  # Display question
33
- st.write(f"คำว่า '{thai_word}' (ชนิดคำ: {correct_part_of_speech}) คือคำว่าอะไร?")
34
-
35
- # Display answer options as radio buttons
36
- selected_answer = st.radio("เลือกคำตอบของคุณ:", options)
37
-
38
- # Check answer and show the result
39
- if not st.session_state.answered and st.button("ตรวจคำตอบ"):
40
- st.session_state.answered = True
41
- if selected_answer == correct_answer:
42
- st.success("ถูกต้อง!")
43
- else:
44
- st.error(f"ผิด! คำตอบที่ถูกต้องคือ: {correct_answer}")
45
-
46
- # 'Next question' button to load a new question
47
- if st.session_state.answered and st.button("คำถามถัดไป"):
48
- st.session_state.question_data = generate_question()
49
- st.session_state.answered = False
 
 
 
 
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