366 lines
11 KiB
Python
366 lines
11 KiB
Python
from __future__ import annotations
|
|
|
|
import argparse
|
|
import re
|
|
from collections import Counter
|
|
|
|
import requests
|
|
|
|
def search(
|
|
query: dict,
|
|
aggs: dict | None = None,
|
|
size: int = 0,
|
|
sort: list | None = None,
|
|
source: list | None = None,
|
|
search_after: list | None = None,
|
|
) -> dict:
|
|
body = {"size": size, "query": query}
|
|
if aggs is not None:
|
|
body["aggs"] = aggs
|
|
if sort is not None:
|
|
body["sort"] = sort
|
|
if source is not None:
|
|
body["_source"] = source
|
|
if search_after is not None:
|
|
body["search_after"] = search_after
|
|
|
|
response = requests.post(
|
|
f"https://project.nes.uni-due.de/elastic/openalex-authors/_search",
|
|
headers={
|
|
"Authorization": f"ApiKey am1wWXBaMEJEVWJoZkE5MnBaUlk6TWlvbW1LRlMzNjdDUHgzYmVkUmI5UQ",
|
|
"Content-Type": "application/json",
|
|
},
|
|
json=body,
|
|
timeout=60,
|
|
)
|
|
|
|
response.raise_for_status()
|
|
|
|
return response.json()
|
|
|
|
|
|
def institution_filter(institution_id: str | None) -> dict | None:
|
|
|
|
if not institution_id:
|
|
return None
|
|
|
|
full_id = (
|
|
institution_id
|
|
if institution_id.startswith("http")
|
|
else f"https://openalex.org/{institution_id}"
|
|
)
|
|
|
|
return {
|
|
"nested": {
|
|
"path": "affiliations",
|
|
"query": {
|
|
"term": {"affiliations.institution.lineage": full_id},
|
|
},
|
|
},
|
|
}
|
|
|
|
|
|
def with_institution_filter(base_query: dict, institution_id: str | None) -> dict:
|
|
inst_filter = institution_filter(institution_id)
|
|
if inst_filter is None:
|
|
return base_query
|
|
return {"bool": {"filter": [base_query, inst_filter]}}
|
|
|
|
|
|
def fetch_analysis_1_data(institution_id: str | None = None) -> list[dict]:
|
|
|
|
result = search(
|
|
query=with_institution_filter({"exists": {"field": "orcid"}}, institution_id),
|
|
aggs={
|
|
"by_orcid": {
|
|
"terms": {
|
|
"field": "orcid",
|
|
"size": 500,
|
|
"min_doc_count": 2,
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
buckets = result["aggregations"]["by_orcid"]["buckets"]
|
|
|
|
return [
|
|
{"orcid": bucket["key"], "author_count": bucket["doc_count"]}
|
|
for bucket in buckets
|
|
]
|
|
|
|
|
|
def fetch_analysis_2_data(institution_id: str | None = None) -> list[dict]:
|
|
|
|
result = search(
|
|
query=with_institution_filter({"exists": {"field": "orcid"}}, institution_id),
|
|
aggs={
|
|
"by_orcid": {
|
|
"terms": {
|
|
"field": "orcid",
|
|
"size": 500,
|
|
"min_doc_count": 2,
|
|
},
|
|
"aggs": {
|
|
"unique_names": {
|
|
"cardinality": {"field": "display_name.keyword"},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
buckets = result["aggregations"]["by_orcid"]["buckets"]
|
|
|
|
return [
|
|
{
|
|
"orcid": bucket["key"],
|
|
"author_count": bucket["doc_count"],
|
|
"unique_names": bucket["unique_names"]["value"],
|
|
}
|
|
for bucket in buckets
|
|
]
|
|
|
|
|
|
def fetch_analysis_3_data(limit: int = 50, institution_id: str | None = None) -> list[dict]:
|
|
|
|
result = search(
|
|
query=with_institution_filter(
|
|
{"exists": {"field": "display_name"}}, institution_id
|
|
),
|
|
size=limit,
|
|
sort=[
|
|
{
|
|
"_script": {
|
|
"type": "number",
|
|
"script": {
|
|
"lang": "painless",
|
|
"source": "doc['display_name.keyword'].value.length()",
|
|
},
|
|
"order": "desc",
|
|
},
|
|
},
|
|
],
|
|
source=["display_name"],
|
|
)
|
|
|
|
hits = result["hits"]["hits"]
|
|
|
|
return [
|
|
{
|
|
"display_name": hit["_source"]["display_name"],
|
|
"length": len(hit["_source"]["display_name"]),
|
|
}
|
|
for hit in hits
|
|
]
|
|
|
|
|
|
def fetch_analysis_4_data(institution_id: str | None = None) -> list[dict]:
|
|
|
|
emoji_ranges = "☀-➿⬀-⯿\U0001F000-\U0001FFFF"
|
|
pattern = f".*[{emoji_ranges}].*"
|
|
emoji_pattern = re.compile(f"[{emoji_ranges}]")
|
|
|
|
query = with_institution_filter(
|
|
{"regexp": {"display_name.keyword": {"value": pattern}}}, institution_id
|
|
)
|
|
sort = [{"id": "asc"}]
|
|
page_size = 1000
|
|
|
|
rows = []
|
|
search_after = None
|
|
|
|
while True:
|
|
result = search(
|
|
query=query,
|
|
size=page_size,
|
|
sort=sort,
|
|
source=["id", "display_name"],
|
|
search_after=search_after,
|
|
)
|
|
|
|
hits = result["hits"]["hits"]
|
|
if not hits:
|
|
break
|
|
|
|
for hit in hits:
|
|
author_id = hit["_source"]["id"].rsplit("/", 1)[-1]
|
|
display_name = hit["_source"]["display_name"]
|
|
for emoji in set(emoji_pattern.findall(display_name)):
|
|
rows.append({"emoji": emoji, "author_id": author_id})
|
|
|
|
if len(hits) < page_size:
|
|
break
|
|
search_after = hits[-1]["sort"]
|
|
|
|
return rows
|
|
|
|
|
|
def fetch_analysis_5_data(institution_id: str | None = None) -> list[dict]:
|
|
|
|
result = search(
|
|
query=with_institution_filter(
|
|
{"exists": {"field": "display_name"}}, institution_id
|
|
),
|
|
aggs={
|
|
"by_display_name": {
|
|
"terms": {
|
|
"field": "display_name.keyword",
|
|
"size": 500,
|
|
"min_doc_count": 2,
|
|
},
|
|
},
|
|
},
|
|
)
|
|
|
|
buckets = result["aggregations"]["by_display_name"]["buckets"]
|
|
|
|
return [
|
|
{"display_name": bucket["key"], "author_count": bucket["doc_count"]}
|
|
for bucket in buckets
|
|
]
|
|
|
|
|
|
def print_table(headers: list[str], rows: list[list[str]]) -> None:
|
|
widths = [
|
|
max(len(str(cell)) for cell in [header, *(row[i] for row in rows)])
|
|
for i, header in enumerate(headers)
|
|
]
|
|
|
|
def print_row(cells: list[str]) -> None:
|
|
print(" | ".join(str(cell).ljust(width) for cell, width in zip(cells, widths)))
|
|
|
|
print_row(headers)
|
|
print_row(["-" * width for width in widths])
|
|
for row in rows:
|
|
print_row(row)
|
|
|
|
|
|
def run_analysis_1(institution_id: str | None = None) -> None:
|
|
print("Analyse 1: ORCIDs, die von 2 oder mehr Autoren-Dokumenten geteilt werden.")
|
|
analysis_1_data = fetch_analysis_1_data(institution_id)
|
|
analysis_1_data.sort(key=lambda group: group["author_count"], reverse=True)
|
|
print_table(
|
|
["ORCID", "Anzahl Autoren"],
|
|
[[group["orcid"], group["author_count"]] for group in analysis_1_data],
|
|
)
|
|
|
|
|
|
def run_analysis_2(institution_id: str | None = None) -> None:
|
|
print(
|
|
"Analyse 2: Wie Analyse 1, zusätzlich mit der Anzahl unterschiedlicher "
|
|
"display_names je ORCID (exakter Match) — trennt echte Namens-Duplikate "
|
|
"von unterschiedlich benannten Autoren mit derselben ORCID."
|
|
)
|
|
analysis_2_data = fetch_analysis_2_data(institution_id)
|
|
analysis_2_data.sort(key=lambda group: group["author_count"], reverse=True)
|
|
print_table(
|
|
["ORCID", "Gesamt", "Unique Names"],
|
|
[
|
|
[group["orcid"], group["author_count"], group["unique_names"]]
|
|
for group in analysis_2_data
|
|
],
|
|
)
|
|
|
|
|
|
def run_analysis_3(institution_id: str | None = None) -> None:
|
|
print(
|
|
"Analyse 3: Die 50 Autoren mit den längsten display_names im gesamten "
|
|
"Autoren-Index, absteigend nach Zeichenlänge sortiert."
|
|
)
|
|
analysis_3_data = fetch_analysis_3_data(limit=50, institution_id=institution_id)
|
|
analysis_3_data.sort(key=lambda entry: entry["length"], reverse=True)
|
|
print_table(
|
|
["Display Name", "Länge"],
|
|
[[entry["display_name"], entry["length"]] for entry in analysis_3_data],
|
|
)
|
|
|
|
|
|
def run_analysis_4(institution_id: str | None = None) -> None:
|
|
max_examples_per_emoji = 5
|
|
print(
|
|
"Analyse 4: Vollständiger Scan über den gesamten Autoren-Index — für "
|
|
"jeden Autor mit einem Emoji im display_name eine Zeile je gefundenem "
|
|
"Emoji (deckt die gängigen Emoji-Unicode-Blöcke ab, nicht zu 100% "
|
|
f"vollständig). Anzahl der Einträge ist die echte Gesamtzahl, pro Emoji "
|
|
f"werden aber nur {max_examples_per_emoji} Beispiel-Autoren angezeigt."
|
|
)
|
|
analysis_4_data = fetch_analysis_4_data(institution_id)
|
|
emoji_counts = Counter(row["emoji"] for row in analysis_4_data)
|
|
analysis_4_data.sort(
|
|
key=lambda row: (-emoji_counts[row["emoji"]], row["emoji"], row["author_id"])
|
|
)
|
|
|
|
table_rows = []
|
|
examples_shown = Counter()
|
|
for row in analysis_4_data:
|
|
emoji = row["emoji"]
|
|
if examples_shown[emoji] >= max_examples_per_emoji:
|
|
continue
|
|
examples_shown[emoji] += 1
|
|
table_rows.append([emoji, emoji_counts[emoji], row["author_id"]])
|
|
|
|
print_table(["Emoji", "Anzahl der Einträge", "OpenAlexID des Authors"], table_rows)
|
|
|
|
|
|
def run_analysis_5(institution_id: str | None = None) -> None:
|
|
print(
|
|
"Analyse 5: Count-by auf display_name — wie viele Autoren-Dokumente "
|
|
"denselben display_name (exakter Match) teilen."
|
|
)
|
|
analysis_5_data = fetch_analysis_5_data(institution_id)
|
|
analysis_5_data.sort(key=lambda group: group["author_count"], reverse=True)
|
|
print_table(
|
|
["Display Name", "Anzahl"],
|
|
[[group["display_name"], group["author_count"]] for group in analysis_5_data],
|
|
)
|
|
|
|
|
|
ANALYSES = {
|
|
1: run_analysis_1,
|
|
2: run_analysis_2,
|
|
3: run_analysis_3,
|
|
4: run_analysis_4,
|
|
5: run_analysis_5,
|
|
}
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
parser = argparse.ArgumentParser(
|
|
description="Analysen über den openalex-authors Elasticsearch-Index.",
|
|
)
|
|
parser.add_argument(
|
|
"--analysis",
|
|
type=int,
|
|
choices=sorted(ANALYSES),
|
|
help="Nur diese Analyse ausführen (1-5). Ohne Angabe werden alle ausgeführt.",
|
|
)
|
|
parser.add_argument(
|
|
"--filter-uni",
|
|
metavar="INSTITUTION_ID",
|
|
help="OpenAlex-Institutions-ID (z.B. I62318514), auf die alle Suchen "
|
|
"gefiltert werden — inklusive Autoren, die nur einer Unter-Einheit "
|
|
"(z.B. einem Institut) dieser Institution zugeordnet sind.",
|
|
)
|
|
return parser.parse_args()
|
|
|
|
|
|
def main() -> None:
|
|
args = parse_args()
|
|
|
|
if args.filter_uni:
|
|
print(f"Filter: nur Institution {args.filter_uni} (inkl. Unter-Einheiten)\n")
|
|
|
|
if args.analysis is not None:
|
|
ANALYSES[args.analysis](args.filter_uni)
|
|
return
|
|
|
|
for number, run_analysis in sorted(ANALYSES.items()):
|
|
if number > 1:
|
|
print()
|
|
run_analysis(args.filter_uni)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|