Add topic aggregation by year
This commit is contained in:
@@ -70,6 +70,64 @@ async def get_all_author_works(
|
||||
return model.WorkList(elements=all_elements, total=total)
|
||||
|
||||
|
||||
async def get_topics_by_time_by_author(
|
||||
author_id: str,
|
||||
max_topics_per_year: int = 3,
|
||||
client: OpenAlexWorksSearchClient | None = None,
|
||||
) -> model.TopicsByTime:
|
||||
works = await get_all_author_works(author_id, client=client)
|
||||
|
||||
years = {}
|
||||
|
||||
for work in works.elements:
|
||||
if work.publication_year is None:
|
||||
continue
|
||||
|
||||
year = work.publication_year
|
||||
|
||||
if year not in years:
|
||||
years[year] = {
|
||||
"publication_count": 0,
|
||||
"topics": {},
|
||||
}
|
||||
|
||||
years[year]["publication_count"] += 1
|
||||
|
||||
for topic in work.topics:
|
||||
if topic.display_name:
|
||||
topic_name = topic.display_name
|
||||
|
||||
if topic_name not in years[year]["topics"]:
|
||||
years[year]["topics"][topic_name] = 0
|
||||
|
||||
years[year]["topics"][topic_name] += 1
|
||||
|
||||
elements = []
|
||||
|
||||
for year in sorted(years.keys()):
|
||||
topic_items = list(years[year]["topics"].items())
|
||||
topic_items.sort(key=lambda item: item[1], reverse=True)
|
||||
|
||||
topics = []
|
||||
|
||||
for topic_name, count in topic_items[:max_topics_per_year]:
|
||||
topics.append(model.TopicCount(
|
||||
name=topic_name,
|
||||
count=count,
|
||||
))
|
||||
|
||||
elements.append(model.TopicsByYear(
|
||||
year=year,
|
||||
publication_count=years[year]["publication_count"],
|
||||
topics=topics,
|
||||
))
|
||||
|
||||
return model.TopicsByTime(
|
||||
elements=elements,
|
||||
total=len(elements),
|
||||
)
|
||||
|
||||
|
||||
async def _search_author_works_data(
|
||||
search_client: OpenAlexWorksSearchClient,
|
||||
author_id: str,
|
||||
|
||||
Reference in New Issue
Block a user