Skip to content

Commit

Permalink
Implemented work/random
Browse files Browse the repository at this point in the history
  • Loading branch information
egonw committed Jul 15, 2023
1 parent 6d14811 commit 9454092
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 1 deletion.
7 changes: 7 additions & 0 deletions scholia/app/templates/work-index.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ <h1>Work</h1>

Scientific articles, conference articles, books, ...

<p>
<div class="btn-group">
<a title="Sample a random work among all works. This query takes several seconds."
href="random"
class="btn btn-primary">Random work</a>
</div>
</p>

<div class="container">

Expand Down
16 changes: 15 additions & 1 deletion scholia/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
49 changes: 49 additions & 0 deletions scholia/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
scholia.query q-to-label <q>
scholia.query q-to-class <q>
scholia.query random-author
scholia.query random-work
scholia.query ror-to-q <rorid>
scholia.query twitter-to-q <twitter>
scholia.query uniprot-to-q <protein>
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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['<twitter>'])
if len(qs) > 0:
Expand Down

0 comments on commit 9454092

Please sign in to comment.