Skip to content

Instantly share code, notes, and snippets.

@RickMoynihan
Last active November 8, 2018 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RickMoynihan/ac5bbb91da07668d3e95ee19daacebb2 to your computer and use it in GitHub Desktop.
Save RickMoynihan/ac5bbb91da07668d3e95ee19daacebb2 to your computer and use it in GitHub Desktop.
Prettifies an RDF file by applying prefixes. Can also convert from one format to another
#!/usr/bin/env bash
":";grafter='grafter {:mvn/version "0.11.5"}'
":";cli='org.clojure/tools.cli {:mvn/version "0.3.7"}'
"exec" "clojure" "-Sdeps" "{:deps {$grafter $cli}}" "$0" "$@"
(require '[grafter.rdf :as rdf])
(require '[grafter.rdf.io :as rio])
(require '[clojure.java.io :as io])
(defn write! [outfile statements]
(rdf/add (rio/rdf-serializer (io/file outfile) :prefixes (assoc rio/default-prefixes
"pmdds" "http://publishmydata.com/def/dataset#"
"pmdf" "http://publishmydata.com/def/ontology/folder/"
"csvw" "http://www.w3.org/ns/csvw#"))
statements))
(defn prettify [infile outfile]
(->> infile
io/file
rdf/statements
(write! outfile)))
(prettify (first *command-line-args*) (second *command-line-args*))
@RickMoynihan
Copy link
Author

RickMoynihan commented Nov 8, 2018

Turns ugly linked data like this:

<http://gss-data.org.uk/data/ons-abs> a <http://publishmydata.com/def/dataset#Dataset> , <http://purl.org/linked-data/cube#DataSet> , <http://publishmydata.com/def/dataset#LinkedDataset> , <http://gss-data.org.uk/def/gdp#TradeDataset> ;
    <http://purl.org/dc/terms/modified> "2018-11-07T11:50:08.752Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> ;
    <http://purl.org/dc/terms/creator> <https://www.gov.uk/government/organisations/office-for-national-statistics> ;
    <http://www.w3.org/2000/01/rdf-schema#label> "ONS ABS"@en ;
    <http://www.w3.org/2000/01/rdf-schema#comment> "Annual Business Survey - Businesses involved in international trade - importing and/or exporting." ;
    <http://purl.org/dc/terms/issued> "2017-11-09T09:30:00Z"^^<http://www.w3.org/2001/XMLSchema#dateTime> ;
    <http://purl.org/dc/terms/title> "Annual Business Survey importers and exporters."@en ;
    <http://publishmydata.com/def/ontology/folder/inFolder> <http://gss-data.org.uk/def/concept/folders/themes/trade> ;
    <http://purl.org/dc/terms/publisher> <https://www.gov.uk/government/organisations/office-for-national-statistics> ;
    <http://purl.org/dc/terms/references> <http://gss-data.org.uk/help> ;
    <http://purl.org/dc/terms/license> <http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/> ;
    <http://publishmydata.com/def/dataset#contactEmail> <mailto:abs@ons.gsi.gov.uk> ;
    <http://publishmydata.com/def/dataset#graph> <http://gss-data.org.uk/graph/ons-abs> ;
    <http://rdfs.org/ns/void#sparqlEndpoint> <http://gss-data.org.uk/sparql> ;
    <http://gss-data.org.uk/def/gdp#family> <http://gss-data.org.uk/def/gdp#trade> ;
    a <http://purl.org/linked-data/cube#DataSet> , <http://www.w3.org/ns/csvw#Table> ;
    <http://www.w3.org/2000/01/rdf-schema#label> "ONS ABS"@en ;
    <http://purl.org/dc/terms/title> "ONS ABS"@en ;
    <http://purl.org/linked-data/cube#structure> <http://gss-data.org.uk/data/ons-abs/structure> ;
    <http://www.w3.org/ns/csvw#url> <file:/tmp/component-specifications8832067094575582985.csv> ;
    <http://www.w3.org/ns/csvw#row> _:genid-8a7b9fe234ee411b90095b629ae74236-B71059D58A85ED87B9BF41A694E7DB37  .

Into prettified prefixed output like this:

@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix void: <http://rdfs.org/ns/void#> .
@prefix dcterms: <http://purl.org/dc/terms/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix pmdds: <http://publishmydata.com/def/dataset#> .
@prefix dcat: <http://www.w3.org/ns/dcat#> .
@prefix csvw: <http://www.w3.org/ns/csvw#> .
@prefix pmdf: <http://publishmydata.com/def/ontology/folder/> .
@prefix sdmx-dimension: <http://purl.org/linked-data/sdmx/2009/dimension#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sdmx-attribute: <http://purl.org/linked-data/sdmx/2009/attribute#> .
@prefix qb: <http://purl.org/linked-data/cube#> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
@prefix sdmx-concept: <http://purl.org/linked-data/sdmx/2009/concept#> .


<http://gss-data.org.uk/data/ons-abs> a pmdds:Dataset , qb:DataSet , pmdds:LinkedDataset , <http://gss-data.org.uk/def/gdp#TradeDataset> ;
        dcterms:modified "2018-11-07T11:50:08.752Z"^^xsd:dateTime ;
        dcterms:creator <https://www.gov.uk/government/organisations/office-for-national-statistics> ;
        rdfs:label "ONS ABS"@en ;
        rdfs:comment "Annual Business Survey - Businesses involved in international trade - importing and/or exporting." ;
        dcterms:issued "2017-11-09T09:30:00.000Z"^^xsd:dateTime ;
        dcterms:title "Annual Business Survey importers and exporters."@en ;
        pmdf:inFolder <http://gss-data.org.uk/def/concept/folders/themes/trade> ;
        dcterms:publisher <https://www.gov.uk/government/organisations/office-for-national-statistics> ;
        dcterms:references <http://gss-data.org.uk/help> ;
        dcterms:license <http://www.nationalarchives.gov.uk/doc/open-government-licence/version/3/> ;
        pmdds:contactEmail <mailto:abs@ons.gsi.gov.uk> ;
        pmdds:graph <http://gss-data.org.uk/graph/ons-abs> ;
        void:sparqlEndpoint <http://gss-data.org.uk/sparql> ;
        <http://gss-data.org.uk/def/gdp#family> <http://gss-data.org.uk/def/gdp#trade> ;
        a qb:DataSet , csvw:Table ;
  1. brew install clojure
  2. copy source into a file prettify-rdf
  3. chmod +x prettify-rdf
  4. run it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment