From 953757a302bc13d906fc2d3aa155c264baba0604 Mon Sep 17 00:00:00 2001 From: Steven Tischer Date: Mon, 8 Jun 2026 19:39:41 +0200 Subject: [PATCH] Add new get_data file --- llm_evaluations/bio_generation/get_data_2.py | 136 +++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 llm_evaluations/bio_generation/get_data_2.py diff --git a/llm_evaluations/bio_generation/get_data_2.py b/llm_evaluations/bio_generation/get_data_2.py new file mode 100644 index 00000000..bbaf27b4 --- /dev/null +++ b/llm_evaluations/bio_generation/get_data_2.py @@ -0,0 +1,136 @@ +import requests, json +import helpers + +# AUTHOR_OPEN_ALEXID = helpers.get_all_author_ids_from_specific_university("University of Duisburg-Essen") +AUTHOR_OPEN_ALEXID = ["A5007170803"] + +ANZAHL_TOP_TOPICS = 5 +WORKS_PAGE_SIZE = 100 + +ES_HEADERS = { + "Authorization": f"ApiKey {helpers.ELASTICSEARCH_API_KEY}", + "Content-Type": "application/json", +} + + +def write_data_to_file(author_id, data): + with open(f"data/{author_id}.json", "w") as f: + json.dump(data, f) + + +def fetch_author_display_name(author_id): + payload = { + "query": { + "query_string": { + "default_field": "_id", + "query": author_id, + } + }, + "_source": ["display_name"], + } + response = requests.post( + f"{helpers.ELASTICSEARCH_URL}/openalex-authors/_search", + json=payload, + headers=ES_HEADERS, + ) + response.raise_for_status() + + hits = response.json()["hits"]["hits"] + if len(hits) == 0: + return None + return hits[0]["_source"].get("display_name") + + +def fetch_author_works(author_id): + author_url = f"https://openalex.org/{author_id}" + authorship_query = { + "nested": { + "path": "authorships", + "query": { + "bool": { + "must": [{"term": {"authorships.author.id": author_url}}], + }, + }, + } + } + + all_works = [] + page = 0 + + while True: + payload = { + "query": authorship_query, + "_source": ["primary_topic"], + "from": page * WORKS_PAGE_SIZE, + "size": WORKS_PAGE_SIZE, + } + response = requests.post( + f"{helpers.ELASTICSEARCH_URL}/openalex-works*/_search", + json=payload, + headers=ES_HEADERS, + ) + response.raise_for_status() + + hits = response.json()["hits"]["hits"] + if len(hits) == 0: + break + + for hit in hits: + all_works.append(hit["_source"]) + + page += 1 + + return all_works + + +def count_primary_topics(works): + counts = {} + + for work in works: + primary_topic = work.get("primary_topic") + if not primary_topic: + continue + + name = primary_topic.get("display_name") + if not name: + continue + + if name not in counts: + counts[name] = { + "id": primary_topic.get("id"), + "display_name": name, + "count": 0, + } + counts[name]["count"] += 1 + + topics_sorted = sorted(counts.values(), key=lambda topic: topic["count"], reverse=True) + return topics_sorted[:ANZAHL_TOP_TOPICS] + + +def main(): + if len(AUTHOR_OPEN_ALEXID) == 0: + print("Keine Autoren-IDs gefunden. Abbruch!") + raise SystemExit(1) + + for author_id in AUTHOR_OPEN_ALEXID: + print(f"Rufe Daten für Autor: {author_id} ab...") + display_name = fetch_author_display_name(author_id) + if display_name is None: + print(f"Keine Daten für {author_id} gefunden... überspringe.\n") + continue + + works = fetch_author_works(author_id) + top_topics = count_primary_topics(works) + + author_data = { + "display_name": display_name, + "topics": top_topics, + } + + print(f"Schreibe Daten für Autor: {display_name}...") + write_data_to_file(author_id, author_data) + print("Nächster Autor...\n") + + +if __name__ == "__main__": + main()