Created
November 2, 2017 03:07
-
-
Save stevecassidy/1ea97780b98253a20e1c3253f154d8f6 to your computer and use it in GitHub Desktop.
Get prompts for Austalk items stored in Alveo using a SPARQL query
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
""" | |
Author: Steve Cassidy | |
Date: 2/11/2017 | |
Query the Alveo SPARQL API for information about the prompt | |
for an item list taken from the Austalk collection. | |
This should be in the item metadata but due to an error in the ingest | |
it was lost, however it is still stored in the RDF metadata since | |
there is a link from each item to it's 'prototype' in the protocol | |
and this has a record of the prompt. Consequently we just need to | |
run a SPARQL query to find the relevant information. | |
""" | |
from __future__ import print_function | |
import pyalveo | |
PREFIXES = """ | |
PREFIX dcterms:<http://purl.org/dc/terms/> | |
PREFIX austalk:<http://ns.austalk.edu.au/> | |
""" | |
def find_prompt(client, item): | |
"""Get the prompt string for an Austalk item, return | |
a tuple (id, prompt) if found, None if not""" | |
query = PREFIXES + """ | |
select ?id ?prompt { | |
BIND (<%s> as ?item) | |
?item dcterms:identifier ?id . | |
?item austalk:prototype ?prot . | |
?prot austalk:prompt ?prompt . | |
} | |
""" % item | |
result = client.sparql_query('austalk', query) | |
b = result['results']['bindings'] | |
# should be exactly one result | |
if len(b) == 1: | |
return b[0]['id']['value'], b[0]['prompt']['value'] | |
else: | |
return None | |
if __name__=='__main__': | |
import sys | |
client = pyalveo.Client() | |
# item list URL is the only argument | |
listurl = sys.argv[1] | |
itemlist = client.get_item_list(listurl) | |
for item in itemlist: | |
id, prompt = find_prompt(client, item) | |
print(id, prompt) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment