File size: 2,837 Bytes
e9388ba
cbaa143
e9388ba
 
 
 
 
 
 
 
 
172a4ed
e9388ba
 
 
 
 
 
 
 
 
cbaa143
e9388ba
 
cbaa143
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e9388ba
 
cbaa143
e9388ba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cbaa143
e9388ba
 
 
 
 
 
 
 
 
 
cbaa143
e9388ba
 
 
 
 
 
 
cbaa143
 
e9388ba
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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