Skip to content
Snippets Groups Projects
Commit 2d8e8fbc authored by Eliott Sammier's avatar Eliott Sammier
Browse files

Parse TAT text and gaps

parent 1e270a00
No related branches found
No related tags found
1 merge request!5Resolve "Parseur par type d'activité"
......@@ -183,6 +183,14 @@ class ChoiceGroup:
self.items: list[Choice]
class Gap:
"""A gap in a gap-fill text exercise"""
def __init__(self, id: str):
self.id = id
self.choices: list[Choice] = []
class ExerciceQC(Exercice):
def __init__(self, is_qcm: bool = False) -> None:
super().__init__()
......@@ -275,8 +283,41 @@ class ExerciceQM(Exercice):
class ExerciceTAT(Exercice):
def __init__(self):
super().__init__()
self.text: str # can be HTML
self.gaps: list[ChoiceGroup]
self.segments: list[str | Gap] = []
@override
def parse_html(self, root: HtmlElement):
super().parse_html(root)
# Find the text container
try:
container = root.find_class("STY_texteTAT")[0]
except IndexError as e:
raise ParseError("ExerciceTAT: text container not found") from e
# Text buffer accumulates the text found
text_segment_buf = container.text or ""
for elem in container:
if elem.tag == "select" and "STY_selectTAT" in elem.classes:
# It's a gap
# Time to "close" the text segment and add it
self.segments.append(text_segment_buf)
# Add the gap
gap_id = elem.attrib["id"].replace("champTrou", "")
self.segments.append(Gap(gap_id))
# New text segment starts with the tail text of this element
text_segment_buf = elem.tail or ""
else:
text_segment_buf += to_html(elem)
self.segments.append(text_segment_buf)
nb_total_gaps = len(container.find_class("STY_selectTAT"))
nb_found_gaps = len([e for e in self.segments if isinstance(e, Gap)])
if nb_found_gaps != nb_total_gaps:
log.warning(
f"{self.id}: Text has {nb_total_gaps} gaps in total, but found {nb_found_gaps} gap elements, some might be missing"
)
pass
class ExerciceGD(Exercice):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment