-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_location_mappings.py
234 lines (221 loc) · 7.62 KB
/
load_location_mappings.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
from string import Template
import datetime
from helpers import generate_uuid
from query import query, update
from escape_helpers import (sparql_escape_string,
sparql_escape_uri,
sparql_escape_datetime,
sparql_escape_float)
from query_result_helpers import to_recs
MAPPING_GRAPH = "http://mu.semte.ch/graphs/entity-mappings"
MANUAL_MAPPING_GRAPH = "http://mu.semte.ch/graphs/entity-manual-mappings"
CLUSTER_GRAPH = "http://mu.semte.ch/graphs/entity-clusters"
def load_ungrouped_location_mapping():
query_template = Template("""
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX locn: <http://www.w3.org/ns/locn#>
PREFIX sssom: <https://w3id.org/sssom/>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
SELECT ?location ?mapping ?a ?b
WHERE {
GRAPH $manual_mapping_graph {
?mapping a sssom:Mapping ;
sssom:predicate_id ?predicate_id .
# TODO: determine if both are ok
VALUES ?predicate_id {
<http://www.w3.org/2004/02/skos/core#exactMatch>
<http://www.w3.org/2004/02/skos/core#relatedMatch>
# 'http://mu.semte.ch/vocabularies/ext/noMatch'
}
{
?mapping sssom:subject_id ?location .
?mapping sssom:object_id ?b .
}
UNION
{
?mapping sssom:subject_id ?a .
?mapping sssom:object_id ?location .
}
}
{
SELECT ?location
WHERE {
VALUES ?g {
<http://locatieslinkeddata.ticketgang-locations.ticketing.acagroup.be>
<http://locatiessparql.kunstenpunt-locaties.professionelekunsten.kunsten.be>
<http://placessparql.publiq-uit-locaties.vrijetijdsparticipatie.publiq.be>
<http://organisatorensparql.publiq-uit-organisatoren.vrijetijdsparticipatie.publiq.be>
}
GRAPH ?g {
?location a dct:Location .
}
GRAPH $manual_mapping_graph {
# For now clusters of mappings. These may become clusters of locations?
?m a sssom:Mapping .
?m sssom:subject_id ?location .
}
GRAPH $cluster_graph {
FILTER NOT EXISTS {
?clocation a ext:Cluster .
?clocation ext:member ?m .
}
}
}
LIMIT 1
}
}
LIMIT 1
""")
query_string = query_template.substitute(
manual_mapping_graph=sparql_escape_uri(MANUAL_MAPPING_GRAPH),
cluster_graph=sparql_escape_uri(CLUSTER_GRAPH)
)
query_result = query(query_string)
if query_result["results"]["bindings"]:
return to_recs(query_result)[0]
else:
return None
def query_related_mappings(mapping, related_mappings=[]):
query_template = Template("""
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX locn: <http://www.w3.org/ns/locn#>
PREFIX sssom: <https://w3id.org/sssom/>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT DISTINCT ?related_mapping
WHERE {
{
SELECT DISTINCT ?location
WHERE {
GRAPH $manual_mapping_graph {
$mapping a sssom:Mapping ;
sssom:predicate_id ?predicate_id .
VALUES ?predicate_id {
<http://www.w3.org/2004/02/skos/core#exactMatch>
<http://www.w3.org/2004/02/skos/core#relatedMatch>
# 'http://mu.semte.ch/vocabularies/ext/noMatch'
}
{
$mapping sssom:subject_id ?location .
}
UNION
{
$mapping sssom:object_id ?location .
}
}
}
}
GRAPH $manual_mapping_graph {
?related_mapping a sssom:Mapping .
{
?related_mapping sssom:subject_id ?location .
}
UNION
{
?related_mapping sssom:object_id ?location .
}
FILTER (?related_mapping NOT IN (
$related_mappings
))
}
}
""")
query_string = query_template.substitute(
mapping=sparql_escape_uri(mapping),
manual_mapping_graph=sparql_escape_uri(MANUAL_MAPPING_GRAPH),
related_mappings=",\n ".join([sparql_escape_uri(m) for m in related_mappings])
)
query_result = query(query_string)
if query_result["results"]["bindings"]:
return to_recs(query_result)
else:
return []
def write_cluster(members, created=datetime.datetime.now()):
CLUSTER_BASE_URI = "http://data.publiq.be/cluster/"
uuid = generate_uuid()
uri = CLUSTER_BASE_URI + uuid
query_template = Template("""
PREFIX sssom: <https://w3id.org/sssom/>
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX mu: <http://mu.semte.ch/vocabularies/core/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
INSERT DATA {
GRAPH $graph {
$cluster a ext:Cluster ;
mu:uuid $uuid ;
dct:created $created ;
dct:modified $modified ;
ext:member $members .
}
}
""")
query_string = query_template.substitute(
graph=sparql_escape_uri(CLUSTER_GRAPH),
uuid=sparql_escape_string(uuid),
cluster=sparql_escape_uri(uri),
created=sparql_escape_datetime(created),
modified=sparql_escape_datetime(created),
members=", ".join([sparql_escape_uri(m) for m in members])
)
update(query_string)
def query_verified_location_mapping_by_address_mapping(address_mapping):
query_template = Template("""
PREFIX prov: <http://www.w3.org/ns/prov#>
PREFIX locn: <http://www.w3.org/ns/locn#>
PREFIX sssom: <https://w3id.org/sssom/>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT DISTINCT (?verified_location_mapping AS ?uri) ?a ?a_label ?b ?b_label
WHERE {
GRAPH $mapping_graph {
$address_mapping a sssom:Mapping .
{
$address_mapping sssom:subject_id ?address .
}
UNION
{
$address_mapping sssom:object_id ?address .
}
}
VALUES ?g {
<http://locatieslinkeddata.ticketgang-locations.ticketing.acagroup.be>
<http://locatiessparql.kunstenpunt-locaties.professionelekunsten.kunsten.be>
<http://placessparql.publiq-uit-locaties.vrijetijdsparticipatie.publiq.be>
<http://organisatorensparql.publiq-uit-organisatoren.vrijetijdsparticipatie.publiq.be>
}
GRAPH ?g {
?location locn:address ?address .
}
GRAPH $manual_mapping_graph {
?verified_location_mapping a sssom:Mapping ;
sssom:subject_id ?a ;
sssom:subject_label ?a_label ;
sssom:object_id ?b ;
sssom:object_label ?b_label ;
sssom:mapping_justification <https://w3id.org/semapv/vocab/ManualMappingCuration> ;
sssom:predicate_id ?predicate_id .
VALUES ?predicate_id {
<http://www.w3.org/2004/02/skos/core#exactMatch>
<http://www.w3.org/2004/02/skos/core#relatedMatch>
# 'http://mu.semte.ch/vocabularies/ext/noMatch'
}
{
?verified_location_mapping sssom:subject_id ?location .
}
UNION
{
?verified_location_mapping sssom:object_id ?location .
}
}
}
""")
query_string = query_template.substitute(
address_mapping=sparql_escape_uri(address_mapping),
mapping_graph=sparql_escape_uri(MAPPING_GRAPH),
manual_mapping_graph=sparql_escape_uri(MANUAL_MAPPING_GRAPH),
)
query_result = query(query_string)
if query_result["results"]["bindings"]:
return to_recs(query_result)
else:
return []