import json from dataclasses import dataclass from string import Template import datasets _CITATION = "" # TODO: @theyorubayesian _DESCRIPTION = \ """ A collection of passages culled from news websites for Cross-Lingual Information Retrieval for African Languages. """ _HOMEPAGE = "https://github.com/ciral/ciral-corpus" _LICENSE = "Apache License 2.0" _VERSION = "1.0.0" _LANGUAGES = [ "hausa", "somali", "swahili", "yoruba", "combined" ] _DATASET_URL = Template("./${mode}passages-v1.0/${language}_passages.jsonl") @dataclass class CiralConfig(datasets.BuilderConfig): translated: bool = False file_stub_dict = { None: "", True: "translated-", False: "" } def get_file_url(self, language: str) -> str: return _DATASET_URL.substitute( mode=self.file_stub_dict.get(self.translated), language=language ) class CiralPassages(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ CiralConfig( version=datasets.Version(_VERSION), name=language, description=f"CIRAL passages for language: {language}" ) for language in _LANGUAGES ] DEFAULT_CONFIG_NAME = "combined" def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, citation=_CITATION, features=datasets.Features({ "docid": datasets.Value("string"), "title": datasets.Value("string"), "text": datasets.Value("string"), "url": datasets.Value("string") }), homepage=_HOMEPAGE, license=_LICENSE ) def _split_generators(self, dl_manager: datasets.DownloadManager): language = self.config.name if language == "combined": language_file = dl_manager.download_and_extract({ _language: self.config.get_file_url(language=_language) for _language in _LANGUAGES[:-1] }) splits = [ datasets.SplitGenerator( name=_language, gen_kwargs={"filepath": language_file[_language]} ) for _language in _LANGUAGES[:-1] ] else: language_file = dl_manager.download_and_extract( self.config.get_file_url(language=language)) splits = [ datasets.SplitGenerator( name="train", gen_kwargs={"filepath": language_file} ) ] return splits def _generate_examples(self, filepath: str): with open(filepath, encoding="utf-8") as f: for line in f: data = json.loads(line) yield data["docid"], data