From b697be2306b005f766948997b8a9d4c6870711f0 Mon Sep 17 00:00:00 2001 From: Edward Betts Date: Wed, 9 Oct 2019 15:27:35 +0100 Subject: [PATCH] Move three more queries into templates. --- app.py | 52 +++------------------- templates/query/find_more_basic.sparql | 7 +++ templates/query/painting_no_depicts.sparql | 5 +++ templates/query/property.sparql | 16 +++++++ 4 files changed, 35 insertions(+), 45 deletions(-) create mode 100644 templates/query/find_more_basic.sparql create mode 100644 templates/query/painting_no_depicts.sparql create mode 100644 templates/query/property.sparql diff --git a/app.py b/app.py index 8b2f14f..df173d7 100755 --- a/app.py +++ b/app.py @@ -61,43 +61,6 @@ find_more_props = { # 'P123': 'publisher', (only 1) } -find_more_basic_query = ''' -select distinct ?item ?image { - VALUES ?value { LIST } - ?item wdt:P31 wd:Q3305213 . - ?item wdt:PID ?value . - ?item wdt:P18 ?image . - FILTER NOT EXISTS { ?item wdt:P180 ?depicts } -} limit LIMIT -''' - -property_query = ''' -select ?object ?objectLabel ?objectDescription (count(*) as ?count) { - ?item wdt:P31 wd:Q3305213 . - ?item wdt:P18 ?image . - ?item wdt:PID ?object . - filter not exists { ?item wdt:P180 ?depicts } - optional { - ?object rdfs:label ?objectLabel. - FILTER(LANG(?objectLabel) = "en"). - } - optional { - ?object schema:description ?objectDescription . - filter(lang(?objectDescription) = "en") - } - -} group by ?object ?objectLabel ?objectDescription -order by desc(?count) -''' - -painting_no_depicts_query = ''' -select distinct ?item where { - ?item wdt:P31 wd:Q3305213 . - ?item wdt:P18 ?image . - filter not exists { ?item wdt:P180 ?depicts } -} -''' - @app.teardown_appcontext def shutdown_session(exception=None): database.session.remove() @@ -212,7 +175,7 @@ def property_query_page(property_id): sort = request.args.get('sort') sort_by_name = sort and sort.lower().strip() == 'name' - q = property_query.replace('PID', pid) + q = render_template('query/property.sparql', pid=pid) rows = wdqs.run_query_with_cache(q, name=pid) no_label_qid = [row['object']['value'].rpartition('/')[2] @@ -250,7 +213,8 @@ def start(): @app.route('/next') def random_painting(): - rows = wdqs.run_query_with_cache(painting_no_depicts_query) + q = render_template('query/painting_no_depicts.sparql') + rows = wdqs.run_query_with_cache(q) has_depicts = True while has_depicts: item_id = wdqs.row_id(random.choice(rows)) @@ -861,12 +825,10 @@ def find_more_json(): qid_list = request.args.getlist('qid') limit = 6 - value_list = ' '.join(f'wd:{qid}' for qid in qid_list) - - q = (find_more_basic_query - .replace('LIST', value_list) - .replace('PID', pid) - .replace('LIMIT', str(limit))) + q = render_template('query/find_more_basic.sparql', + qid_list=qid_list, + pid=pid, + limit=limit) filenames = [] bindings = wdqs.run_query_with_cache(q, f'{pid}={",".join(qid_list)}_{limit}') diff --git a/templates/query/find_more_basic.sparql b/templates/query/find_more_basic.sparql new file mode 100644 index 0000000..4e30c99 --- /dev/null +++ b/templates/query/find_more_basic.sparql @@ -0,0 +1,7 @@ +select distinct ?item ?image { + values ?value { {% for qid in qid_list %} wd:{{ qid }}{% endfor %} } + ?item wdt:P31 wd:Q3305213 . + ?item wdt:{{ pid }} ?value . + ?item wdt:P18 ?image . + filter not exists { ?item wdt:P180 ?depicts } +} limit {{ limit }} diff --git a/templates/query/painting_no_depicts.sparql b/templates/query/painting_no_depicts.sparql new file mode 100644 index 0000000..fd780fa --- /dev/null +++ b/templates/query/painting_no_depicts.sparql @@ -0,0 +1,5 @@ +select distinct ?item where { + ?item wdt:P31 wd:Q3305213 . + ?item wdt:P18 ?image . + filter not exists { ?item wdt:P180 ?depicts } +} diff --git a/templates/query/property.sparql b/templates/query/property.sparql new file mode 100644 index 0000000..85f50bd --- /dev/null +++ b/templates/query/property.sparql @@ -0,0 +1,16 @@ +select ?object ?objectLabel ?objectDescription (count(*) as ?count) { + ?item wdt:P31 wd:Q3305213 . + ?item wdt:P18 ?image . + ?item wdt:{{ pid }} ?object . + filter not exists { ?item wdt:P180 ?depicts } + optional { + ?object rdfs:label ?objectLabel. + FILTER(LANG(?objectLabel) = "en"). + } + optional { + ?object schema:description ?objectDescription . + filter(lang(?objectDescription) = "en") + } + +} group by ?object ?objectLabel ?objectDescription +order by desc(?count)