Converter Module

This module contains the main class used for processing TVDs.

Example

If cloned from codeberg the repository will contain some examples in the /docs/samples folder.

>>> from tvd2rdf.converter import Converter
>>> c = Converter()
>>> c.read_namespaces("docs/samples/namespaces.csv")
"Namespace csv file 'docs/samples/namespaces.csv' read and loaded."
>>> c.read_tvd("docs/samples/terms.csv")
"TVD file 'docs/samples/terms.csv' has been read and processed."
>>> c.write_out(fmt="turtle")
@prefix ex: <https://example.org/terms#> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
@prefix sdo: <https://schema.org/> .
@prefix skos: <http://www.w3.org/2004/02/skos/core#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
ex:Document a rdfs:Class ;
    rdfs:label "Document" ;
    rdfs:comment "A written resource." ;
    skos:scopeNote "Documents may be in physical or digital media." .
ex: a owl:Ontology ;
    rdfs:label "Test Terms" ;
    rdfs:comment "A test RDF vocabulary." ;
    skos:scopeNote "Used for testing VocabDef" .
.. etc.
... etc.
class tvd2rdf.converter.Converter(config_fn: str = None)

Class to convert a TVD in a csv file into RDF.

Variables:
  • vocab_rdf (RDFLib Graph) – The RDF vocabulary definition Graph.

  • namespaces (dict) – Prefix to URI-stem mappings for use in cURIs.

  • types_map (dict) – Name to URI mapping for types of terms that can be defined (e.g. Properties, Classes).

  • relationships_map (dict) – name to RDF property for relationships used in term definitions.

  • fields_map (dict) – Field name to rdf property mapping for each type of term.

  • known_fields (list) – Collated list of all known fields.

  • config (dict) – Instance configuration that informs the mapping.

__init__(config_fn: str = None)

Create a new tvd2rdf Converter instance.

Parameters:

config_fn (str) – Optional filename for configuration file.

read_namespaces(fn: str = None)

Load namespaces from a (csv) file.

A csv file with two columns can be read to supplement the namespaces read from config.yaml. It should have two columns, the first headed prefix, the second headed URI.

Parameters:

fn (str) – The path / filename for the namespaces csv file.

read_tvd(fname: str)

Read a Tabular Vocabulary Definition from a csv file.

Uses csv.DictReader to read the file into a dict, checks that the keys (column headings) are present and correct, converts each (key, value) pair into an RDF statement in the vocab definition graph self.vocab_rdf.

Parameters:

fname (str) – The path/filename for the TVD.

Warns:

Warning – If a row in the TVD cannot be converted.

write_out(fn: str = '', fmt: str = '')

Write the RDF vocabulary terms definitions to a file or terminal.

Parameters:
  • fn (str) – Optional path / filename for file into which to write the RDF term definitions; if absent the term definitions will be printed to the terminal.

  • fmt (str) – Any RDF serialization format supported by RDFLib, including “turtle”, “n3”, “nquads”, “longturtle”, “trig”, “nt”, “xml”, “pretty-xml”, and “trix”.