Page 1 of 1

Verständnis von Wikipedia -Titeln, die API batchieren

Posted: 12 Feb 2025, 12:02
by Anonymous
Mit der Mediawiki -API können wir die Wikipedia -API abfragen. Eines der Felder sind Titel wobei ein oder mehr Titel gleichzeitig abgefragt werden kann. Das Zusammenstellen von Szenarien mit hoher Last wird empfohlen, um mehrere aufeinanderfolgende Anfragen zu vermeiden. Mehrere Titel sollten durch ein Rohr | Zeichen getrennt werden. Nehmen wir an, ich habe eine englische Kategorie "Antike", ich möchte die entsprechende Kategorie in einer anderen Sprache finden. Dies ist möglich, indem Sie die API für die Prop-Langlinks . Verwenden Sie keine Batching, aber wenn ich batching verwende, bekomme ich nicht immer alle Ergebnisse zurück. Zur Veranschaulichung habe ich eine Liste englischer Kategorien, und bei jeder Iteration verarbeite ich einen Element mehr als zuvor (beginnend mit nur einem). Bei der Charge wird klar, dass bei größeren Listen (immer noch gut innerhalb der von der API auferlegten Grenze von 50) die früheren Kategorien verloren gehen und nicht mehr enthalten sind. Wenn Sie Batching nicht verwenden (Batch Size = 1), tritt dieses Problem nicht auf.import requests

def get_translated_category(category_titles: str | list[str], target_lang: str, batch_size: int = 50) -> list[str]:
"""Fetch the translated equivalent of a Wikipedia category."""

endpoint = "https://en.wikipedia.org/w/api.php"
if isinstance(category_titles, str):
category_titles = [category_titles]

category_titles = [f"Category:{title}" for title in category_titles]

translated_categories = {}
# API is limited to 50 titles per request
for start_idx in range(0, len(category_titles), batch_size):
end_idx = start_idx + batch_size
batch_titles = category_titles[start_idx:end_idx]
params = {
"action": "query",
"format": "json",
"prop": "langlinks",
"titles": "|".join(batch_titles),
"lllimit": "max"
}

response = requests.get(endpoint, params=params)
data = response.json()

pages = data.get("query", {}).get("pages", {})
for page_data in pages.values():
title = page_data["title"].split(":")[-1]
if title in translated_categories:
print("We already found this category title!")
langlinks = page_data.get("langlinks", [])
for link in langlinks:
if link["lang"] == target_lang:
translated_categories[title] = link["*"].split(":")[-1]

return translated_categories

if __name__ == "__main__":
english_categories: list[str] = [
"Classical antiquity",
"Late antiquity",
"Latin-language literature",
"Roman Kingdom",
"Roman Republic",
"Roman Empire",
"Byzantine Empire",
"Latin language",
"Ancient Greek",
"Ancient Greece",
"Ancient Greek literature",
"Medieval history of Greece",
]

print("Batch size 50 (default)")
for idx in range(len(english_categories)):
categories = english_categories[:idx+1]
latin_categories = get_translated_category(categories, "la")
print(latin_categories)

print()
print("Batch size 1 (no batching)")
for idx in range(len(english_categories)):
categories = english_categories[:idx+1]
latin_categories = get_translated_category(categories, "la", batch_size=1)
print(latin_categories)
< /code>
Die Ausgabe des obigen Codes lautet: < /p>
Batch size 50 (default)
{'Classical antiquity': 'Res classicae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum'}
{'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum'}
{'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Latin language': 'Lingua Latina', 'Roman Empire': 'Imperium Romanum'}
{'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Latin language': 'Lingua Latina', 'Roman Empire': 'Imperium Romanum'}
{'Ancient Greece': 'Graecia antiqua', 'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Roman Empire': 'Imperium Romanum'}
{'Ancient Greece': 'Graecia antiqua', 'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Roman Empire': 'Imperium Romanum'}
{'Ancient Greece': 'Graecia antiqua', 'Byzantine Empire': 'Imperium Byzantinum', 'Late antiquity': 'Antiquitas Posterior', 'Roman Empire': 'Imperium Romanum'}

Batch size 1 (no batching)
{'Classical antiquity': 'Res classicae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina', 'Ancient Greek': 'Lingua Graeca antiqua'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina', 'Ancient Greek': 'Lingua Graeca antiqua', 'Ancient Greece': 'Graecia antiqua'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina', 'Ancient Greek': 'Lingua Graeca antiqua', 'Ancient Greece': 'Graecia antiqua', 'Ancient Greek literature': 'Litterae Graecae antiquae'}
{'Classical antiquity': 'Res classicae', 'Late antiquity': 'Antiquitas Posterior', 'Latin-language literature': 'Litterae Latinae', 'Roman Empire': 'Imperium Romanum', 'Byzantine Empire': 'Imperium Byzantinum', 'Latin language': 'Lingua Latina', 'Ancient Greek': 'Lingua Graeca antiqua', 'Ancient Greece': 'Graecia antiqua', 'Ancient Greek literature': 'Litterae Graecae antiquae'}
< /code>
Es sollte sofort klar sein, dass es einen Unterschied zwischen Charge und nicht Verwendung von Chargen und mehr besorgniserregender Verwendung gibt, dass die Verwendung von Stapeln dazu führt, dass einige Elemente verworfen werden. Ich dachte, dass dies vielleicht der Fall wäre, wenn Kategorien lateinisch verschmolzen werden und denselben Namen haben. Daher beschließt die API, nur einen von ihnen zurückzugeben, aber soweit ich das beurteilen kann, ist dies nicht der Fall. < BR /> Wie kann ich sicherstellen, dass ich meine Anfragen (Titel) zusammengefügt habe, die ich mit der Wikipedia -API mit den gleichen Ergebnissen wie ich einzelne Anfragen entlassen kann?>