File size: 3,298 Bytes
0213f44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
import csv

import datasets
from datasets.tasks import Summarization


logger = datasets.logging.get_logger(__name__)


_CITATION = """\
@inproceedings{hasan-etal-2021-xl,
    title = "{XL}-Sum: Large-Scale Multilingual Abstractive Summarization for 44 Languages",
    author = "Hasan, Tahmid  and
      Bhattacharjee, Abhik  and
      Islam, Md. Saiful  and
      Mubasshir, Kazi  and
      Li, Yuan-Fang  and
      Kang, Yong-Bin  and
      Rahman, M. Sohel  and
      Shahriyar, Rifat",
    booktitle = "Findings of the Association for Computational Linguistics: ACL-IJCNLP 2021",
    month = aug,
    year = "2021",
    address = "Online",
    publisher = "Association for Computational Linguistics",
    url = "https://aclanthology.org/2021.findings-acl.413",
    pages = "4693--4703",
}
"""

_DESCRIPTION = """Persian portion of the XLSum Dataset"""

_DOWNLOAD_URLS = {
    "train": "https://huggingface.co/datasets/hezarai/xlsum-fa/resolve/main/xlsum-fa_train.csv",
    "test": "https://huggingface.co/datasets/hezarai/xlsum-fa/resolve/main/xlsum-fa_test.csv",
}


class XLSumFaConfig(datasets.BuilderConfig):
    def __init__(self, **kwargs):
        super(XLSumFaConfig, self).__init__(**kwargs)


class XLSumFa(datasets.GeneratorBasedBuilder):
    BUILDER_CONFIGS = [
        XLSumFaConfig(
            name="xlsum-fa",
            version=datasets.Version("1.0.0"),
            description=_DESCRIPTION,
        ),
    ]

    def _info(self):
        text_column = "text"
        summary_column = "summary"
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {text_column: datasets.Value("string"),
                 summary_column: datasets.features.Value("string")}
            ),
            homepage="https://huggingface.co/datasets/hezarai/xlsum-fa",
            citation=_CITATION,
            task_templates=[Summarization(text_column=text_column, summary_column=summary_column)],
        )

    def _split_generators(self, dl_manager):
        """
        Returns SplitGenerators.
        """
        train_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["train"])
        test_path = dl_manager.download_and_extract(_DOWNLOAD_URLS["test"])

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN, gen_kwargs={"filepath": train_path}
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST, gen_kwargs={"filepath": test_path}
            ),
        ]

    def _generate_examples(self, filepath):
        """
        Per each file_path read the csv file and iterate it.
        For each row yield a tuple of (id, {"text": ..., "summary": ..., ...})
        Each call to this method yields an output like below:
        ```
        (123, {"text": "...", "summary": "..."})
        ```
        """
        logger.info("⏳ Generating examples from = %s", filepath)
        with open(filepath, encoding="utf-8") as csv_file:
            csv_reader = csv.reader(
                csv_file, quotechar='"', skipinitialspace=True
            )

            next(csv_reader, None)

            for id_, row in enumerate(csv_reader):
                text, label = row
                yield id_, {"text": text, "summary": label}