File size: 3,179 Bytes
71af8ac
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
import datasets

_DESCRIPTION = "Catalan chatbot conversations from 10 different domains"
_HOMEPAGE = "https://zenodo.org/records/7276036"
_LICENSE  = "CC BY 4.0"
_VERSION = "1.0.0"

_DATAPATH = "KitKat.json"

_CATEGORIES = [
    "allotjament",
    "banca",
    "menjar_domicili",
    "comerc",
    "lloguer_vehicles",
    "transports",
    "assegurances",
    "ajuntament",
    "clinica",
    "telefonia",
]

class KitKat(datasets.GeneratorBasedBuilder):
    """ KitKat: Conversacional dataset for Catalan. """

    VERSION = datasets.Version(_VERSION)

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

    @staticmethod
    def _info():
        features = datasets.Features(
            {
                "id": datasets.Value("string"),
                "category": datasets.Value("string"),
                "conversation": [
                    {
                        # "id": datasets.Value("string"),
                        "role": datasets.Value("string"),
                        "text": datasets.Value("string"),
                        "intent": datasets.Value("string"),
                        "slots": [
                            {
                                "tag": datasets.Value("string"),
                                "text": datasets.Value("string"),
                                "Start_char": datasets.Value("int32"),
                                "End_char": datasets.Value("int32"),
                            }
                        ]
                    }
                ]
            }
        )
        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:
            data = json.load(f)

        _key = 0
        for category,conversations in data["xats"].items():
            if self.config.name in ["all", category]:
                for conversation in conversations:
                    yield _key, {
                        "id": conversation["id"], # conversation["número"],
                        "category": category,
                        # "conversation": [{"id": x["id"], "role":x["actor"], "text":x["text"], "slots":x["slots"], "intent":x["intent"]} for x in conversation["frases"]]
                        "conversation": [{"role":x["actor"], "text":x["text"], "slots":x["slots"], "intent":x["intent"]} for x in conversation["frases"]]
                    }
                    _key+=1