File size: 4,511 Bytes
2d11779
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from glob import glob
import json
import os
from pathlib import Path

import datasets


_URLS = {
    "faq": "data/faq.json",
    "product": "data/product.jsonl",
}


_CITATION = """\
@dataset{early_media,
  author       = {Xing Tian},
  title        = {e_commerce_customer_service},
  month        = aug,
  year         = 2023,
  publisher    = {Xing Tian},
  version      = {1.0},
}
"""


class TelemarketingVoiceClassification(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(name="faq", version=VERSION, description="faq"),
        datasets.BuilderConfig(name="product", version=VERSION, description="product"),
    ]

    def _info(self):
        if self.config.name == "faq":
            features = datasets.Features(
                {
                    "url": datasets.Value("string"),
                    "question": datasets.Value("string"),
                    "answer": datasets.Value("string"),
                    "label": datasets.Value("string"),
                }
            )
        elif self.config.name == "product":
            features = datasets.Features(
                {
                    "title": datasets.Value("string"),
                    "brand": datasets.Value("string"),
                    "review": datasets.Value("string"),
                    "description": datasets.Value("string"),
                    "mpn": datasets.Value("string"),
                    "color": datasets.Sequence(datasets.Value("string")),
                    "size": datasets.Sequence(datasets.Value("string")),
                    "sku": datasets.Value("string"),
                    "ratingValue": datasets.Value("float32"),
                    "reviewCount": datasets.Value("int64"),
                    "overview": datasets.Value("string"),
                    "category": datasets.Value("string"),
                    "url": datasets.Value("string"),
                }
            )
        else:
            raise NotImplementedError

        return datasets.DatasetInfo(
            features=features,
            supervised_keys=None,
            homepage="",
            license="",
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators."""
        url = _URLS[self.config.name]
        dl_path = dl_manager.download_and_extract(url)
        archive_path = dl_path

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={"archive_path": archive_path, "split": "train"},
            ),
        ]

    def _generate_faq(self, archive_path, split):
        archive_path = Path(archive_path)

        with open(archive_path, "r", encoding="utf-8") as f:
            faq = json.load(f)

        idx = 0
        for qa in faq:
            yield idx, {
                "url": qa["url"],
                "question": qa["question"],
                "answer": qa["answer"],
                "label": qa["label"],
            }
            idx += 1

    def _generate_product(self, archive_path, split):
        archive_path = Path(archive_path)

        idx = 0
        with open(archive_path, "r", encoding="utf-8") as f:
            for row in f:
                row = json.loads(row)

                yield idx, {
                    "title": row["title"],
                    "brand": row["brand"],
                    "review": row["review"],
                    "description": row["description"],
                    "mpn": row["mpn"],
                    "color": row["color"],
                    "size": row["size"],
                    "sku": row["sku"],
                    "ratingValue": float(row["ratingValue"]) if row["ratingValue"] is not None else None,
                    "reviewCount": int(row["reviewCount"]) if row["reviewCount"] is not None else None,
                    "overview": row["overview"],
                    "category": row["category"],
                    "url": row["url"],
                }
                idx += 1

    def _generate_examples(self, archive_path, split):
        """Yields examples."""
        if self.config.name == "faq":
            return self._generate_faq(archive_path, split)
        elif self.config.name == "product":
            return self._generate_product(archive_path, split)
        else:
            raise NotImplementedError


if __name__ == '__main__':
    pass