Spaces:
Sleeping
Sleeping
Update quiz.py
Browse files
quiz.py
CHANGED
|
@@ -1,27 +1,58 @@
|
|
| 1 |
-
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
max_new_tokens=50,
|
| 14 |
-
temperature=0.7,
|
| 15 |
-
top_p=0.9
|
| 16 |
-
)
|
| 17 |
-
question = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 18 |
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
return user_answer == correct_answer
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import random
|
| 2 |
+
from vocab import get_words_from_source
|
| 3 |
+
from sentences import get_sentence
|
| 4 |
|
| 5 |
+
# 生成單字選擇題
|
| 6 |
|
| 7 |
+
def generate_fill_in_blank_exam(source, num):
|
| 8 |
+
words_data = get_words_from_source(source)
|
| 9 |
+
words = random.sample(words_data, num)
|
| 10 |
|
| 11 |
+
questions = []
|
| 12 |
+
for word_data in words:
|
| 13 |
+
word = word_data['word']
|
| 14 |
+
phonetic = word_data['phonetic']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
|
| 16 |
+
# 取得例句
|
| 17 |
+
sentence_data = get_sentence(word)
|
| 18 |
+
if not sentence_data:
|
| 19 |
+
continue # 沒例句的跳過
|
| 20 |
|
| 21 |
+
sentence = sentence_data[2]
|
| 22 |
+
blank_sentence = sentence.replace(word, '______')
|
|
|
|
| 23 |
|
| 24 |
+
# 生成干擾選項 (亂數抽 3 個其他單字)
|
| 25 |
+
other_words = [w['word'] for w in words_data if w['word'] != word]
|
| 26 |
+
distractors = random.sample(other_words, 3)
|
| 27 |
+
options = [word] + distractors
|
| 28 |
+
random.shuffle(options)
|
| 29 |
+
|
| 30 |
+
questions.append({
|
| 31 |
+
"sentence": blank_sentence,
|
| 32 |
+
"options": options,
|
| 33 |
+
"answer": word,
|
| 34 |
+
"phonetic": phonetic
|
| 35 |
+
})
|
| 36 |
+
|
| 37 |
+
return questions
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# 自動對答案並計分
|
| 41 |
+
def check_exam(user_answers, questions):
|
| 42 |
+
correct_count = 0
|
| 43 |
+
results = []
|
| 44 |
+
|
| 45 |
+
for i, user_answer in enumerate(user_answers):
|
| 46 |
+
correct_answer = questions[i]['answer']
|
| 47 |
+
is_correct = (user_answer == correct_answer)
|
| 48 |
+
results.append({
|
| 49 |
+
"question": questions[i]['sentence'],
|
| 50 |
+
"user_answer": user_answer,
|
| 51 |
+
"correct_answer": correct_answer,
|
| 52 |
+
"is_correct": is_correct
|
| 53 |
+
})
|
| 54 |
+
if is_correct:
|
| 55 |
+
correct_count += 1
|
| 56 |
+
|
| 57 |
+
score = f"{correct_count}/{len(questions)} 分"
|
| 58 |
+
return score, results
|