Skip to content

Instantly share code, notes, and snippets.

@alecjacobson
Created May 6, 2024 17:15
Show Gist options
  • Save alecjacobson/030992b697903b49cca2e78239adef90 to your computer and use it in GitHub Desktop.
Save alecjacobson/030992b697903b49cca2e78239adef90 to your computer and use it in GitHub Desktop.
from semanticscholar import SemanticScholar
from collections import defaultdict
from tqdm import tqdm
import sys
import os
s2_api_key = 'replaceme'
sch = SemanticScholar(timeout=10,api_key=s2_api_key)
print("Connected to SemanticScholar")
# alec
author_ids = [2242015445,145151177,2251097727]
# if author is number make into a singleton list
if isinstance(author_ids, int):
author_ids = [author_ids]
count = 0
first_count = 0
last_count = 0
papers = []
names = []
for author_id in author_ids:
author = sch.get_author(author_id)
names.append(author.name)
if len(author.papers) < author.paperCount:
print(f"Warning: {author.name} has {author.paperCount} papers, but only {len(author.papers)} were retrieved")
# append papers to list
papers.extend(author.papers)
names = ", ".join(names)
# print names
print(f"Retrieved {len(papers)} papers for {names}")
try:
# unique papers
papers = list({paper['paperId']:paper for paper in papers}.values())
# make a dict from "authorname" to count
author_counts = defaultdict(int)
# for each authors_paper_short_rec in papers
for authors_paper_short_rec in papers:
authors_paper_full_rec = sch.get_paper(authors_paper_short_rec['paperId'])
citations = authors_paper_full_rec.citations
# for each author in citation['authors']
for citation in citations:
for author in citation['authors']:
# get name
name = author['name']
author_counts[name] += 1
except Exception as e:
print(f"Error: {e}")
print("-------------------------------------------------")
# print count | name sorted by count
for name, count in sorted(author_counts.items(), key=lambda x: x[1], reverse=True):
print(f"{count} | {name}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment