Skip to content

Commit

Permalink
Merge pull request #169 from Ankush-Chander/multifile-support
Browse files Browse the repository at this point in the history
add multifile support for load_jsonld
  • Loading branch information
ceteri committed Apr 24, 2021
2 parents 7ac619d + dfe3f6a commit 061723b
Show file tree
Hide file tree
Showing 4 changed files with 310 additions and 1 deletion.
106 changes: 106 additions & 0 deletions dat/gorm.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
{
"@context": {
"@language": "en",
"dct": "http://purl.org/dc/terms/",
"owl": "http://www.w3.org/2002/07/owl#",
"prov": "http://www.w3.org/ns/prov#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"schema": "http://schema.org/",
"sh": "http://www.w3.org/ns/shacl#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"xsd": "http://www.w3.org/2001/XMLSchema#"
},
"@graph": [
{
"@id": "http://example.org/sagas#Astrid",
"@type": "http://example.org/sagas#Viking",
"http://example.org/sagas#childOf": [
{
"@id": "http://example.org/sagas#Leif"
},
{
"@id": "http://example.org/sagas#Bodil"
}
],
"http://xmlns.com/foaf/0.1/topic_interest": {
"@id": "http://example.org/sagas#Fighting"
}
},
{
"@id": "http://example.org/sagas#Bodil",
"@type": "http://example.org/sagas#Viking",
"http://example.org/sagas#spouseOf": {
"@id": "http://example.org/sagas#Leif"
}
},
{
"@id": "http://example.org/sagas#Leif",
"@type": "http://example.org/sagas#Viking",
"http://example.org/sagas#childOf": {
"@id": "http://example.org/sagas#Bjorn"
}
},
{
"@id": "http://example.org/sagas#Viking",
"@type": "http://xmlns.com/foaf/0.1/Person"
},
{
"@id": "http://example.org/sagas#Fighting",
"@type": "http://xmlns.com/foaf/0.1/Thing"
},
{
"@id": "http://example.org/sagas#childOf",
"owl:inverseOf": {
"@id": "http://example.org/sagas#ancestorOf"
},
"rdfs:domain": {
"@id": "http://example.org/sagas#Viking"
},
"rdfs:range": {
"@id": "http://example.org/sagas#Viking"
}
},
{
"@id": "http://example.org/sagas#Bjorn",
"@type": "http://example.org/sagas#Viking",
"http://example.org/sagas#childOf": {
"@id": "http://example.org/sagas#Gorm"
},
"http://xmlns.com/foaf/0.1/topic_interest": {
"@id": "http://example.org/sagas#Pilaging"
}
},
{
"@id": "http://example.org/sagas#spouseOf",
"@type": "owl:SymmetricProperty",
"rdfs:domain": {
"@id": "http://example.org/sagas#Viking"
},
"rdfs:range": {
"@id": "http://example.org/sagas#Viking"
}
},
{
"@id": "http://example.org/sagas#Gorm",
"@type": "http://example.org/sagas#Viking",
"http://xmlns.com/foaf/0.1/topic_interest": {
"@id": "http://example.org/sagas#Berserkr"
}
},
{
"@id": "http://example.org/sagas#Pilaging",
"@type": "http://xmlns.com/foaf/0.1/Thing",
"skos:broader": {
"@id": "http://example.org/sagas#Fighting"
}
},
{
"@id": "http://example.org/sagas#Berserkr",
"@type": "http://xmlns.com/foaf/0.1/Thing",
"skos:broader": {
"@id": "http://example.org/sagas#Fighting"
}
}
]
}
162 changes: 162 additions & 0 deletions dat/nom.jsonld
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
{
"@context": {
"@language": "en",
"dct": "http://purl.org/dc/terms/",
"owl": "http://www.w3.org/2002/07/owl#",
"prov": "http://www.w3.org/ns/prov#",
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"schema": "http://schema.org/",
"sh": "http://www.w3.org/ns/shacl#",
"skos": "http://www.w3.org/2004/02/skos/core#",
"xsd": "http://www.w3.org/2001/XMLSchema#"
},
"@graph": [
{
"@id": "http://example.org/#Pancake",
"@type": "http://example.org/#Component",
"dct:identifier": {
"@id": "http://www.wikidata.org/entity/Q44541"
},
"http://example.org/#madeFrom": {
"@id": "http://example.org/#Batter"
},
"skos:altLabel": {
"@language": "fr",
"@value": "Crêpe"
},
"skos:closeMatch": {
"@id": "http://www.wikidata.org/entity/Q12200"
},
"skos:definition": "A thin, round cake made from a batter of eggs, milk, and flour.",
"skos:narrower": [
{
"@id": "http://www.wikidata.org/entity/Q815898"
},
{
"@id": "http://www.wikidata.org/entity/Q899392"
},
{
"@id": "http://www.wikidata.org/entity/Q1025010"
}
],
"skos:prefLabel": "Pancake"
},
{
"@id": "http://example.org/#NOM_Vocab",
"@type": "skos:ConceptScheme",
"dct:publisher": {
"@id": "https://derwen.ai/"
},
"skos:definition": "A mid-level SKOS/OWL ontology used in the `kglab` examples",
"skos:prefLabel": "NOM Example Vocabulary"
},
{
"@id": "http://example.org/#Component",
"@type": "skos:Concept",
"dct:identifier": {
"@id": "http://www.wikidata.org/entity/Q746549"
},
"skos:altLabel": "Dish",
"skos:definition": "A specific prepared food which is ready to eat or used in building another dish.",
"skos:prefLabel": "Component"
},
{
"@id": "http://example.org/#Mixing",
"@type": "http://example.org/#Component",
"skos:closeMatch": {
"@id": "https://www.foodonline.com/solution/mixing-and-blending"
},
"skos:definition": "A process of combining two or more separate components to produce a certain level of homogeneity.",
"skos:prefLabel": "Mixing"
},
{
"@id": "http://example.org/#Dough",
"@type": "http://example.org/#Component",
"dct:identifier": {
"@id": "http://www.wikidata.org/entity/Q178024"
},
"http://example.org/#usesProcess": {
"@id": "http://example.org/#Kneading"
},
"skos:definition": "A flour-based paste used to prepare food.",
"skos:prefLabel": "Dough"
},
{
"@id": "http://example.org/#usesProcess",
"rdfs:domain": {
"@id": "http://example.org/#Component"
},
"rdfs:range": {
"@id": "http://example.org/#Process"
}
},
{
"@id": "http://example.org/#madeFrom",
"rdfs:domain": {
"@id": "http://example.org/#Component"
},
"rdfs:range": {
"@id": "http://example.org/#Component"
}
},
{
"@id": "http://example.org/#Noodle",
"@type": "http://example.org/#Component",
"dct:identifier": {
"@id": "http://www.wikidata.org/entity/Q192874"
},
"http://example.org/#madeFrom": {
"@id": "http://example.org/#Dough"
},
"skos:altLabel": "Dumpling",
"skos:closeMatch": {
"@id": "http://www.wikidata.org/entity/Q1854639"
},
"skos:definition": "A type of staple food made from some type of unleavened dough.",
"skos:narrower": [
{
"@id": "http://www.wikidata.org/entity/Q20065"
},
{
"@id": "http://www.wikidata.org/entity/Q178"
}
],
"skos:prefLabel": "Noodle"
},
{
"@id": "http://example.org/#Batter",
"@type": "http://example.org/#Component",
"dct:identifier": {
"@id": "http://www.wikidata.org/entity/Q29493"
},
"http://example.org/#usesProcess": {
"@id": "http://example.org/#Mixing"
},
"skos:definition": "A flour-based mixture used to prepare food.",
"skos:prefLabel": "Batter"
},
{
"@id": "http://example.org/#Process",
"@type": "skos:Concept",
"dct:identifier": {
"@id": "http://www.wikidata.org/entity/Q627371"
},
"skos:altLabel": [
"Food Processing",
"Cooking"
],
"skos:definition": "The transformation of raw ingredients into food, or of food into other forms.",
"skos:prefLabel": "Process"
},
{
"@id": "http://example.org/#Kneading",
"@type": "http://example.org/#Component",
"dct:identifier": {
"@id": "http://www.wikidata.org/entity/Q3089784"
},
"skos:definition": "A process in the making of bread or dough, used to mix the ingredients and add strength to the final product.",
"skos:prefLabel": "Kneading"
}
]
}
2 changes: 1 addition & 1 deletion kglab/kglab.py
Original file line number Diff line number Diff line change
Expand Up @@ -668,7 +668,7 @@ def save_rdf_text (
**args,
).decode(encoding)


