Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added multilingual support for the Wikipedia reader. #12616

Merged
merged 2 commits into from
Apr 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Simple reader that reads wikipedia."""

from typing import Any, List

from llama_index.core.readers.base import BasePydanticReader
Expand Down Expand Up @@ -27,15 +28,26 @@ def __init__(self) -> None:
def class_name(cls) -> str:
return "WikipediaReader"

def load_data(self, pages: List[str], **load_kwargs: Any) -> List[Document]:
def load_data(
self, pages: List[str], lang_prefix: str = "en", **load_kwargs: Any
) -> List[Document]:
"""Load data from the input directory.

Args:
pages (List[str]): List of pages to read.

lang_prefix (str): Language prefix for Wikipedia. Defaults to English. Valid Wikipedia language codes
can be found at https://en.wikipedia.org/wiki/List_of_Wikipedias.
"""
import wikipedia

if lang_prefix.lower() != "en":
if lang_prefix.lower() in wikipedia.languages():
wikipedia.set_lang(lang_prefix.lower())
else:
raise ValueError(
f"Language prefix '{lang_prefix}' for Wikipedia is not supported. Check supported languages at https://en.wikipedia.org/wiki/List_of_Wikipedias."
)

results = []
for page in pages:
wiki_page = wikipedia.page(page, **load_kwargs)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ license = "MIT"
maintainers = ["jerryjliu"]
name = "llama-index-readers-wikipedia"
readme = "README.md"
version = "0.1.3"
version = "0.1.4"

[tool.poetry.dependencies]
python = ">=3.8.1,<4.0"
Expand Down