-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaatc_generate.py
91 lines (74 loc) · 3.9 KB
/
aatc_generate.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#####
# script creates a Getty AAT (https://vocab.getty.edu/aat/) slim (a) subsection of Getty AAT concepts
# via a SPARQL CONSTRUCT query
# saving the results onto the aatc.ttl file, referred to The Art and Architecture Thesaurus Concepts (AATC)
#
# Transformation
# AAT gvp:concepts with labels in EN or NL become a skos:Concept with rdfs:label @en @nl
# lang: @en-US is converted to @en
# AAT concept become members (skos:inScheme) of the http://vocabularies.dans.knaw.nl/aatconcepts skos:ConceptScheme
#####
from SPARQLWrapper import SPARQLWrapper, TURTLE
from datetime import date
prefixes = '''
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX aat: <http://vocab.getty.edu/aat/>
PREFIX gvp: <http://vocab.getty.edu/ontology#>
PREFIX gvp_lang: <http://vocab.getty.edu/language/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX skosxl: <http://www.w3.org/2008/05/skos-xl#>
PREFIX dcterms: <http://purl.org/dc/terms/>
PREFIX aatc: <http://vocabularies.dans.knaw.nl/aatconcepts/>
'''
def sparql_query(query):
'''
function
* receives SPARQL query as input
* runs the query against Getty Vocabularies SPARQL endpoint
* returns the results in turtle (ttl) format
'''
endpoint = "http://vocab.getty.edu/sparql"
sparql = SPARQLWrapper(endpoint)
query = prefixes + query
sparql.setQuery(query)
sparql.setReturnFormat(TURTLE)
results = sparql.query().convert()
return results
def print_sparql_results(results):
for row in results["results"]["bindings"]:
return (row)
today = date.today().strftime("%Y-%m-%d")
getty_concepts_construct = '''
CONSTRUCT {
<http://vocabularies.dans.knaw.nl/aatconcepts> a skos:ConceptScheme ;
dct:title "The Art and Architecture Thesaurus Concepts"@en ;
rdfs:label "The Art and Architecture Thesaurus Concepts"@en ;
rdfs:comment "The Art and Architecture Thesaurus Concepts (AATC) is a SKOS concept scheme that restructures the concepts from the Art and Architecture Thesaurus (AAT) into a flat controlled vocabulary, and excludes non-concepts, such as facets, hierarchies and guide terms. The result is a controlled vocabulary where each term is a skos:Concept, member of aatc: skos:ConceptScheme, with English(@en) and Dutch(@nl) labels. Original URI are kept, and users are encourage to find more information and reference the term through its URI. AATC development was motivated by the need to index AAT terms in a Skosmos server, so that AAT terms could be used easily queried via the Skosmos API and easily used by software applications to classify data with AAT terms"@en ;
dct:creator <https://ror.org/008pnp284> , <https://orcid.org/0000-0002-7839-3698> ;
dct:created "%s"^^xsd:date ;
dcterms:license <http://opendatacommons.org/licenses/by/1.0/> .
?concept a skos:Concept ;
skos:inScheme <http://vocabularies.dans.knaw.nl/aatconcepts> ;
skos:prefLabel ?label_literal_en ;
skos:prefLabel ?label_literal_nl ;
dcterms:issued ?issuedDate .
}
WHERE {
?concept a gvp:Concept ;
skos:inScheme aat: ;
skosxl:prefLabel ?preflabel .
OPTIONAL {?concept dcterms:issued ?issuedDate .}
{ ?preflabel dcterms:language aat:300388277 ; skosxl:literalForm ?label_literal_en. } # lang: @en
UNION
{ ?preflabel dcterms:language aat:300387822 ; skosxl:literalForm ?label_literal_en_us .
BIND (STRLANG(STR(?label_literal_en_us), 'en') AS ?label_literal_en)} # lang: @en-US - converts to @en
UNION
{ ?preflabel dcterms:language aat:300388256 ; skosxl:literalForm ?label_literal_nl . } # lang: @nl
FILTER( STRSTARTS(str(?concept), str(aat:)) )
}
ORDER BY ?concept
''' % today
print(getty_concepts_construct)
construct_results = sparql_query(query=getty_concepts_construct)
with open('aatc.ttl', 'wb') as att_subjects_f:
att_subjects_f.write(construct_results)