@multifile()
def load_jsonld (
self,
path: IOPathLike,
Expand Down
41 changes: 41 additions & 0 deletions test.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,5 +191,46 @@ def test_multiple_file_load_rdf(self):
assert multifile_node_count == sequential_node_count


def test_multiple_file_load_jsonld(self):
"""
Coverage:
* KnowledgeGraph.load_jsonld() load jsonld from multiple files using a wildcard expression
"""
# create a KnowledgeGraph object
with warnings.catch_warnings():
warnings.filterwarnings("ignore", message="unclosed file*")
kg = kglab.KnowledgeGraph()

# load json-ld from a file1 into KG
kg.load_jsonld("dat/gorm.jsonld")

# load json-ld from a file2 into KG
kg.load_jsonld("dat/nom.jsonld")

measure = kglab.Measure()
measure.measure_graph(kg)
sequential_edge_count = measure.get_edge_count()
sequential_node_count = measure.get_node_count()
# ic(sequential_edge_count)
# ic(sequential_node_count)

# load jsonld from all files (file1 and file2) matching the
# expression into KG
kg_multifile = kglab.KnowledgeGraph()
kg_multifile.load_jsonld("dat/*m.jsonld")

measure.reset()
measure.measure_graph(kg_multifile)
multifile_edge_count = measure.get_edge_count()
multifile_node_count = measure.get_node_count()

# ic(multifile_edge_count)
# ic(multifile_node_count)
assert multifile_edge_count == sequential_edge_count
assert multifile_node_count == sequential_node_count



if __name__ == "__main__":
unittest.main()

0 comments on commit 061723b

Please sign in to comment.