Source code

Download source archive ↓

src/rdflib/void.py

1from __future__ import annotations
2
3import collections
4from typing import DefaultDict, Dict, Optional, Set
5
6from rdflib.graph import Graph, _ObjectType, _PredicateType, _SubjectType
7from rdflib.namespace import RDF, VOID
8from rdflib.term import IdentifiedNode, Literal, URIRef
9
10
11def generateVoID(  # noqa: N802
12    g: Graph,
13    dataset: Optional[IdentifiedNode] = None,
14    res: Optional[Graph] = None,
15    distinctForPartitions: bool = True,  # noqa: N803
16):
17    """Returns a new graph with a VoID description of the passed dataset
18
19    For more info on Vocabulary of Interlinked Datasets (VoID), see:
20    http://vocab.deri.ie/void
21
22    This only makes two passes through the triples (once to detect the types
23    of things)
24
25    The tradeoff is that lots of temporary structures are built up in memory
26    meaning lots of memory may be consumed :)
27    I imagine at least a few copies of your original graph.
28
29    the distinctForPartitions parameter controls whether
30    distinctSubjects/objects are tracked for each class/propertyPartition
31    this requires more memory again
32    """
33
34    typeMap: Dict[_SubjectType, Set[_SubjectType]] = (  # noqa: N806
35        collections.defaultdict(set)
36    )
37    classes: Dict[_ObjectType, Set[_SubjectType]] = collections.defaultdict(set)
38    for e, c in g.subject_objects(RDF.type):
39        classes[c].add(e)
40        typeMap[e].add(c)
41
42    triples = 0
43    subjects: Set[_SubjectType] = set()
44    objects: Set[_ObjectType] = set()
45    properties: Set[_PredicateType] = set()
46    classCount: DefaultDict[_SubjectType, int] = collections.defaultdict(  # noqa: N806
47        int
48    )
49    propCount: DefaultDict[_PredicateType, int] = collections.defaultdict(  # noqa: N806
50        int
51    )
52
53    classProps = collections.defaultdict(set)  # noqa: N806
54    classObjects = collections.defaultdict(set)  # noqa: N806
55    propSubjects = collections.defaultdict(set)  # noqa: N806
56    propObjects = collections.defaultdict(set)  # noqa: N806
57
58    for s, p, o in g:
59        triples += 1
60        subjects.add(s)
61        properties.add(p)
62        objects.add(o)
63
64        # class partitions
65        if s in typeMap:
66            for c in typeMap[s]:
67                classCount[c] += 1
68                if distinctForPartitions:
69                    classObjects[c].add(o)
70                    classProps[c].add(p)
71
72        # property partitions
73        propCount[p] += 1
74        if distinctForPartitions:
75            propObjects[p].add(o)
76            propSubjects[p].add(s)
77
78    if not dataset:
79        dataset = URIRef("http://example.org/Dataset")
80
81    if not res:
82        res = Graph()
83
84    res.add((dataset, RDF.type, VOID.Dataset))
85
86    # basic stats
87    res.add((dataset, VOID.triples, Literal(triples)))
88    res.add((dataset, VOID.classes, Literal(len(classes))))
89
90    res.add((dataset, VOID.distinctObjects, Literal(len(objects))))
91    res.add((dataset, VOID.distinctSubjects, Literal(len(subjects))))
92    res.add((dataset, VOID.properties, Literal(len(properties))))
93
94    for i, c in enumerate(classes):
95        part = URIRef(dataset + "_class%d" % i)
96        res.add((dataset, VOID.classPartition, part))
97        res.add((part, RDF.type, VOID.Dataset))
98
99        res.add((part, VOID.triples, Literal(classCount[c])))
100        res.add((part, VOID.classes, Literal(1)))
101
102        res.add((part, VOID["class"], c))
103
104        res.add((part, VOID.entities, Literal(len(classes[c]))))
105        res.add((part, VOID.distinctSubjects, Literal(len(classes[c]))))
106
107        if distinctForPartitions:
108            res.add((part, VOID.properties, Literal(len(classProps[c]))))
109            res.add((part, VOID.distinctObjects, Literal(len(classObjects[c]))))
110
111    for i, p in enumerate(properties):
112        part = URIRef(dataset + "_property%d" % i)
113        res.add((dataset, VOID.propertyPartition, part))
114        res.add((part, RDF.type, VOID.Dataset))
115
116        res.add((part, VOID.triples, Literal(propCount[p])))
117        res.add((part, VOID.properties, Literal(1)))
118
119        res.add((part, VOID.property, p))
120
121        if distinctForPartitions:
122            entities = 0
123            propClasses = set()  # noqa: N806
124            for s in propSubjects[p]:
125                if s in typeMap:
126                    entities += 1
127                for c in typeMap[s]:
128                    propClasses.add(c)
129
130            res.add((part, VOID.entities, Literal(entities)))
131            res.add((part, VOID.classes, Literal(len(propClasses))))
132
133            res.add((part, VOID.distinctSubjects, Literal(len(propSubjects[p]))))
134            res.add((part, VOID.distinctObjects, Literal(len(propObjects[p]))))
135
136    return res, dataset