guenthermi
commited on
Commit
·
705db86
1
Parent(s):
4b180be
support multiple languages
Browse files- README.md +5 -0
- xmarket_ml.py +150 -0
README.md
ADDED
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# XMarket Category to Product Retrieval Dataset
|
2 |
+
|
3 |
+
An ecommerce category to product retrieval dataset in multiple languages.
|
4 |
+
The data comes from:
|
5 |
+
https://xmrec.github.io/
|
xmarket_ml.py
ADDED
@@ -0,0 +1,150 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
"""XMarket retrieval dataset for multiple languages"""
|
2 |
+
|
3 |
+
|
4 |
+
import json
|
5 |
+
import gzip
|
6 |
+
|
7 |
+
from typing import List
|
8 |
+
|
9 |
+
import datasets
|
10 |
+
|
11 |
+
_DESCRIPTION = """\
|
12 |
+
An ecommerce category to product retrieval dataset in multiple languages.
|
13 |
+
The data comes from:
|
14 |
+
https://xmrec.github.io/
|
15 |
+
"""
|
16 |
+
|
17 |
+
_HOMEPAGE_URL = 'https://xmrec.github.io/'
|
18 |
+
_LANGUAGES = {'es': 'ES', 'de': 'DE', 'en': 'EN'}
|
19 |
+
_VERSION = '1.0.0'
|
20 |
+
|
21 |
+
|
22 |
+
URLS = [
|
23 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Arts_Crafts_and_Sewing/metadata_%s_Arts_Crafts_and_Sewing.json.gz',
|
24 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Automotive/metadata_%s_Automotive.json.gz',
|
25 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Books/metadata_%s_Books.json.gz',
|
26 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/CDs_and_Vinyl/metadata_%s_CDs_and_Vinyl.json.gz',
|
27 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Cell_Phones_and_Accessories/metadata_%s_Cell_Phones_and_Accessories.json.gz',
|
28 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Digital_Music/metadata_%s_Digital_Music.json.gz',
|
29 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Electronics/metadata_%s_Electronics.json.gz',
|
30 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Grocery_and_Gourmet_Food/metadata_%s_Grocery_and_Gourmet_Food.json.gz',
|
31 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Home_and_Kitchen/metadata_%s_Home_and_Kitchen.json.gz',
|
32 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Industrial_and_Scientific/metadata_%s_Industrial_and_Scientific.json.gz',
|
33 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Kindle_Store/metadata_%s_Kindle_Store.json.gz',
|
34 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Movies_and_TV/metadata_%s_Movies_and_TV.json.gz',
|
35 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Musical_Instruments/metadata_%s_Musical_Instruments.json.gz',
|
36 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Office_Products/metadata_%s_Office_Products.json.gz',
|
37 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Sports_and_Outdoors/metadata_%s_Sports_and_Outdoors.json.gz',
|
38 |
+
'https://ciir.cs.umass.edu/downloads/XMarket/FULL/%s/Toys_and_Games/metadata_%s_Toys_and_Games.json.gz',
|
39 |
+
]
|
40 |
+
|
41 |
+
|
42 |
+
class XMarketMLConfig(datasets.BuilderConfig):
|
43 |
+
"""BuilderConfig for XMarketES."""
|
44 |
+
|
45 |
+
def __init__(self, languages=None, **kwargs):
|
46 |
+
super(XMarketMLConfig, self).__init__(
|
47 |
+
version=datasets.Version(_VERSION, ''), **kwargs
|
48 |
+
)
|
49 |
+
self.languages = languages
|
50 |
+
|
51 |
+
|
52 |
+
class XMarketML(datasets.GeneratorBasedBuilder):
|
53 |
+
"""The XMarketES category to product retrieval dataset"""
|
54 |
+
|
55 |
+
BUILDER_CONFIGS = [
|
56 |
+
XMarketMLConfig(
|
57 |
+
name=name,
|
58 |
+
description=f'{name.title()} of the multilingual XMarket dataset.',
|
59 |
+
languages=None,
|
60 |
+
)
|
61 |
+
for name in ['corpus', 'queries', 'qrels']
|
62 |
+
]
|
63 |
+
|
64 |
+
BUILDER_CONFIG_CLASS = XMarketMLConfig
|
65 |
+
|
66 |
+
def __init__(self, *args, **kwargs):
|
67 |
+
super().__init__(*args, **kwargs)
|
68 |
+
self._data = None
|
69 |
+
|
70 |
+
def _info(self):
|
71 |
+
return datasets.DatasetInfo(
|
72 |
+
description=_DESCRIPTION,
|
73 |
+
features=datasets.Features(
|
74 |
+
{
|
75 |
+
"_id": datasets.Value("string"),
|
76 |
+
"title": datasets.Value("string"),
|
77 |
+
"text": datasets.Value("string"),
|
78 |
+
}
|
79 |
+
),
|
80 |
+
supervised_keys=None,
|
81 |
+
homepage=_HOMEPAGE_URL,
|
82 |
+
)
|
83 |
+
|
84 |
+
def _split_generators(self, dl_manager: datasets.DownloadManager):
|
85 |
+
json_files = []
|
86 |
+
for lang in self.config.languages:
|
87 |
+
for url in URLS:
|
88 |
+
json_files.append(dl_manager.download(url % (lang, lang)))
|
89 |
+
return [
|
90 |
+
datasets.SplitGenerator(
|
91 |
+
name=datasets.Split.TEST,
|
92 |
+
gen_kwargs={
|
93 |
+
'files': json_files,
|
94 |
+
'split': 'test',
|
95 |
+
},
|
96 |
+
),
|
97 |
+
]
|
98 |
+
|
99 |
+
def _generate_examples(
|
100 |
+
self,
|
101 |
+
files: List[str],
|
102 |
+
split: str = None,
|
103 |
+
):
|
104 |
+
if not self._data:
|
105 |
+
corpus = []
|
106 |
+
queries = dict()
|
107 |
+
qrels = dict()
|
108 |
+
for file in files:
|
109 |
+
with gzip.open(file, 'rt') as f:
|
110 |
+
for line in f:
|
111 |
+
product = json.loads(line)
|
112 |
+
corpus.append(
|
113 |
+
{
|
114 |
+
"_id": product['asin'],
|
115 |
+
"title": product['title'],
|
116 |
+
"text": product['description'],
|
117 |
+
}
|
118 |
+
)
|
119 |
+
for category in product['categories']:
|
120 |
+
if category in queries:
|
121 |
+
qid = queries[category]
|
122 |
+
qrels[qid].append(product['asin'])
|
123 |
+
else:
|
124 |
+
qid = f'q{len(queries)}'
|
125 |
+
queries[category] = qid
|
126 |
+
qrels[qid] = [product['asin']]
|
127 |
+
self._data = {
|
128 |
+
'corpus': corpus,
|
129 |
+
'queries': queries,
|
130 |
+
'qrels': qrels,
|
131 |
+
}
|
132 |
+
if self.config.name == 'corpus':
|
133 |
+
for line in self._data['corpus']:
|
134 |
+
yield line['_id'], line
|
135 |
+
elif self.config.name == 'queries':
|
136 |
+
for query, qid in queries.items():
|
137 |
+
yield qid, {
|
138 |
+
"_id": qid,
|
139 |
+
"title": '',
|
140 |
+
"text": query,
|
141 |
+
}
|
142 |
+
elif self.config.name == 'qrels':
|
143 |
+
for qid, dids in qrels.items():
|
144 |
+
yield qid, {
|
145 |
+
"_id": qid,
|
146 |
+
"title": '',
|
147 |
+
"text": ' '.join(dids),
|
148 |
+
}
|
149 |
+
else:
|
150 |
+
raise ValueError(f'Unknown config name: {self.config.name}')
|