#!/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{e_commerce_customer_service, author = {Xing Tian}, title = {e_commerce_customer_service}, month = aug, year = 2023, publisher = {Xing Tian}, version = {1.0}, } """ class ECommerceCustomerService(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