File size: 3,858 Bytes
5b1b3c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
104
105
106
107
108
109
110
111
112
113
114
import os
import re
import csv
import json
import datasets

_DESCRIPTION = "Spanish articles from WikiHow"
_HOMEPAGE = "https://www.wikihow.com"
_LICENSE  = "CC BY-NC-SA 3.0"
_VERSION = "1.1.0"

_DATAPATH = "wikihow_es.jsonl"

_CATEGORIES = [
    "salud",
    "viajes",
    "deportes",
    "relaciones",
    "pasatiempos",
    "adolescentes",
    "vida-familiar",
    "en-el-trabajo",
    "comida-y-diversión",
    "finanzas-y-negocios",
    "mascotas-y-animales",
    "carreras-y-educación",
    "filosofía-y-religión",
    "arte-y-entretenimiento",
    "en-la-casa-y-el-jardín",
    "cuidado-y-estilo-personal",
    "computadoras-y-electrónica",
    "días-de-fiesta-y-tradiciones",
    "automóviles-y-otros-vehículos",
]

def format_methods(methods, short=False):
    EOL = "\n" if short else "\n\n"
    formatted = []
    for method in methods:
        if method["title"].lower() != "pasos":
            content = f"Método {method['number']}: {method['title']}{EOL}"
        else:
            content = f"Sigue los siguientes pasos:{EOL}"
        for step in method["steps"]:
            step_content = re.sub(r"\n+", "\n", step).strip()
            if short:
                step_content = step_content.split("\n")[0]
            content += step_content + EOL
        formatted.append(content.strip())
    return formatted


class WikiHowEs(datasets.GeneratorBasedBuilder):
    """ WikiHowEs: Collection of Spanish tutorials. """

    VERSION = datasets.Version(_VERSION)

    DEFAULT_CONFIG_NAME = "all"
    BUILDER_CONFIGS = [datasets.BuilderConfig(name="all", version=VERSION, description="All articles from WikiHow-ES.")]
    for _CAT in _CATEGORIES:
        BUILDER_CONFIGS.append(
            datasets.BuilderConfig(name=_CAT, version=VERSION, description=f"Articles from the category {_CAT}")
        )

    @staticmethod
    def _info():
        features = datasets.Features(
            {
                "category": datasets.Value("string"),
                "question": datasets.Value("string"),
                "introduction": datasets.Value("string"),
                "answers": datasets.features.Sequence(datasets.Value("string")),
                "short_answers": datasets.features.Sequence(datasets.Value("string")),
                "url": datasets.Value("string"),
                "num_answers": datasets.Value("int32"),
                "num_refs": datasets.Value("int32"),
                "expert_author": datasets.Value("bool"),
            }
        )
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
        )

    @staticmethod
    def _split_generators(dl_manager):
        data_dir = dl_manager.download_and_extract(_DATAPATH)
        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepath": data_dir,
                },
            ),
        ]

    def _generate_examples(self, filepath):
        with open(filepath, encoding="utf-8") as f:
            for key, row in enumerate(f):
                data = json.loads(row)
                if self.config.name in ["all", data["category"].lower()]:
                    yield key, {
                        "category": data["category"],
                        "question": f"¿{data['title']}?",
                        "introduction": data["intro"],
                        "answers": format_methods(data["methods"], short=False),
                        "short_answers": format_methods(data["methods"], short=True),
                        "num_answers": data["num_methods"],
                        "num_refs": data["num_refs"],
                        "expert_author": data["expert_author"],
                        "url": data["url"],
                    }