-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
184 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
from .filesystem import export_dir | ||
from rdflib import Literal, RDF, URIRef, Namespace, Graph | ||
from rdflib.namespace import DC, RDFS, OWL, SKOS | ||
|
||
|
||
verb_mapping = { | ||
"exact": OWL.sameAs, | ||
"approximate": SKOS.related, | ||
"narrower": SKOS.narrower, | ||
"broader": SKOS.broader, | ||
} | ||
|
||
|
||
def write_matching_to_rdf(data, format="turtle", extension="ttl"): | ||
g = Graph() | ||
|
||
olca = Namespace("http://greendelta.github.io/olca-schema/context.jsonld#") | ||
|
||
g.bind("olca", "http://greendelta.github.io/olca-schema/context.jsonld") | ||
g.bind("dc", DC) | ||
g.bind("owl", OWL) | ||
g.bind("skos", SKOS) | ||
|
||
olca_object = olca.Flow if data["catalog"] == "gs1" else olca.Process | ||
|
||
# Start by describing what we are linking against (only those elements used) | ||
node_dict = {} | ||
for key in (key for key in data if key.startswith("row-")): | ||
for o in data[key]["matches"]: | ||
match = o["data"] | ||
if match["code"] not in node_dict: | ||
uri = "http://perdu.data/{}/{}".format(data["catalog"], match["code"]) | ||
node = URIRef(uri) | ||
g.add((node, RDF.type, olca_object)) | ||
g.add((node, DC.title, Literal(match["name"]))) | ||
g.add((node, RDFS.label, Literal(match["name"]))) | ||
g.add((node, DC.description, Literal(match["description"]))) | ||
node_dict[match["code"]] = node | ||
|
||
# Now describe our links | ||
for key in (key for key in data if key.startswith("row-")): | ||
uri = "http://perdu.data/source/{}".format(data["hash"]) | ||
node = URIRef(uri) | ||
g.add((node, RDF.type, olca.Flow)) | ||
g.add((node, RDFS.label, Literal(data[key]["source"]))) | ||
g.add((node, DC.publisher, Literal("perdu.data"))) | ||
g.add((node, DC.creator, URIRef("https://github.com/cmutel/perdu"))) | ||
for match in data[key]["matches"]: | ||
g.add( | ||
(node, verb_mapping[match["method"]], node_dict[match["data"]["code"]]) | ||
) | ||
|
||
fp = export_dir / "{}.{}.{}".format(data["hash"], data["catalog"], extension) | ||
if fp.is_file(): | ||
fp.unlink() | ||
|
||
with open(fp, "wb") as f: | ||
g.serialize(f, format=format, encoding="utf-8") | ||
return fp |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
test_data = { | ||
"row-0": { | ||
"matches": [ | ||
{ | ||
"data": { | ||
"code": "336612", | ||
"description": "This U.S. industry comprises establishments primarily engaged in building boats.", | ||
"name": "Boat Building", | ||
"score": 14.394085818147092, | ||
}, | ||
"method": "broader", | ||
}, | ||
{ | ||
"data": { | ||
"code": "327910", | ||
"description": "This industry comprises establishments primarily engaged in manufacturing abrasive produts", | ||
"name": "Abrasive Product Manufacturing", | ||
"score": 9.026339256926837, | ||
}, | ||
"method": "exact", | ||
}, | ||
], | ||
"source": "Plastic polytetrafluoroethylene race liner", | ||
}, | ||
"catalog": "USEEIO", | ||
"hash": "a90895a4c0e1b6f9e19e59555fb231b3593b9d3890a7d2520248760c43a9ed73", | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from fixtures.match_fixture import test_data | ||
from pathlib import Path | ||
from perdu.semantic_web import write_matching_to_rdf | ||
import tempfile | ||
|
||
|
||
def test_ttl_export(monkeypatch): | ||
with tempfile.TemporaryDirectory() as td: | ||
monkeypatch.setattr("perdu.semantic_web.export_dir", Path(td)) | ||
write_matching_to_rdf(test_data) | ||
|
||
|
||
def test_jsonld_export(monkeypatch): | ||
with tempfile.TemporaryDirectory() as td: | ||
monkeypatch.setattr("perdu.semantic_web.export_dir", Path(td)) | ||
write_matching_to_rdf(test_data, "json-ld", "json") |