Last active
June 4, 2022 07:45
-
-
Save xflr6/9ae65e32e0f325b72594 to your computer and use it in GitHub Desktop.
Example building RDF with rdflib
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
"""Build RDF with rdflib and serialize in turtle format.""" | |
import rdflib | |
from rdflib.namespace import DCTERMS, RDF, RDFS, SKOS | |
GOLD = rdflib.Namespace('http://purl.org/linguistics/gold/') | |
LANGUOID = rdflib.Namespace('http://glottolog.org/resource/languoid/id/') | |
VOID = rdflib.Namespace('http://rdfs.org/ns/void#') | |
PREFIXES = {'dcterms': DCTERMS, | |
'gold': GOLD, | |
'languoid': LANGUOID, | |
'rdf': RDF, | |
'rdfs': RDFS, | |
'skos': SKOS, | |
'void': VOID} | |
g = rdflib.Graph() | |
for prefix, namespace in PREFIXES.items(): | |
g.bind(prefix, namespace, replace=True) | |
l = LANGUOID['ainu1252'] | |
name_en = rdflib.Literal('Ainu', lang='en') | |
g.add((l, RDF.type, DCTERMS.LinguisticSystem)) | |
g.add((l, RDF.type, GOLD.LanguageFamily)) | |
g.add((l, RDFS.label, name_en)) | |
g.add((l, VOID.inDataset, rdflib.URIRef('http://glottolog.org/'))) | |
g.add((l, DCTERMS.title, name_en)) | |
g.add((l, DCTERMS.spatial, rdflib.Literal('Eurasia'))) | |
g.add((l, SKOS.prefLabel, name_en)) | |
g.add((l, SKOS.altLabel, rdflib.Literal('ainu1252', lang='x-clld'))) | |
g.add((l, SKOS.scopeNote, rdflib.Literal('language', lang='x-clld'))) | |
g.add((l, SKOS.editorialNote, rdflib.Literal('established'))) | |
g.add((l, SKOS.narrower, LANGUOID['ainu1240'])) | |
g.add((l, SKOS.narrower, LANGUOID['kuri1271'])) | |
g.add((l, SKOS.narrower, LANGUOID['sakh1245'])) | |
print(g.serialize()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment