From 4734aade78377b779fde83f338eecd437eb3534d Mon Sep 17 00:00:00 2001 From: Anthony Bargnesi Date: Wed, 19 Mar 2014 15:36:09 -0400 Subject: [PATCH] example to test sparql endpoint; refs #46 --- sparql_test.py | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100755 sparql_test.py diff --git a/sparql_test.py b/sparql_test.py new file mode 100755 index 0000000..ffb2621 --- /dev/null +++ b/sparql_test.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +# -*- coding: utf8 -*- +# vim: ts=4 sw=4: +from json import dumps +from urllib.error import HTTPError +from SPARQLWrapper import SPARQLWrapper, GET, JSON + + +def query(query, endpoint): + sparql = SPARQLWrapper(endpoint) + sparql.setQuery(query) + sparql.setReturnFormat(JSON) + sparql.setMethod(GET) + + try: + result = sparql.query() + return result.convert() + except HTTPError as e: + print(str(e)) + +if __name__ == '__main__': + import sys + + if len(sys.argv) != 2: + sys.stderr.write('usage: sparql_test.py [SPARQL_ENDPOINT_URL]\n') + sys.exit(1) + + endpoint_url = sys.argv[1] + EXACT_MATCH_ORPHANS = """ + prefix skos: + select (count(distinct ?uri2) as ?count) + where { + ?uri1 skos:exactMatch ?uri2 . + minus { ?uri2 skos:inScheme ?scheme .} + } + """ + ORTHOLOGOUS_MATCH_ORPHANS = """ + prefix skos: + prefix belv: + select (count(distinct ?uri2) as ?count) + where { + ?uri1 belv:orthologousMatch ?uri2 . + minus { ?uri2 skos:inScheme ?scheme .} + } + """ + + print("Orphans for exactMatch:") + print(dumps(query(EXACT_MATCH_ORPHANS, endpoint_url), indent=4)) + + print("Orphans for orthologousMatch:") + print(dumps(query(ORTHOLOGOUS_MATCH_ORPHANS, endpoint_url), indent=4))