Skip to content

Instantly share code, notes, and snippets.

View niklasl's full-sized avatar

Niklas Lindström niklasl

View GitHub Profile
@niklasl
niklasl / rdf_vocab_to_jsonld_context.py
Created May 22, 2012 16:41
Generating a JSON-LD context from an RDF vocabulary
import sys
from rdflib import *
fpath = sys.argv[1]
prefix = sys.argv[2]
g = Graph().parse(fpath, format='n3' if fpath.endswith(('.n3', '.ttl')) else 'xml')
def name_pair(item):
qname = item.qname()
"""
A simple query language parser experiment.
"""
OPERATORS: dict[str, tuple[str | None, int, str | int]] = {
'and': ('AND', -1, 'AND'),
'or': ('OR', -1, 'AND'),
'not': ('NOT', 0, 1),
'^': ('INV', 0, 1),
'=': ('EQ', -1, 1),
## WORKS
</work/neverwhere-0> a :Work ;
#:identityType :Hub ;
:title [ a :Title ; :mainTitle "Neverwhere" ] ;
:contribution [ a :PrimaryContribution ;
:agent </person/Neil_Gaiman> ] ;
:language </language/eng> .
</work/neverwhere-1> a :Text ;
/**
* To level up your synesthesia, choose colors, apply on web pages and read
* some 100 000 words. Purportedly. :)
* http://www.theatlantic.com/technology/archive/2012/07/can-you-teach-yourself-synesthesia/259519/
*/
var charColorMap = {
a: 'darkred',
e: 'green',
s: 'blue',
@niklasl
niklasl / context-vocab-array-combined-iri-coerce.json
Created November 4, 2011 20:32
JSON-LD context array with different vocabs using combined iri-coerce objects
{
"@context": [
{
"@vocab": "http://www.w3.org",
"label": "/2000/01/rdf-schema#label",
"comment": "/2000/01/rdf-schema#comment",
"prefLabel": "/2004/02/skos/core#prefLabel",
"altLabel": "/2004/02/skos/core#altLabel",
"prev": {"/1999/xhtml/vocab#prev": "@iri"},
@niklasl
niklasl / rsync-to-unison.sh
Created October 28, 2017 21:02
Crude fallback when unison isn't available on your local machine. (Note that this method stores duplicates of the files of interest on the server.)
# Paths of interest
LOCALDIR=syncdir
HOST=user@example.org
LANDINGDIR=landingdir
TARGETDIR=syncdir
# Sync files from local device to the server landing dir
rsync -auv --delete $LOCALDIR/ $HOST:$LANDINGDIR
# Two-way-sync the landing dir with the target dir
@niklasl
niklasl / make_summary.py
Created February 3, 2017 14:33
Flat KBV vocabulary summary
from __future__ import unicode_literals, print_function
from rdflib import *
g = Graph().parse("https://id.kb.se/vocab/")
with open('select-summary.rq') as f:
select = f.read()
print('Term', 'Bases', 'Domain', 'Range', sep='\t')
for row in g.query(select):
@niklasl
niklasl / illustrated-text-class.ttl
Created October 7, 2016 12:44
Just an OWL sketch for possibly inferring some BibFrame details from other details...
prefix : <http://www.w3.org/2002/07/owl#>
prefix bf: <http://id.loc.gov/ontologies/bibframe/>
prefix kbv: <https://id.kb.se/vocab/>
prefix marcrel: <http://id.loc.gov/vocabulary/relators/>
kbv:IllustratedText a :Class;
:equivalentClass [
:intersectionOf (
bf:Text
[ a :Restriction;
@niklasl
niklasl / get-saogf-from-idkbse.py
Last active May 30, 2016 14:22
Get SAOGF from id.kb.se
#!/usr/bin/env python3
from urllib.parse import urljoin
from urllib.request import urlopen, Request
import json
import codecs
reader = codecs.getreader("utf-8")
def crawl(collection_url):
while True:
@niklasl
niklasl / schemaorg_to_jsonldcontext.py
Last active December 30, 2015 19:19
Generates a JSON-LD Context for Schema.org.
from rdflib import *
SDO = Namespace("http://schema.org/")
datatype_coerce_map = {
#SDO.Number: XSD.double,
SDO.Date: 'xsd:date',
SDO.DateTime: "xsd:dateTime",
}