Skip to content

Rdf Graph


This module provides functionality to convert video annotations into JSON-LD format. It includes the following main functions:

  1. annotations_to_jsonLD(annotations, isAutomatic: bool):

    • Converts video annotations into a JSON-LD graph.
    • Binds various namespaces to the RDF graph.
    • Adds nodes and triples for video, concepts, and prerequisites.
    • Serializes the RDF graph into JSON-LD format.
    • Compacts the JSON-LD using a provided context.
    • Returns the RDF graph and the compacted JSON-LD data.
  2. graph_to_rdf(jsonld):

    • Converts JSON-LD data into an RDF graph.

annotations_to_jsonLD(annotations, isAutomatic)

Converts video annotations into a JSON-LD graph.

Parameters:

Name Type Description Default
annotations dict

A dictionary containing video annotations.

required
isAutomatic bool

A flag indicating whether the annotations are automatic.

required

Returns:

Name Type Description
g Graph

The RDF graph containing the annotations.

data dict

The compacted JSON-LD data.

Examples:

>>> annotations = {
...     "id": "video1",
...     "definitions": [{"concept": "example", "description_type": "type", "start": "2023-01-01T00:00:00", "start_sent_id": "1", "end": "2023-01-01T00:01:00", "end_sent_id": "2"}],
...     "relations": [{"target": "example", "prerequisite": "example2", "time": "2023-01-01T00:00:00", "xywh": "None", "sent_id": "1", "word_id": "None"}],
...     "annotator": "http://example.org/annotator"
... }
>>> g, data = annotations_to_jsonLD(annotations, isAutomatic=False)
Source code in apps/annotator/code/ontology/rdf_graph.py
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
def annotations_to_jsonLD(annotations, isAutomatic:bool):
    """
    Converts video annotations into a JSON-LD graph.

    Parameters
    ----------
    annotations : dict
        A dictionary containing video annotations.
    isAutomatic : bool
        A flag indicating whether the annotations are automatic.

    Returns
    -------
    g : rdflib.Graph
        The RDF graph containing the annotations.
    data : dict
        The compacted JSON-LD data.

    Examples
    --------
    >>> annotations = {
    ...     "id": "video1",
    ...     "definitions": [{"concept": "example", "description_type": "type", "start": "2023-01-01T00:00:00", "start_sent_id": "1", "end": "2023-01-01T00:01:00", "end_sent_id": "2"}],
    ...     "relations": [{"target": "example", "prerequisite": "example2", "time": "2023-01-01T00:00:00", "xywh": "None", "sent_id": "1", "word_id": "None"}],
    ...     "annotator": "http://example.org/annotator"
    ... }
    >>> g, data = annotations_to_jsonLD(annotations, isAutomatic=False)
    """

    print("***** EKEEL - Video Annotation: ontology.py::to_jsonLD(): Inizio ******")

    concepts_anno = []
    prereq_anno = []

    if "definitions" in annotations:
        concepts_anno = annotations["definitions"]
    if "relations" in annotations:    
        prereq_anno = annotations["relations"]
    video_id = annotations["id"]

    if not isAutomatic:
        creator = annotations["annotator"]
        creator = URIRef(creator)

    g = Graph()

    g.bind("oa", oa)
    g.bind("dctypes", dctypes)
    g.bind("edu", edu)
    g.bind("SKOS", SKOS)
    g.bind("dcterms", dcterms)


    # aggiunge un nodo
    video = URIRef("video_" + str(video_id))
    #video = URIRef(edurell + "video_" + str(video_id))
    g.add((video, RDF.type, dctypes.MovingImage))

    conll = URIRef("conll_" + str(video_id))
    #conll = URIRef(edurell + "conll_" + str(video_id))
    g.add((conll, RDF.type, dctypes.Text))


    # collegamento video conll
    ann_linking_conll = URIRef("ann0")
    #ann_linking_conll = URIRef(edurell + "ann0")
    g.add((ann_linking_conll, RDF.type, oa.Annotation))
    g.add((ann_linking_conll, oa.motivatedBy, edu.linkingConll))
    g.add((ann_linking_conll, oa.hasBody, conll))
    g.add((ann_linking_conll, oa.hasTarget, video))

    date = Literal(datetime.now())

    #creo il nuovo nodo dei concetti
    #localVocabulary = URIRef("localVocabulary")
    #g.add((localVocabulary, RDF.type, SKOS.Collection))


    # per ogni annotazione di concetto spiegato aggiungo le triple
    for i, annotation in enumerate(concepts_anno):
        ann = URIRef("ann" + str(i + 1))

        g.add((ann, RDF.type, oa.Annotation))

        if isAutomatic:
            g.add((ann, dcterms.creator, URIRef(annotation["creator"])))
        else:
            g.add((ann, dcterms.creator, creator))

        g.add((ann, dcterms.created, date))
        g.add((ann, oa.motivatedBy, oa.describing))
        g.add((ann, SKOS.note, Literal("concept"+annotation["description_type"])))


        concept = URIRef("concept_" + annotation["concept"].replace(" ", "_"))

        #crea un nodo concetto

        #g.add((localVocabulary, SKOS.member, concept))
        #g.add((concept, RDF.type, SKOS.Concept))


        g.add((ann, oa.hasBody, concept))

        blank_target = BNode()


        blank_selector = BNode()

        g.add((ann, oa.hasTarget, blank_target))
        g.add((blank_target, RDF.type, oa.SpecificResource))

        g.add((blank_target, oa.hasSelector, blank_selector))
        g.add((blank_selector, RDF.type, oa.RangeSelector))

        g.add((blank_target, oa.hasSource, video))

        blank_startSelector = BNode()
        blank_endSelector = BNode()

        g.add((blank_startSelector, RDF.type, edu.InstantSelector))
        g.add((blank_endSelector, RDF.type, edu.InstantSelector))

        g.add((blank_selector, oa.hasStartSelector, blank_startSelector))
        g.add((blank_selector, oa.hasEndSelector, blank_endSelector))

        g.add((blank_startSelector, RDF.value, Literal(annotation["start"] + "^^xsd:dateTime")))
        g.add((blank_startSelector, edu.conllSentId, Literal(annotation["start_sent_id"])))
        #g.add((blank_startSelector, edu.conllWordId, Literal(annotation["word_id"])))

        g.add((blank_endSelector, RDF.value, Literal(annotation["end"] + "^^xsd:dateTime")))
        g.add((blank_endSelector, edu.conllSentId, Literal(annotation["end_sent_id"])))


    num_definitions = len(concepts_anno) + 1

    # per ogni annotazione di prerequisito aggiungo le triple
    for i, annotation in enumerate(prereq_anno):
        ann = URIRef("ann" + str(num_definitions + i))

        target_concept = URIRef("concept_" +  annotation["target"].replace(" ", "_"))
        prereq_concept = URIRef("concept_" +  annotation["prerequisite"].replace(" ", "_"))

        #g.add((target_concept, RDF.type, SKOS.Concept))
        #g.add((prereq_concept, RDF.type, SKOS.Concept))

        g.add((ann, RDF.type, oa.Annotation))

        if isAutomatic:
            g.add((ann, dcterms.creator, URIRef(annotation["creator"])))
        else:
            g.add((ann, dcterms.creator, creator))

        g.add((ann, dcterms.created, date))
        g.add((ann, oa.motivatedBy, edu.linkingPrerequisite))

        g.add((ann, oa.hasBody, prereq_concept))
        g.add((ann, SKOS.note, Literal(annotation["weight"] + "Prerequisite")))

        blank_target = BNode()

        g.add((ann, oa.hasTarget, blank_target))
        g.add((blank_target, RDF.type, oa.SpecificResource))
        g.add((blank_target, dcterms.subject, target_concept))

        g.add((blank_target, oa.hasSource, video))

        blank_selector_video = BNode()

        g.add((blank_target, oa.hasSelector, blank_selector_video))
        g.add((blank_selector_video, RDF.type, edu.InstantSelector))
        g.add((blank_selector_video, RDF.value, Literal(annotation["time"] + "^^xsd:dateTime")))

        if annotation["xywh"] != "None":
            g.add((blank_selector_video, edu.hasMediaFrag, Literal(annotation["xywh"])))


        g.add((blank_selector_video, edu.conllSentId, Literal(annotation["sent_id"])))

        if annotation["word_id"] != "None":
            g.add((blank_selector_video, edu.conllWordId, Literal(annotation["word_id"])))

    # stampo il grafo in formato turtle
    # turtle = g.serialize(format='turtle').decode("utf-8")
    # print(turtle)

    # creo file json-ld

    #jsonld = json.loads(g.serialize(format='json-ld', context=context))

    jsonld = json.loads(g.serialize(format='json-ld'))
    jsonld = pyld.jsonld.compact(jsonld, context)

    '''
    Nested nodes creation

    FROM:                            |  TO:
    {                                |  {
        ...                          |    ...
        "target": 123                |     "target":{
    },                               |                 "id":123,
    {                                |                  "type":example
        "id": 123,                   |               }
        "type": example              |   }
    }

    '''

    for o in jsonld["@graph"]:
        if "target" in o:
            for i, t in enumerate(jsonld["@graph"]):
                if o["motivation"] != "edu:linkingConll" and o["target"] == t["id"]:
                    o["target"] = t
                    del jsonld["@graph"][i]
                    for j, s in enumerate(jsonld["@graph"]):
                        if o["target"]["selector"] == s["id"]:
                            o["target"]["selector"] = s
                            del jsonld["@graph"][j]

                            if o["motivation"] == "describing":
                                for k, p in enumerate(jsonld["@graph"]):
                                    if o["target"]["selector"]["startSelector"] == p["id"]:
                                        o["target"]["selector"]["startSelector"] = p
                                        del jsonld["@graph"][k]
                                        break

                                for k, p in enumerate(jsonld["@graph"]):
                                    if o["target"]["selector"]["endSelector"] == p["id"]:
                                        o["target"]["selector"]["endSelector"] = p
                                        del jsonld["@graph"][k]
                                        break

    #print(jsonld)
    data = {
        "graph":jsonld
    }

    #print(data)
    print("***** EKEEL - Video Annotation: ontology.py::to_jsonLD(): Fine ******")

    return g, data

graph_to_rdf(jsonld)

Converts JSON-LD data into an RDF graph.

Parameters:

Name Type Description Default
jsonld dict

A dictionary containing JSON-LD data.

required

Returns:

Name Type Description
g Graph

The RDF graph parsed from the JSON-LD data.

Examples:

>>> jsonld = {
...     "@context": "http://schema.org",
...     "@type": "Person",
...     "name": "Jane Doe"
... }
>>> g = graph_to_rdf(jsonld)
Source code in apps/annotator/code/ontology/rdf_graph.py
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
def graph_to_rdf(jsonld):
    """
    Converts JSON-LD data into an RDF graph.

    Parameters
    ----------
    jsonld : dict
        A dictionary containing JSON-LD data.

    Returns
    -------
    g : rdflib.Graph
        The RDF graph parsed from the JSON-LD data.

    Examples
    --------
    >>> jsonld = {
    ...     "@context": "http://schema.org",
    ...     "@type": "Person",
    ...     "name": "Jane Doe"
    ... }
    >>> g = graph_to_rdf(jsonld)
    """
    json_expanded = pyld.jsonld.expand(jsonld)

    return Graph().parse(data=json.dumps(json_expanded), format='json-ld')