diff --git a/service/src/main/inse/config.py b/service/src/main/inse/config.py index fbd6098f..2b70a56c 100644 --- a/service/src/main/inse/config.py +++ b/service/src/main/inse/config.py @@ -29,6 +29,8 @@ class Settings(BaseSettings): llm_model: str = 'gemma4:e4b' elasticsearch_url: str = 'https://project.nes.uni-due.de/elastic/' elasticsearch_api_key: str = 'am1wWXBaMEJEVWJoZkE5MnBaUlk6TWlvbW1LRlMzNjdDUHgzYmVkUmI5UQ==' + elasticsearch_works_url: str = 'https://project.nes.uni-due.de/elastic/openalex-works/_search' + settings = Settings() logger.info(f"Database URL: {settings.database_url}.") diff --git a/service/src/main/inse/logic.py b/service/src/main/inse/logic.py index d98c878f..013d2547 100644 --- a/service/src/main/inse/logic.py +++ b/service/src/main/inse/logic.py @@ -287,3 +287,185 @@ def generate_person_bio(author_id: str) -> str: return llm.call(system_prompt=system_prompt, user_prompt=user_prompt) +def create_publications_by_author_query( + author_id: str, + page: int = 1, + page_size: int = 20 +) -> dict: + if not author_id.startswith("https://openalex.org/"): + author_id = f"https://openalex.org/{author_id}" + + from_value = (page - 1) * page_size + + return { + "from": from_value, + "size": page_size, + "_source": [ + "id", + "title", + "display_name", + "publication_year", + "authorships.author.id", + "authorships.author.display_name", + "authorships.author_position", + "funders.id", + "funders.display_name", + "funders.ror" + ], + "query": { + "bool": { + "must": [ + { + "nested": { + "path": "authorships", + "query": { + "term": { + "authorships.author.id": author_id + } + } + } + }, + { + "nested": { + "path": "funders", + "query": { + "exists": { + "field": "funders.id" + } + } + } + } + ] + } + } + } + +def get_publications_by_author_id( + author_id: str, + page: int = 1, + page_size: int = 20 +) -> dict: + headers = { + "Authorization": f"ApiKey {config.settings.elasticsearch_api_key}", + "Content-Type": "application/json" + } + + payload = create_publications_by_author_query(author_id, page, page_size) + + with httpx.Client() as client: + response = client.post( + config.settings.elasticsearch_works_url, + json=payload, + headers=headers + ) + response.raise_for_status() + data = response.json() + + total = data.get("hits", {}).get("total", {}).get("value", 0) + hits = data.get("hits", {}).get("hits", []) + + return { + "author_id": author_id if author_id.startswith("https://openalex.org/") else f"https://openalex.org/{author_id}", + "page": page, + "page_size": page_size, + "total": total, + "results_count": len(hits), + "results": hits + } + +def get_author_name(author_id: str) -> str | None: + short_author_id = author_id.replace("https://openalex.org/", "") + + search_result = search_persons(author_id=short_author_id) + + if not search_result.persons: + return None + + return search_result.persons[0].name + +def get_author_position_in_publication(publication: dict, author_id: str) -> str: + normalized_author_id = ( + author_id if author_id.startswith("https://openalex.org/") + else f"https://openalex.org/{author_id}" + ) + + for authorship in publication.get("authorships", []): + author = authorship.get("author", {}) + if author.get("id") == normalized_author_id: + return authorship.get("author_position", "unknown") + + return "unknown" + +def get_funders_by_author_id(author_id: str) -> dict: + page = 1 + page_size = 100 + funder_counts = {} + + normalized_author_id = ( + author_id if author_id.startswith("https://openalex.org/") + else f"https://openalex.org/{author_id}" + ) + + author_name = get_author_name(author_id) + + while True: + data = get_publications_by_author_id(author_id, page, page_size) + results = data.get("results", []) + + if not results: + break + + for hit in results: + publication = hit.get("_source", {}) + publication_id = publication.get("id") + author_position = get_author_position_in_publication(publication, author_id) + publication_funders = set() + + # Funders field + for funder in publication.get("funders", []): + funder_id = funder.get("id") + funder_name = funder.get("display_name") + + if funder_id or funder_name: + key = funder_id or funder_name + publication_funders.add((key, funder_id, funder_name)) + + # Count each funder only once per publication + for key, funder_id, funder_name in publication_funders: + if key not in funder_counts: + funder_counts[key] = { + "funder_id": funder_id, + "funder_name": funder_name, + "publication_count": 0, + "author_position_count": { + "first": 0, + "middle": 0, + "last": 0, + "unknown": 0 + }, + "publication_ids": { + "first": [], + "middle": [], + "last": [], + "unknown": [] + } + } + + funder_counts[key]["publication_count"] += 1 + if author_position not in funder_counts[key]["author_position_count"]: + author_position = "unknown" + + funder_counts[key]["author_position_count"][author_position] += 1 + funder_counts[key]["publication_ids"][author_position].append(publication_id) + + if len(results) < page_size: + break + + page += 1 + + return { + "author_id": normalized_author_id, + "author_name": author_name, + "funder_count": len(funder_counts), + "funders": list(funder_counts.values()) + } \ No newline at end of file diff --git a/service/src/main/inse/service.py b/service/src/main/inse/service.py index 22a16a19..43a78e5f 100644 --- a/service/src/main/inse/service.py +++ b/service/src/main/inse/service.py @@ -179,3 +179,22 @@ def search_persons(author_id: Annotated[str, Query(description="OpenAlex author def generate_person_bio(author_id: Annotated[str, Query(description="OpenAlex author ID (e.g., A5093075883)")]): bio = logic.generate_person_bio(author_id) return model.PersonBio(bio=bio) + + +@app.get('/authors/{author_id}/publications', + operation_id='getPublicationsByAuthor', + summary='Get publications by author.', + tags=['Publications']) +def get_publications_by_author( + author_id: str, + page: int = 1, + page_size: int = 20 +): + return logic.get_publications_by_author_id(author_id, page, page_size) + +@app.get('/authors/{author_id}/funders', + operation_id='getFundersByAuthor', + summary='Get funding organizations by author.', + tags=['Funding']) +def get_funders_by_author(author_id: str): + return logic.get_funders_by_author_id(author_id) \ No newline at end of file