Introduction¶
Installation¶
To be able to use this library you need to have a modern version of Python installed.
This easiest way to install this library is through pip or easy install:
$ pip install skosprovider_rdf
This will download and install skosprovider_rdf
and a few libraries it
depends on.
Usage¶
This library offers an implementation of the
skosprovider.providers.VocabularyProvider
interface that uses an
rdflib.graph.Graph
as input. This provider can be used to add a SKOS
vocabulary contained in an RDF file to your application. The provider
itself does not read the SKOS file, but expects to be passed a
Graph
. So any type of RDF serialisation that can be read by
rdflib
, can be used with this provider.
import os
from rdflib import Graph
from skosprovider_rdf.providers import RDFProvider
graph = Graph()
file = os.path.join(os.path.dirname(__file__), '..', 'tests', 'data', 'simple_turtle_products')
graph.parse(file, format="turtle")
provider = RDFProvider(
{'id': 'PRODUCTS'},
graph
)
print("provider.get_all()")
print("------------------")
print(provider.get_all())
print("")
print("provider.find({'label': 'jewelry'})")
print("-----------------------------------")
print(provider.find({'label': 'jewelry'}))
print("")
print("provider.get_by_id('http://wwww.products.com/Jewellery')")
print("--------------------------------------------------------")
print(provider.get_by_id('http://www.products.com/Jewellery'))
print("")
print("provider.get_by_uri('http://wwww.products.com/Jewellery')")
print("---------------------------------------------------------")
print(provider.get_by_uri('http://www.products.com/Jewellery'))
print("")
Out of the box skosprovider_rdf
assumes your RDF file contains
exactly one conceptscheme. If no conceptscheme is found in the file and you did
not pass one to the provider through the concept_scheme parameter, a new
conceptscheme is automatically created. If more than one conceptscheme is
present in the file, you can again specify the conceptscheme through the
concept_scheme parameter (passing a skosprovider.skos.ConceptScheme
)
or you can pass the uri of one of the conceptschemes present in the
concept_scheme_uri parameter. When you specify a conceptscheme like this, only
the concepts linked to this scheme through skos:inScheme statements will be
loaded.
'''
This examples fetches only one conceptscheme from a turtle file containing two.
'''
import os
from rdflib import Graph
from skosprovider_rdf.providers import RDFProvider
graph = Graph()
file = os.path.join(os.path.dirname(__file__), '..', 'tests', 'data', 'waarde_en_besluit_types.ttl')
graph.parse(file, format="turtle")
provider = RDFProvider(
{'id': 'WAARDETYPES'},
graph,
concept_scheme_uri = 'https://id.erfgoed.net/thesauri/waardetypes'
)
print("provider.get_all()")
print("------------------")
print(provider.get_all())
print("")
print("provider.find({'label': 'esthetische waarde'})")
print("-----------------------------------")
print(provider.find({'label': 'esthetische waarde'}))
print("")
print("provider.get_by_id(46)")
print("--------------------------------------------------------")
print(provider.get_by_id('46'))
print("")
print("provider.get_by_uri('https://id.erfgoed.net/thesauri/waardetypes/46')")
print("---------------------------------------------------------")
print(provider.get_by_uri('https://id.erfgoed.net/thesauri/waardetypes/46'))
print("")
It also provides a utility function to dump any implementation
of skosprovider.providers.VocabularyProvider
to a
rdflib.graph.Graph
. Again, since the provider only deals with the
Graph
object, it’s possible to serialise a VocabularyProvider
to whatever RDF serialisations rdflib
allows.
'''
This script demonstrates dumping a
:class:`skosprovider.providers.SimpleCsvProvider` as a RDF Graph. In this
case, `n3` serialisation is used, other serialisations are available through
:mod:`rdflib`.
'''
import os
import csv
from skosprovider.providers import SimpleCsvProvider
from skosprovider.uri import UriPatternGenerator
from skosprovider.skos import ConceptScheme, Label, Note, Source
from skosprovider_rdf.utils import rdf_dumper
ifile = open(
os.path.join(os.path.dirname(__file__), 'data', 'menu.csv'))
reader = csv.reader(ifile)
csvprovider = SimpleCsvProvider(
{'id': 'MENU'},
reader,
uri_generator=UriPatternGenerator('http://id.python.org/menu/%s'),
concept_scheme=ConceptScheme(
uri='http://id.python.org/menu',
labels=[
Label(type='prefLabel', language='en', label='A pythonesque menu.')
],
notes=[
Note(
type='changeNote',
language='en',
note="<strong>We didn't need no change notes when I was younger.</strong>",
markup='HTML'
)
],
sources=[
Source("Monthy Python's Flying Circus, 1970. Spam.")
]
)
)
graph = rdf_dumper(csvprovider)
print(graph.serialize(format='n3'))