diff --git a/examples/Tallies.ipynb b/examples/Tallies.ipynb new file mode 100644 index 0000000..9b9bb3c --- /dev/null +++ b/examples/Tallies.ipynb @@ -0,0 +1,127 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 1, + "id": "rural-papua", + "metadata": {}, + "outputs": [], + "source": [ + "from scite import tallies" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "id": "motivated-helena", + "metadata": {}, + "outputs": [], + "source": [ + "single_doi = \"10.1016/j.biopsych.2005.08.012\"" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "worldwide-twins", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2021-03-11 00:09:33,188 - Tallies - Calling https://api.scite.ai/tallies/10.1016/j.biopsych.2005.08.012\n" + ] + }, + { + "data": { + "text/plain": [ + "{'doi': '10.1016/j.biopsych.2005.08.012',\n", + " 'total': 318,\n", + " 'supporting': 24,\n", + " 'contradicting': 4,\n", + " 'mentioning': 290,\n", + " 'unclassified': 0}" + ] + }, + "execution_count": 6, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tallies.get_tally(single_doi)" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "id": "exciting-leisure", + "metadata": {}, + "outputs": [], + "source": [ + "DOIs = [\"10.1017/CBO9780511628283.008\", \"10.1093/oxfordhb/9780199646135.013.7\"]" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "id": "historic-ultimate", + "metadata": {}, + "outputs": [ + { + "name": "stderr", + "output_type": "stream", + "text": [ + "2021-03-11 00:15:00,527 - Tallies - Calling https://api.scite.ai/tallies with a payload of 2 papers\n" + ] + }, + { + "data": { + "text/plain": [ + "{'tallies': {'10.1017/cbo9780511628283.008': {'doi': '10.1017/cbo9780511628283.008',\n", + " 'total': 190,\n", + " 'supporting': 0,\n", + " 'contradicting': 0,\n", + " 'mentioning': 190,\n", + " 'unclassified': 0},\n", + " '10.1093/oxfordhb/9780199646135.013.7': {'doi': '10.1093/oxfordhb/9780199646135.013.7',\n", + " 'total': 0,\n", + " 'supporting': 0,\n", + " 'contradicting': 0,\n", + " 'mentioning': 0,\n", + " 'unclassified': 0}}}" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tallies.get_tallies(DOIs)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.8.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/scite/tallies.py b/scite/tallies.py new file mode 100644 index 0000000..f30e46f --- /dev/null +++ b/scite/tallies.py @@ -0,0 +1,54 @@ +# -*- coding: utf-8 -*- +from .models import DOI, DOIs +from .http_utils import remote_call +from .logging_utils import set_logger + +logger = set_logger("Tallies") + + +def get_tally(doi): + """Get smart citation tally for given DOI. + + + Examples + -------- + >>> get_tally("10.1016/j.biopsych.2005.08.012") + ... { + ... 'doi': '10.1016/j.biopsych.2005.08.012', + ... 'total': 318, + ... 'supporting': 24, + ... 'contradicting': 4, + ... 'mentioning': 290, + ... 'unclassified': 0 + ...} + + Notes + ----- + Docs: https://api.scite.ai/docs#operation/get_tally_tallies__doi__get + """ + if not isinstance(doi, DOI): + doi = DOI(id=doi).id + logger.debug(f"Calling https://api.scite.ai/tallies/{doi}") + return remote_call(endpoint=f"tallies/{doi}") + + +def get_tallies(dois): + """Get multiple smart citation tallies + Up to 500 papers can be requested at once by + passing in a list of DOIs + + + Examples + -------- + >>> DOIs = ["10.1017/CBO9780511628283.008", "10.1093/oxfordhb/9780199646135.013.7"] + >>> get_tallies(DOIs) + + Notes + ----- + Docs: https://api.scite.ai/docs#operation/get_tallies_tallies_get + """ + if not isinstance(dois, DOIs): + items = DOIs(ids=[{"id": item} for item in dois]) + dois = {item.id for item in items.ids} + logger.debug(f"Calling https://api.scite.ai/tallies with a payload of {len(dois)} papers") + return remote_call(endpoint="tallies", payload=[*dois])