From 9454092e9c5eb56bf0fc3b03923293c86d2e76b8 Mon Sep 17 00:00:00 2001 From: Egon Willighagen Date: Sat, 15 Jul 2023 14:06:08 +0200 Subject: [PATCH] Implemented work/random --- scholia/app/templates/work-index.html | 7 ++++ scholia/app/views.py | 16 ++++++++- scholia/query.py | 49 +++++++++++++++++++++++++++ 3 files changed, 71 insertions(+), 1 deletion(-) diff --git a/scholia/app/templates/work-index.html b/scholia/app/templates/work-index.html index db4542e9e..475f92560 100644 --- a/scholia/app/templates/work-index.html +++ b/scholia/app/templates/work-index.html @@ -17,6 +17,13 @@

Work

Scientific articles, conference articles, books, ... +

+

+ Random work +
+

diff --git a/scholia/app/views.py b/scholia/app/views.py index 6dc099424..21963ae5c 100644 --- a/scholia/app/views.py +++ b/scholia/app/views.py @@ -22,7 +22,7 @@ twitter_to_qs, cordis_to_qs, mesh_to_qs, pubmed_to_qs, lipidmaps_to_qs, ror_to_qs, wikipathways_to_qs, pubchem_to_qs, atomic_number_to_qs, ncbi_taxon_to_qs, - ncbi_gene_to_qs, uniprot_to_qs) + ncbi_gene_to_qs, uniprot_to_qs, random_work) from ..utils import sanitize_q, remove_special_characters_url from ..wikipedia import q_to_bibliography_templates @@ -2044,6 +2044,20 @@ def show_work_export(q): return render_template('work-export.html', q=q) +@main.route('/work/random') +def show_work_random(): + """Redirect to random work. + + Returns + ------- + reponse : werkzeug.wrappers.Response + Redirect + + """ + q = random_work() + return redirect(url_for('app.show_work', q=q), code=302) + + @main.route('/cito/' + q_pattern) def show_cito(q): """Return HTML rendering for a specific Citation Typing Ontology intention. diff --git a/scholia/query.py b/scholia/query.py index 20c2e6979..e02f8f1f5 100644 --- a/scholia/query.py +++ b/scholia/query.py @@ -24,6 +24,7 @@ scholia.query q-to-label scholia.query q-to-class scholia.query random-author + scholia.query random-work scholia.query ror-to-q scholia.query twitter-to-q scholia.query uniprot-to-q @@ -1668,6 +1669,50 @@ def random_author(): return q +def random_work(): + """Return random work. + + Sample a scientific work randomly from Wikidata by a call to the Wikidata + Query Service. + + Returns + ------- + q : str + Wikidata identifier. + + Notes + ----- + The work returned is not necessarily a scholarly work. + + The algorithm uses a somewhat hopeful randomization and if no work is + found it falls back on Q21146099. + + Examples + -------- + >>> q = random_work() + >>> q.startswith('Q') + True + + """ + # Generate 100 random Q-items and hope that one of them is a work with an + # work + values = " ".join("wd:Q{}".format(randrange(1, 100000000)) + for _ in range(100)) + + query = """SELECT ?work {{ + VALUES ?work {{ {values} }} + ?work wdt:P50 ?author . + }} + LIMIT 1""".format(values=values) + bindings = query_to_bindings(query) + if len(bindings) > 0: + q = bindings[0]['work']['value'][31:] + else: + # Fallback + q = "Q21146099" + return q + + def main(): """Handle command-line interface.""" from docopt import docopt @@ -1804,6 +1849,10 @@ def main(): q = random_author() print(q) + elif arguments['random-work']: + q = random_work() + print(q) + elif arguments['twitter-to-q']: qs = twitter_to_qs(arguments['']) if len(qs) > 0: