| """XMarket retrieval dataset for multiple languages""" | |
| import json | |
| import gzip | |
| from typing import List | |
| import datasets | |
| _DESCRIPTION = """\ | |
| An ecommerce category to product retrieval dataset in multiple languages. | |
| The data comes from: | |
| https://xmrec.github.io/ | |
| """ | |
| _HOMEPAGE_URL = 'https://xmrec.github.io/' | |
| _LANGUAGES = {'es': 'es', 'de': 'de', 'en': 'us'} | |
| _VERSION = '1.0.0' | |
| URLS = [ | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Arts_Crafts_and_Sewing/metadata_%s_Arts_Crafts_and_Sewing.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Automotive/metadata_%s_Automotive.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Books/metadata_%s_Books.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/CDs_and_Vinyl/metadata_%s_CDs_and_Vinyl.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Cell_Phones_and_Accessories/metadata_%s_Cell_Phones_and_Accessories.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Digital_Music/metadata_%s_Digital_Music.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Electronics/metadata_%s_Electronics.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Grocery_and_Gourmet_Food/metadata_%s_Grocery_and_Gourmet_Food.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Home_and_Kitchen/metadata_%s_Home_and_Kitchen.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Industrial_and_Scientific/metadata_%s_Industrial_and_Scientific.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Kindle_Store/metadata_%s_Kindle_Store.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Movies_and_TV/metadata_%s_Movies_and_TV.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Musical_Instruments/metadata_%s_Musical_Instruments.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Office_Products/metadata_%s_Office_Products.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Sports_and_Outdoors/metadata_%s_Sports_and_Outdoors.json.gz', | |
| 'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Toys_and_Games/metadata_%s_Toys_and_Games.json.gz', | |
| ] | |
| class XMarketMLConfig(datasets.BuilderConfig): | |
| """BuilderConfig for XMarket.""" | |
| def __init__(self, languages=None, **kwargs): | |
| super(XMarketMLConfig, self).__init__( | |
| version=datasets.Version(_VERSION, ''), **kwargs | |
| ) | |
| self.languages = languages | |
| class XMarketML(datasets.GeneratorBasedBuilder): | |
| """The XMarket category to product retrieval dataset""" | |
| BUILDER_CONFIGS = [ | |
| XMarketMLConfig( | |
| name=f"{name}-{lang}", | |
| description=f'{name.title()} of the multilingual XMarket dataset.', | |
| languages=None, | |
| ) | |
| for name in ['corpus', 'queries', 'qrels'] for lang in _LANGUAGES | |
| ] | |
| BUILDER_CONFIG_CLASS = XMarketMLConfig | |
| def __init__(self, *args, **kwargs): | |
| super().__init__(*args, **kwargs) | |
| self._data = None | |
| def _info(self): | |
| return datasets.DatasetInfo( | |
| description=_DESCRIPTION, | |
| features=datasets.Features( | |
| { | |
| "_id": datasets.Value("string"), | |
| "title": datasets.Value("string"), | |
| "text": datasets.Value("string"), | |
| } | |
| ), | |
| supervised_keys=None, | |
| homepage=_HOMEPAGE_URL, | |
| ) | |
| def _split_generators(self, dl_manager: datasets.DownloadManager): | |
| json_files = [] | |
| for lang in self.config.languages: | |
| url_lang = _LANGUAGES[lang] | |
| for url in URLS: | |
| json_files.append(dl_manager.download(url % (url_lang, url_lang))) | |
| return [ | |
| datasets.SplitGenerator( | |
| name=datasets.Split.TEST, | |
| gen_kwargs={ | |
| 'files': json_files, | |
| 'split': 'test', | |
| }, | |
| ), | |
| ] | |
| def _generate_examples( | |
| self, | |
| files: List[str], | |
| split: str = None, | |
| ): | |
| if not self._data: | |
| corpus = [] | |
| queries = dict() | |
| qrels = dict() | |
| for file in files: | |
| with gzip.open(file, 'rt') as f: | |
| for line in f: | |
| product = json.loads(line) | |
| corpus.append( | |
| { | |
| "_id": product['asin'], | |
| "title": product['title'], | |
| "text": product['description'], | |
| } | |
| ) | |
| for category in product['categories']: | |
| if category in queries: | |
| qid = queries[category] | |
| qrels[qid].append(product['asin']) | |
| else: | |
| qid = f'q{len(queries)}' | |
| queries[category] = qid | |
| qrels[qid] = [product['asin']] | |
| self._data = { | |
| 'corpus': corpus, | |
| 'queries': queries, | |
| 'qrels': qrels, | |
| } | |
| if self.config.name.startswith('corpus'): | |
| for line in self._data['corpus']: | |
| yield line['_id'], line | |
| elif self.config.name.startswith('queries'): | |
| for query, qid in queries.items(): | |
| yield qid, { | |
| "_id": qid, | |
| "title": '', | |
| "text": query, | |
| } | |
| elif self.config.name.startswith('qrels'): | |
| for qid, dids in qrels.items(): | |
| yield qid, { | |
| "_id": qid, | |
| "title": '', | |
| "text": ' '.join(dids), | |
| } | |
| else: | |
| raise ValueError(f'Unknown config name: {self.config.name}') | |