Skip to content

Commit

Permalink
➕ extracting multiple correct answers from hints; 🔧 var type & lint
Browse files Browse the repository at this point in the history
  • Loading branch information
gy-mate committed Jul 20, 2024
1 parent 3335c02 commit 9dc7a52
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions src/moodle_to_vikwikiquiz/quiz_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,20 +180,34 @@ def prettify(text: str) -> str:


def strip_whitespaces(text: str) -> str:
text = text.strip(".\n")
text = re.sub(r"\r\n|\s{2}", " ", text)
text = text.strip("., \n")
text = re.sub(r" \n|\r\n|\s{2}", " ", text)
return text


def get_correct_answers_if_provided(question: Tag) -> set[str]:
def get_correct_answers_if_provided(question: Tag) -> set[str | None]:
tag = question.find("div", class_="rightanswer")
correct_answers: set[str] = set()
correct_answers: set[str | None] = set()
if tag:
hint_text = tag.text
if correct_answer := re.findall(r"(?<=The correct answer is: ).+", hint_text):
assert correct_answer
prettified_answer = prettify(correct_answer[0])
assert isinstance(tag, Tag)
hint_text = prettify(tag.text)
if only_correct_answer := re.findall(
r"(?<=The correct answer is: ).+", hint_text
):
assert only_correct_answer
prettified_answer = prettify(only_correct_answer[0])
correct_answers.add(prettified_answer)
elif hint_text.find("The correct answers are: ") != -1:
correct_answer_tags = tag.find_all("p")
for correct_answer_tag in correct_answer_tags:
correct_answer = correct_answer_tag.text
correct_answers.add(prettify(correct_answer))
elif tag.find("img"):
pass
else:
raise NotImplementedError(
f"Correct answers could not be extracted from '{hint_text}'!"
)
return correct_answers


Expand All @@ -202,7 +216,7 @@ def answer_is_correct(
answer_text: str,
grade: float,
maximum_points: float,
correct_answers: set[str],
correct_answers: set[str | None],
) -> bool:
if correct_answers and answer_text in correct_answers:
return True
Expand Down

0 comments on commit 9dc7a52

Please sign in to comment.