-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_address_mappings.py
71 lines (68 loc) · 2.1 KB
/
load_address_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
from string import Template
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
from address import Address
MAPPING_GRAPH = "http://mu.semte.ch/graphs/entity-mappings"
def load_address_mapping_page(page=0, size=50, _from=None):
offset = page * size
limit = size
if _from:
from_filter = f"FILTER (?created > {sparql_escape_datetime(_from)})"
else:
from_filter = ""
query_template = Template("""
PREFIX locn: <http://www.w3.org/ns/locn#>
PREFIX sssom: <https://w3id.org/sssom/>
PREFIX dct: <http://purl.org/dc/terms/>
SELECT (?mapping AS ?uri) ?a_location ?b_location ?a_locator_name ?b_locator_name ?address_similarity_score
WHERE {
GRAPH $graph {
?mapping
a sssom:Mapping ;
dct:created ?created ;
sssom:subject_id ?a ;
sssom:object_id ?b ;
sssom:similarity_score ?address_similarity_score .
$from_filter
}
?a a locn:Address .
?b a locn:Address .
?a_location locn:address ?a .
?b_location locn:address ?b .
FILTER (?a_location != ?b_location)
?a_location (locn:LocatorName | locn:locatorName) ?a_locator_name .
?b_location (locn:LocatorName | locn:locatorName) ?b_locator_name .
FILTER NOT EXISTS {
{
?existing_mapping
sssom:subject_id ?a_location ;
sssom:object_id ?b_location .
}
UNION
{
?existing_mapping
sssom:subject_id ?b_location ;
sssom:object_id ?a_location .
}
}
}
ORDER BY DESC(?address_similarity_score)
OFFSET $offset
LIMIT $limit
""")
query_string = query_template.substitute(
graph=sparql_escape_uri(MAPPING_GRAPH),
from_filter=from_filter,
offset=offset,
limit=limit
)
query_result = query(query_string)
if query_result["results"]["bindings"]:
return to_recs(query_result)
else:
return None