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

Save TAT exercises to RDF, update ontology

parent 90495a98
Branches
No related tags found
1 merge request!5Resolve "Parseur par type d'activité"
......@@ -31,6 +31,12 @@
rdfs:range :Reponse .
### http://www.semanticweb.org/eliott/ontologies/2024/4/macao/aReponseIncorrecte
:aSegment rdf:type owl:ObjectProperty ;
rdfs:domain :ExerciceTAT ;
rdfs:range :Segment .
### http://www.semanticweb.org/eliott/ontologies/2024/4/macao/contenuDans
:contenuDans rdf:type owl:ObjectProperty ;
owl:inverseOf :contient .
......@@ -213,11 +219,21 @@
rdfs:subClassOf :MacaoContenu .
### http://www.semanticweb.org/eliott/ontologies/2024/4/macao/Reponse
:Champ rdf:type owl:Class ;
rdfs:subClassOf :MacaoContenu .
### http://www.semanticweb.org/eliott/ontologies/2024/4/macao/Reponse
:Reponse rdf:type owl:Class ;
rdfs:subClassOf :MacaoContenu .
### http://www.semanticweb.org/eliott/ontologies/2024/4/macao/Reponse
:Segment rdf:type owl:Class ;
rdfs:subClassOf :MacaoContenu .
### http://www.semanticweb.org/eliott/ontologies/2024/4/macao/SimpleFlash
:SimpleFlash rdf:type owl:Class ;
rdfs:subClassOf :FlashObject .
......
This diff is collapsed.
This diff is collapsed.
......@@ -163,6 +163,27 @@ class Choice:
"""A `Comment` associated with this choice, displayed when the exercise
is incorrect and this choice is selected"""
def save(self, graph: Graph, rdf_name: str) -> URIRef:
"""Save this choice to the RDF graph"""
display_name = rdf_name + " | " + ("V" if self.is_correct else "F")
choice_node = NS[rdf_name]
graph.add((choice_node, RDF.type, NS["Reponse"]))
graph.add((choice_node, NS["id"], Literal(self.id)))
graph.add((choice_node, NS["index"], Literal(self.index)))
graph.add((choice_node, NS["correct"], Literal(self.is_correct)))
graph.add((choice_node, NS["html"], Literal(self.html)))
# Save optional comment
if self.comment is not None:
graph.add((choice_node, NS["commentaireSugg"], Literal(self.comment.html)))
graph.add(
(
choice_node,
NS["__protege_display_name"],
Literal(display_name),
)
)
return choice_node
@dataclass
class ChoiceGroup:
......@@ -177,6 +198,13 @@ class Gap:
id: str
choices: list[Choice] = field(default_factory=list)
def save(self, graph: Graph, rdf_name: str):
"""Save this gap to the RDF graph"""
for choice in self.choices:
choice_uri = choice.save(graph, f"{rdf_name}_{choice.id}")
graph.add((NS[rdf_name], NS["aReponse"], choice_uri))
pass
@dataclass
class ExerciceQC(Exercice):
......@@ -221,26 +249,7 @@ class ExerciceQC(Exercice):
def save(self, graph: Graph):
super().save(graph)
for choice in self.choices.values():
rdf_name = f"{self.id}_{choice.id}" # ex: pg157_2, pg173_rep21
display_name = rdf_name + " | " + ("V" if choice.is_correct else "F")
choice_node = NS[rdf_name]
graph.add((choice_node, RDF.type, NS["Reponse"]))
graph.add((choice_node, NS["id"], Literal(choice.id)))
graph.add((choice_node, NS["index"], Literal(choice.index)))
graph.add((choice_node, NS["correct"], Literal(choice.is_correct)))
graph.add((choice_node, NS["html"], Literal(choice.html)))
# Save optional comment
if choice.comment is not None:
graph.add(
(choice_node, NS["commentaireSugg"], Literal(choice.comment.html))
)
graph.add(
(
choice_node,
NS["__protege_display_name"],
Literal(display_name),
)
)
choice_node = choice.save(graph, f"{self.id}_{choice.id}")
graph.add((NS[self.id], NS["aReponse"], choice_node))
# Our fake "class hierarchy" just for easier visualization
graph.add((choice_node, RDFS.subClassOf, NS[self.id]))
......@@ -296,6 +305,8 @@ class ExerciceTAT(Exercice):
else:
text_segment_buf += to_html(elem)
if text_segment_buf != "":
# Avoid adding an empty segment if there are none
self.segments.append(text_segment_buf)
nb_total_gaps = len(container.find_class("STY_selectTAT"))
......@@ -306,6 +317,23 @@ class ExerciceTAT(Exercice):
)
pass
@override
def save(self, graph: Graph):
super().save(graph)
for index, segment in enumerate(self.segments):
rdf_name = f"{self.id}_seg{index}"
segment_uri = NS[rdf_name]
# The Segment has to be an object, even text segments, because
# we also need an `index` property since RDF is unordered.$
graph.add((NS[self.id], NS["aSegment"], segment_uri))
graph.add((segment_uri, RDF.type, NS["Segment"]))
graph.add((segment_uri, NS["index"], Literal(index)))
if isinstance(segment, str):
graph.add((segment_uri, NS["text"], Literal(segment)))
else:
graph.add((segment_uri, RDF.type, NS["Champ"]))
segment.save(graph, rdf_name)
def get_or_create_gap(self, gap_id: str) -> Gap:
"""Find a gap by ID, creating it if needed"""
if gap_id not in self.gaps:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment