linhnguyen02 commited on
Commit ·
6d7ed95
1
Parent(s): 90c13d8
update fill_the_blank
Browse files- src/interfaces/evaluation.py +1 -0
- src/llms/models/openai.py +1 -1
- src/llms/prompts/fill_in_blank.py +27 -20
- src/routers/public/quesion_openai.py +5 -4
- src/services/eval.py +40 -30
src/interfaces/evaluation.py
CHANGED
|
@@ -13,6 +13,7 @@ class GeneratedQuestion:
|
|
| 13 |
|
| 14 |
paragraph: Optional[str] = None
|
| 15 |
content: Optional[str] = None
|
|
|
|
| 16 |
list_words: List[str] = field(default_factory=list)
|
| 17 |
choices: List[IChoice] = field(default_factory=list)
|
| 18 |
|
|
|
|
| 13 |
|
| 14 |
paragraph: Optional[str] = None
|
| 15 |
content: Optional[str] = None
|
| 16 |
+
explanation: Optional[str] = None
|
| 17 |
list_words: List[str] = field(default_factory=list)
|
| 18 |
choices: List[IChoice] = field(default_factory=list)
|
| 19 |
|
src/llms/models/openai.py
CHANGED
|
@@ -25,7 +25,7 @@ def extract_json(text):
|
|
| 25 |
|
| 26 |
class OpenAILLM(LLMBase):
|
| 27 |
# "gpt-4.1-nano-2025-04-14"
|
| 28 |
-
def __init__(self, model="Qwen/Qwen2.5-3B-Instruct", temperature=0.
|
| 29 |
self.model = model
|
| 30 |
self.temperature = temperature
|
| 31 |
self.max_tokens = max_tokens
|
|
|
|
| 25 |
|
| 26 |
class OpenAILLM(LLMBase):
|
| 27 |
# "gpt-4.1-nano-2025-04-14"
|
| 28 |
+
def __init__(self, model="Qwen/Qwen2.5-3B-Instruct", temperature=0.2, max_tokens=1024, top_p=0.3, top_k=5, enable_vision=False, vision_details="auto", http_client=None):
|
| 29 |
self.model = model
|
| 30 |
self.temperature = temperature
|
| 31 |
self.max_tokens = max_tokens
|
src/llms/prompts/fill_in_blank.py
CHANGED
|
@@ -1,46 +1,50 @@
|
|
| 1 |
GEN_FILL_IN_BLANK_QUESTION_PROMPT = """
|
| 2 |
### Task
|
| 3 |
You are an expert in automatically generating English exam questions.
|
| 4 |
-
Create **one list of "Fill in the Blank"
|
| 5 |
|
| 6 |
### Input
|
| 7 |
- A list of words to be used to create the sentence.
|
| 8 |
- Question type: "single-choice" or "multiple-choice".
|
| 9 |
- Desired number of answer choices.
|
| 10 |
-
- Number of
|
| 11 |
|
| 12 |
### Core requirements
|
| 13 |
-
1. First produce a **grammatically correct, natural, meaningful English sentence** using
|
| 14 |
-
-
|
| 15 |
-
- Sentence length: **
|
| 16 |
-
- This correct sentence will be used
|
| 17 |
-
- If no words are provided, you may freely create
|
| 18 |
|
| 19 |
-
2. **Validation step (mandatory):** Before creating the blank, ensure the sentence is fully grammatical and natural.
|
| 20 |
- Avoid unnatural collocations or redundant connectors.
|
| 21 |
- If any connector misuse or tense inconsistency is found, rewrite the sentence until it is correct.
|
| 22 |
|
| 23 |
### Connector rules (important — follow exactly)
|
| 24 |
-
- **Do NOT combine a subordinating conjunction
|
| 25 |
-
- Incorrect: "Although he was tired, but he continued."
|
| 26 |
- Correct: "Although he was tired, he continued." or "He was tired, but he continued."
|
| 27 |
- Do NOT use duplicate connectors (e.g., "although ... however").
|
| 28 |
- Avoid redundant fillers such as "but yet", "and also", "and then then".
|
| 29 |
- If you use "because", do not also use "so" in the same causal relationship.
|
| 30 |
|
| 31 |
-
### IMPORTANT BALANCE RULE
|
| 32 |
- Do NOT overuse connectors.
|
| 33 |
- Prefer testing **verbs, verb tense, collocations, prepositions, vocabulary, or fixed expressions** when appropriate.
|
| 34 |
- Only test connectors when they are truly central to the sentence meaning.
|
|
|
|
| 35 |
---
|
| 36 |
|
| 37 |
### Blank-creation rules
|
| 38 |
-
3. After you have a validated correct sentence, choose
|
| 39 |
-
-
|
| 40 |
-
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
| 44 |
|
| 45 |
4. The blank(s) must make sense — the question should be solvable through grammar or meaning, not guessing.
|
| 46 |
|
|
@@ -48,12 +52,15 @@ Create **one list of "Fill in the Blank" question** in English.
|
|
| 48 |
|
| 49 |
### Choices and answer
|
| 50 |
5. Create a `choices` list of answer options (equal to the desired number).
|
| 51 |
-
- Include the correct
|
| 52 |
-
- For incorrect distractors
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
6. The `answer` field must list the correct word(s) that fill the blank(s).
|
| 55 |
7. The `explanation` field must contain the **full correct sentence** (before blanking).
|
| 56 |
8. The `tags` field must list the linguistic skill tested (e.g., "preposition", "connector", "verb tense", "collocation", "vocabulary").
|
| 57 |
|
| 58 |
Follow these rules strictly and make the question natural, educational, and clear.
|
| 59 |
-
"""
|
|
|
|
| 1 |
GEN_FILL_IN_BLANK_QUESTION_PROMPT = """
|
| 2 |
### Task
|
| 3 |
You are an expert in automatically generating English exam questions.
|
| 4 |
+
Create **one list of "Fill in the Blank" questions** in English.
|
| 5 |
|
| 6 |
### Input
|
| 7 |
- A list of words to be used to create the sentence.
|
| 8 |
- Question type: "single-choice" or "multiple-choice".
|
| 9 |
- Desired number of answer choices.
|
| 10 |
+
- Number of questions to generate.
|
| 11 |
|
| 12 |
### Core requirements
|
| 13 |
+
1. First, produce a **grammatically correct, natural, and meaningful English sentence** using **some of the words** from the provided list.
|
| 14 |
+
- The sentence may be simple or have **up to two clauses**.
|
| 15 |
+
- Sentence length: **6–30 words**.
|
| 16 |
+
- This correct sentence will later be used as the `explanation` value (the full correct version).
|
| 17 |
+
- If no words are provided, you may freely create an appropriate sentence.
|
| 18 |
|
| 19 |
+
2. **Validation step (mandatory):** Before creating the blank, ensure the sentence is fully grammatical and natural.
|
| 20 |
- Avoid unnatural collocations or redundant connectors.
|
| 21 |
- If any connector misuse or tense inconsistency is found, rewrite the sentence until it is correct.
|
| 22 |
|
| 23 |
### Connector rules (important — follow exactly)
|
| 24 |
+
- **Do NOT combine a subordinating conjunction** such as "although", "though", "while", or "despite" **with a coordinating conjunction** like "but" in the same sentence.
|
| 25 |
+
- Incorrect: "Although he was tired, but he continued."
|
| 26 |
- Correct: "Although he was tired, he continued." or "He was tired, but he continued."
|
| 27 |
- Do NOT use duplicate connectors (e.g., "although ... however").
|
| 28 |
- Avoid redundant fillers such as "but yet", "and also", "and then then".
|
| 29 |
- If you use "because", do not also use "so" in the same causal relationship.
|
| 30 |
|
| 31 |
+
### IMPORTANT BALANCE RULE
|
| 32 |
- Do NOT overuse connectors.
|
| 33 |
- Prefer testing **verbs, verb tense, collocations, prepositions, vocabulary, or fixed expressions** when appropriate.
|
| 34 |
- Only test connectors when they are truly central to the sentence meaning.
|
| 35 |
+
|
| 36 |
---
|
| 37 |
|
| 38 |
### Blank-creation rules
|
| 39 |
+
3. After you have a validated correct sentence, choose:
|
| 40 |
+
- **1 important word** for "single-choice", or
|
| 41 |
+
- **2 or more important words** for "multiple-choice"
|
| 42 |
+
to replace with blanks (`____`).
|
| 43 |
+
- The version with blanks must be assigned to the `content` field.
|
| 44 |
+
- The text replaced by blanks is the **answer** of the question.
|
| 45 |
+
- Example:
|
| 46 |
+
- content: "She ____ to bed later yesterday."
|
| 47 |
+
- answer: ["went"]
|
| 48 |
|
| 49 |
4. The blank(s) must make sense — the question should be solvable through grammar or meaning, not guessing.
|
| 50 |
|
|
|
|
| 52 |
|
| 53 |
### Choices and answer
|
| 54 |
5. Create a `choices` list of answer options (equal to the desired number).
|
| 55 |
+
- Include the correct answer.
|
| 56 |
+
- For incorrect distractors:
|
| 57 |
+
- Use words of the same part of speech or different forms/tenses of the same word.
|
| 58 |
+
- If the answer is a verb, distractors must match the original tense or be plausible tense variants.
|
| 59 |
+
- Distractors may be semantically similar but incorrect in context.
|
| 60 |
|
| 61 |
6. The `answer` field must list the correct word(s) that fill the blank(s).
|
| 62 |
7. The `explanation` field must contain the **full correct sentence** (before blanking).
|
| 63 |
8. The `tags` field must list the linguistic skill tested (e.g., "preposition", "connector", "verb tense", "collocation", "vocabulary").
|
| 64 |
|
| 65 |
Follow these rules strictly and make the question natural, educational, and clear.
|
| 66 |
+
"""
|
src/routers/public/quesion_openai.py
CHANGED
|
@@ -23,15 +23,16 @@ async def generate_question(body: ICreateQuestion):
|
|
| 23 |
num_question=body.num_question,
|
| 24 |
num_ans_per_question=body.num_ans_per_question,
|
| 25 |
)
|
| 26 |
-
print("+++++", list_questions)
|
| 27 |
evaluator = QuestionQualityEvaluator()
|
| 28 |
final_data = []
|
| 29 |
for q in list_questions:
|
| 30 |
data = {**body.model_dump(), **q}
|
| 31 |
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
| 35 |
|
| 36 |
return JSONResponse(status_code=200, content=res_ok(final_data))
|
| 37 |
|
|
|
|
| 23 |
num_question=body.num_question,
|
| 24 |
num_ans_per_question=body.num_ans_per_question,
|
| 25 |
)
|
|
|
|
| 26 |
evaluator = QuestionQualityEvaluator()
|
| 27 |
final_data = []
|
| 28 |
for q in list_questions:
|
| 29 |
data = {**body.model_dump(), **q}
|
| 30 |
|
| 31 |
+
res_eval = evaluator.evaluate(data)
|
| 32 |
+
final_data.append({
|
| 33 |
+
**q,
|
| 34 |
+
"score": res_eval["score"]
|
| 35 |
+
})
|
| 36 |
|
| 37 |
return JSONResponse(status_code=200, content=res_ok(final_data))
|
| 38 |
|
src/services/eval.py
CHANGED
|
@@ -47,7 +47,7 @@ class QuestionQualityEvaluator:
|
|
| 47 |
d_score, d_issues = self._check_distractors(q)
|
| 48 |
all_issues.append({"field": "distractor", "score": d_score, "issues": d_issues})
|
| 49 |
|
| 50 |
-
w_score = self.weights["structure"] + self.weights["popularity"] + self.weights["distractor"] + self.weights["ai_adjust_factor"] if check_by_ai else 0.0
|
| 51 |
final_score = (
|
| 52 |
s_score * self.weights["structure"] +
|
| 53 |
p_score * self.weights["popularity"] +
|
|
@@ -57,7 +57,7 @@ class QuestionQualityEvaluator:
|
|
| 57 |
rounded_score = math.ceil(final_score * 10) / 10
|
| 58 |
|
| 59 |
return {
|
| 60 |
-
"score": min(round(rounded_score, 1), 10.0),
|
| 61 |
"issues": all_issues,
|
| 62 |
"suggestions": list(set(all_suggestions))
|
| 63 |
}
|
|
@@ -68,11 +68,20 @@ class QuestionQualityEvaluator:
|
|
| 68 |
score = 1.0
|
| 69 |
|
| 70 |
# Question text
|
| 71 |
-
if not q
|
| 72 |
issues.append("missing_question_text")
|
| 73 |
score -= self.penalties["missing_question_text"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
else:
|
| 75 |
-
grammar_count, grammar_msgs = self._check_grammar(q
|
| 76 |
if grammar_count > 0:
|
| 77 |
issues.append({
|
| 78 |
"type": "question_grammar_error",
|
|
@@ -82,7 +91,7 @@ class QuestionQualityEvaluator:
|
|
| 82 |
score -= grammar_count * self.penalties["grammar_error_per_count"]
|
| 83 |
|
| 84 |
# Choices
|
| 85 |
-
if not q
|
| 86 |
issues.append("missing_choices")
|
| 87 |
score -= self.penalties["missing_choices"]
|
| 88 |
else:
|
|
@@ -90,18 +99,18 @@ class QuestionQualityEvaluator:
|
|
| 90 |
unique_contents = []
|
| 91 |
has_correct = False
|
| 92 |
|
| 93 |
-
for choice in q
|
| 94 |
-
content = (choice
|
| 95 |
if not content:
|
| 96 |
empty_count += 1
|
| 97 |
continue
|
| 98 |
unique_contents.append(content)
|
| 99 |
-
if choice
|
| 100 |
has_correct = True
|
| 101 |
|
| 102 |
if empty_count > 0:
|
| 103 |
issues.append(f"{empty_count}_empty_choices")
|
| 104 |
-
score -= self.penalties["empty_choice_ratio"] * (empty_count / len(q
|
| 105 |
|
| 106 |
if len(set(unique_contents)) < len(unique_contents):
|
| 107 |
issues.append("duplicated_choices")
|
|
@@ -125,9 +134,9 @@ class QuestionQualityEvaluator:
|
|
| 125 |
return max(score, 0.0), issues, suggestions
|
| 126 |
|
| 127 |
def _check_popularity(self, q: GeneratedQuestion) -> float:
|
| 128 |
-
unique_words = set(q
|
| 129 |
-
for choice in q
|
| 130 |
-
unique_words.update((choice
|
| 131 |
|
| 132 |
if not unique_words:
|
| 133 |
return 0.0
|
|
@@ -135,7 +144,7 @@ class QuestionQualityEvaluator:
|
|
| 135 |
es = Elastic()
|
| 136 |
resp = es.search(
|
| 137 |
index=self.INDEX,
|
| 138 |
-
size=
|
| 139 |
query={"terms": {"word.keyword": list(unique_words)}},
|
| 140 |
aggs={
|
| 141 |
"by_word": {
|
|
@@ -215,23 +224,24 @@ class QuestionQualityEvaluator:
|
|
| 215 |
matches = self._grammar_tool.check(text)
|
| 216 |
serious_matches = [
|
| 217 |
m for m in matches
|
| 218 |
-
if m.
|
| 219 |
-
and not m.
|
| 220 |
]
|
| 221 |
|
| 222 |
error_messages = [
|
| 223 |
{
|
| 224 |
"message": m.message,
|
| 225 |
-
"rule": m.
|
| 226 |
-
"error_text": text[m.offset:m.offset + m.
|
| 227 |
"suggestions": m.replacements[:3]
|
| 228 |
}
|
| 229 |
for m in serious_matches[:max_errors]
|
| 230 |
]
|
|
|
|
| 231 |
return len(error_messages), error_messages
|
| 232 |
|
| 233 |
def _check_pos_and_meaning_of_choice(self, q: GeneratedQuestion) -> Optional[float]:
|
| 234 |
-
if q
|
| 235 |
return 1.0
|
| 236 |
|
| 237 |
to_be_regex = re.compile(
|
|
@@ -242,8 +252,8 @@ class QuestionQualityEvaluator:
|
|
| 242 |
cleaned_choices: List[str] = []
|
| 243 |
score = 1.0
|
| 244 |
|
| 245 |
-
for c in q
|
| 246 |
-
content = (c
|
| 247 |
if not content:
|
| 248 |
score -= self.distractor_cfg["empty_choice_deduction"]
|
| 249 |
continue
|
|
@@ -257,14 +267,14 @@ class QuestionQualityEvaluator:
|
|
| 257 |
docs = [self.nlp(text) for text in cleaned_choices]
|
| 258 |
tokens = [token for doc in docs for token in doc]
|
| 259 |
|
| 260 |
-
return score * self.lexical_family_difficulty(tokens, q
|
| 261 |
|
| 262 |
def _cal_score_embedding_similarity(self, q: GeneratedQuestion) -> Optional[float]:
|
| 263 |
-
if q
|
| 264 |
return None
|
| 265 |
|
| 266 |
-
correct = [c
|
| 267 |
-
distractors = [c
|
| 268 |
if not correct or not distractors:
|
| 269 |
return 0.0
|
| 270 |
|
|
@@ -294,23 +304,23 @@ class QuestionQualityEvaluator:
|
|
| 294 |
return 1.0
|
| 295 |
|
| 296 |
def _cal_score_for_paragraph(self, q: GeneratedQuestion) -> Optional[float]:
|
| 297 |
-
if q
|
| 298 |
QuestionTypeEnum.VOCAB, QuestionTypeEnum.FACT,
|
| 299 |
QuestionTypeEnum.MAIN_IDEA, QuestionTypeEnum.INFERENCE,
|
| 300 |
QuestionTypeEnum.PURPOSE
|
| 301 |
}:
|
| 302 |
return None
|
| 303 |
|
| 304 |
-
correct_answer = next((c
|
| 305 |
-
if not correct_answer or not q
|
| 306 |
return 0.0
|
| 307 |
|
| 308 |
-
words = q
|
| 309 |
word_count = len(words)
|
| 310 |
p_cfg = self.distractor_cfg["paragraph"]
|
| 311 |
|
| 312 |
# Length score
|
| 313 |
-
if q
|
| 314 |
thresholds = p_cfg["vocab_length_thresholds"]
|
| 315 |
scores = [0.2, 0.3, 0.4, 0.5]
|
| 316 |
else:
|
|
@@ -324,7 +334,7 @@ class QuestionQualityEvaluator:
|
|
| 324 |
break
|
| 325 |
|
| 326 |
# Difficulty score
|
| 327 |
-
doc = self.nlp(q
|
| 328 |
sentences = [sent.text.strip() for sent in doc.sents if sent.text.strip()]
|
| 329 |
if not sentences:
|
| 330 |
return length_score * p_cfg["length_weight"]
|
|
|
|
| 47 |
d_score, d_issues = self._check_distractors(q)
|
| 48 |
all_issues.append({"field": "distractor", "score": d_score, "issues": d_issues})
|
| 49 |
|
| 50 |
+
w_score = self.weights["structure"] + self.weights["popularity"] + self.weights["distractor"] + (self.weights["ai_adjust_factor"] if check_by_ai else 0.0)
|
| 51 |
final_score = (
|
| 52 |
s_score * self.weights["structure"] +
|
| 53 |
p_score * self.weights["popularity"] +
|
|
|
|
| 57 |
rounded_score = math.ceil(final_score * 10) / 10
|
| 58 |
|
| 59 |
return {
|
| 60 |
+
"score": min(round(rounded_score, 1), 10.0) * 10,
|
| 61 |
"issues": all_issues,
|
| 62 |
"suggestions": list(set(all_suggestions))
|
| 63 |
}
|
|
|
|
| 68 |
score = 1.0
|
| 69 |
|
| 70 |
# Question text
|
| 71 |
+
if not q["content"] or not q["content"].strip():
|
| 72 |
issues.append("missing_question_text")
|
| 73 |
score -= self.penalties["missing_question_text"]
|
| 74 |
+
elif q["type"] == QuestionTypeEnum.FILL_IN_BLANK and q["explanation"] and q["explanation"].strip():
|
| 75 |
+
grammar_count, grammar_msgs = self._check_grammar(q["explanation"])
|
| 76 |
+
if grammar_count > 0:
|
| 77 |
+
issues.append({
|
| 78 |
+
"type": "question_grammar_error",
|
| 79 |
+
"count": grammar_count,
|
| 80 |
+
"details": grammar_msgs
|
| 81 |
+
})
|
| 82 |
+
score -= grammar_count * self.penalties["grammar_error_per_count"]
|
| 83 |
else:
|
| 84 |
+
grammar_count, grammar_msgs = self._check_grammar(q["content"])
|
| 85 |
if grammar_count > 0:
|
| 86 |
issues.append({
|
| 87 |
"type": "question_grammar_error",
|
|
|
|
| 91 |
score -= grammar_count * self.penalties["grammar_error_per_count"]
|
| 92 |
|
| 93 |
# Choices
|
| 94 |
+
if not q["choices"] or len(q["choices"]) == 0:
|
| 95 |
issues.append("missing_choices")
|
| 96 |
score -= self.penalties["missing_choices"]
|
| 97 |
else:
|
|
|
|
| 99 |
unique_contents = []
|
| 100 |
has_correct = False
|
| 101 |
|
| 102 |
+
for choice in q["choices"]:
|
| 103 |
+
content = (choice["content"] or "").strip()
|
| 104 |
if not content:
|
| 105 |
empty_count += 1
|
| 106 |
continue
|
| 107 |
unique_contents.append(content)
|
| 108 |
+
if choice["is_correct"]:
|
| 109 |
has_correct = True
|
| 110 |
|
| 111 |
if empty_count > 0:
|
| 112 |
issues.append(f"{empty_count}_empty_choices")
|
| 113 |
+
score -= self.penalties["empty_choice_ratio"] * (empty_count / len(q["choices"]))
|
| 114 |
|
| 115 |
if len(set(unique_contents)) < len(unique_contents):
|
| 116 |
issues.append("duplicated_choices")
|
|
|
|
| 134 |
return max(score, 0.0), issues, suggestions
|
| 135 |
|
| 136 |
def _check_popularity(self, q: GeneratedQuestion) -> float:
|
| 137 |
+
unique_words = set(q["content"].lower().split())
|
| 138 |
+
for choice in q["choices"] or []:
|
| 139 |
+
unique_words.update((choice["content"] or "").lower().split())
|
| 140 |
|
| 141 |
if not unique_words:
|
| 142 |
return 0.0
|
|
|
|
| 144 |
es = Elastic()
|
| 145 |
resp = es.search(
|
| 146 |
index=self.INDEX,
|
| 147 |
+
size=1,
|
| 148 |
query={"terms": {"word.keyword": list(unique_words)}},
|
| 149 |
aggs={
|
| 150 |
"by_word": {
|
|
|
|
| 224 |
matches = self._grammar_tool.check(text)
|
| 225 |
serious_matches = [
|
| 226 |
m for m in matches
|
| 227 |
+
if m.rule_issue_type in {"grammar", "misspelling"}
|
| 228 |
+
and not m.rule_id.startswith("UPPERCASE_SENTENCE_START")
|
| 229 |
]
|
| 230 |
|
| 231 |
error_messages = [
|
| 232 |
{
|
| 233 |
"message": m.message,
|
| 234 |
+
"rule": m.rule_id,
|
| 235 |
+
"error_text": text[m.offset:m.offset + m.error_length],
|
| 236 |
"suggestions": m.replacements[:3]
|
| 237 |
}
|
| 238 |
for m in serious_matches[:max_errors]
|
| 239 |
]
|
| 240 |
+
print(error_messages)
|
| 241 |
return len(error_messages), error_messages
|
| 242 |
|
| 243 |
def _check_pos_and_meaning_of_choice(self, q: GeneratedQuestion) -> Optional[float]:
|
| 244 |
+
if q["type"] in {QuestionTypeEnum.PRONUNCIATION, QuestionTypeEnum.STRESS}:
|
| 245 |
return 1.0
|
| 246 |
|
| 247 |
to_be_regex = re.compile(
|
|
|
|
| 252 |
cleaned_choices: List[str] = []
|
| 253 |
score = 1.0
|
| 254 |
|
| 255 |
+
for c in q["choices"] or []:
|
| 256 |
+
content = (c["content"] or "").strip()
|
| 257 |
if not content:
|
| 258 |
score -= self.distractor_cfg["empty_choice_deduction"]
|
| 259 |
continue
|
|
|
|
| 267 |
docs = [self.nlp(text) for text in cleaned_choices]
|
| 268 |
tokens = [token for doc in docs for token in doc]
|
| 269 |
|
| 270 |
+
return score * self.lexical_family_difficulty(tokens, q["num_ans_per_question"] or 4)
|
| 271 |
|
| 272 |
def _cal_score_embedding_similarity(self, q: GeneratedQuestion) -> Optional[float]:
|
| 273 |
+
if q["type"] not in {QuestionTypeEnum.SYNONYM, QuestionTypeEnum.ANTONYM, QuestionTypeEnum.VOCAB}:
|
| 274 |
return None
|
| 275 |
|
| 276 |
+
correct = [c["content"] for c in q["choices"] if c["is_correct"]]
|
| 277 |
+
distractors = [c["content"] for c in q["choices"] if not c["is_correct"]]
|
| 278 |
if not correct or not distractors:
|
| 279 |
return 0.0
|
| 280 |
|
|
|
|
| 304 |
return 1.0
|
| 305 |
|
| 306 |
def _cal_score_for_paragraph(self, q: GeneratedQuestion) -> Optional[float]:
|
| 307 |
+
if q["type"] not in {
|
| 308 |
QuestionTypeEnum.VOCAB, QuestionTypeEnum.FACT,
|
| 309 |
QuestionTypeEnum.MAIN_IDEA, QuestionTypeEnum.INFERENCE,
|
| 310 |
QuestionTypeEnum.PURPOSE
|
| 311 |
}:
|
| 312 |
return None
|
| 313 |
|
| 314 |
+
correct_answer = next((c["content"] for c in q["choices"] if c["is_correct"]), None)
|
| 315 |
+
if not correct_answer or not q["paragraph"]:
|
| 316 |
return 0.0
|
| 317 |
|
| 318 |
+
words = q["paragraph"].lower().split()
|
| 319 |
word_count = len(words)
|
| 320 |
p_cfg = self.distractor_cfg["paragraph"]
|
| 321 |
|
| 322 |
# Length score
|
| 323 |
+
if q["type"] == QuestionTypeEnum.VOCAB:
|
| 324 |
thresholds = p_cfg["vocab_length_thresholds"]
|
| 325 |
scores = [0.2, 0.3, 0.4, 0.5]
|
| 326 |
else:
|
|
|
|
| 334 |
break
|
| 335 |
|
| 336 |
# Difficulty score
|
| 337 |
+
doc = self.nlp(q["paragraph"])
|
| 338 |
sentences = [sent.text.strip() for sent in doc.sents if sent.text.strip()]
|
| 339 |
if not sentences:
|
| 340 |
return length_score * p_cfg["length_weight"]
|