Datasets:

Modalities:
Tabular
Text
Formats:
csv
Libraries:
Datasets
pandas
License:
JingyuanHe1222 commited on
Commit
1c02e1f
·
1 Parent(s): cb2352c

custom data loading

Browse files
Files changed (2) hide show
  1. README.md +22 -4
  2. clueweb_reco.py +219 -0
README.md CHANGED
@@ -1,5 +1,26 @@
1
  ---
2
  configs:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3
  - config_name: ordered_id_splits
4
  data_files:
5
  - split: train
@@ -18,10 +39,7 @@ configs:
18
  - split: test
19
  path: "interaction_splits/test_input.tsv"
20
  - config_name: clueweb_id_mapping
21
- data_files: "cwid_to_id.tsv"
22
- license: mit
23
- ---
24
-
25
 
26
 
27
  # ClueWeb-Reco:
 
1
  ---
2
  configs:
3
+ - config_name: input
4
+ data_files:
5
+ - split: valid
6
+ path:
7
+ - "interaction_splits/valid_inter_input.tsv"
8
+ - split: test
9
+ path:
10
+ - "interaction_splits/test_inter_input.tsv"
11
+ default: true
12
+ - config_name: target
13
+ data_files:
14
+ - split: valid
15
+ path:
16
+ - "interaction_splits/valid_inter_target.csv"
17
+ - config_name: mapping
18
+ data_files: "cwid_to_id.tsv"
19
+ license: mit
20
+ ---
21
+
22
+
23
+ <!-- configs:
24
  - config_name: ordered_id_splits
25
  data_files:
26
  - split: train
 
39
  - split: test
40
  path: "interaction_splits/test_input.tsv"
41
  - config_name: clueweb_id_mapping
42
+ data_files: "cwid_to_id.tsv" -->
 
 
 
43
 
44
 
45
  # ClueWeb-Reco:
clueweb_reco.py ADDED
@@ -0,0 +1,219 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+ # TODO: Address all TODOs and remove all explanatory comments
15
+
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+ from decimal import Decimal
22
+
23
+ import datasets
24
+
25
+ # TODO: citation
26
+ # # Find for instance the citation on arxiv or on the dataset repo/website
27
+ # _CITATION = """\
28
+ # @InProceedings{huggingface:dataset,
29
+ # title = {A great new dataset},
30
+ # author={huggingface, Inc.
31
+ # },
32
+ # year={2020}
33
+ # }
34
+ # """
35
+ _CITATION = ""
36
+
37
+ #
38
+ _DESCRIPTION = """\
39
+ ClueWeb-Reco is a novel zero-shot test set derived from real, \
40
+ consented user browsing sequences,
41
+ aligning with modern recommendation scenarios while ensuring privacy.
42
+ """
43
+
44
+ _HOMEPAGE = "https://huggingface.co/datasets/cx-cmu/ClueWeb-Reco"
45
+
46
+ _LICENSE = "mit"
47
+
48
+ # The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
49
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
50
+ _URLS = {
51
+ "input": "https://huggingface.co/datasets/cx-cmu/ClueWeb-Reco/tree/main/interaction_splits",
52
+ "target": "https://huggingface.co/datasets/cx-cmu/ClueWeb-Reco/tree/main/interaction_splits",
53
+ "mapping": "https://huggingface.co/datasets/cx-cmu/ClueWeb-Reco/tree/main",
54
+ }
55
+
56
+
57
+ class ClueWebRecoDataset(datasets.GeneratorBasedBuilder):
58
+ """Process the ClueWeb-Reco zero-shot dataset"""
59
+
60
+ VERSION = datasets.Version("1.1.0")
61
+
62
+ # This is an example of a dataset with multiple configurations.
63
+ # If you don't want/need to define several sub-sets in your dataset,
64
+ # just remove the BUILDER_CONFIG_CLASS and the BUILDER_CONFIGS attributes.
65
+
66
+ # If you need to make complex sub-parts in the datasets with configurable options
67
+ # You can create your own builder configuration class to store attribute, inheriting from datasets.BuilderConfig
68
+ # BUILDER_CONFIG_CLASS = MyBuilderConfig
69
+
70
+ # You will be able to load one or the other configurations in the following list with
71
+ # data = datasets.load_dataset('my_dataset', 'first_domain')
72
+ # data = datasets.load_dataset('my_dataset', 'second_domain')
73
+ BUILDER_CONFIGS = [
74
+ datasets.BuilderConfig(name="input", version=VERSION, description="This is the input parts of the dataset"),
75
+ datasets.BuilderConfig(name="target", version=VERSION, description="This is the target parts of the dataset"),
76
+ datasets.BuilderConfig(name="mapping", version=VERSION, description="This is the mapping between official ClueWeb ids and our internal ClueWeb ids"),
77
+ ]
78
+
79
+ DEFAULT_CONFIG_NAME = "input"
80
+
81
+ def _info(self):
82
+ if self.config.name == "input": # This is the name of the configuration selected in BUILDER_CONFIGS above
83
+ features = datasets.Features(
84
+ {
85
+ "session_id": datasets.Value("string"),
86
+ "cw_internal_id": datasets.Value("int32"),
87
+ "timestamp": datasets.Value("string")
88
+ }
89
+ )
90
+ elif self.config.name == "target":
91
+ features = datasets.Features(
92
+ {
93
+ "session_id": datasets.Value("string"),
94
+ "target_cw_internal_id": datasets.Value("int32"),
95
+ "timestamp": datasets.Value("string")
96
+ }
97
+ )
98
+ elif self.config.name == "mapping":
99
+ features = datasets.Features(
100
+ {
101
+ "cwid": datasets.Value("string"),
102
+ "cw_internal_id": datasets.Value("int32"),
103
+ }
104
+ )
105
+ return datasets.DatasetInfo(
106
+ # This is the description that will appear on the datasets page.
107
+ description=_DESCRIPTION,
108
+ # This defines the different columns of the dataset and their types
109
+ features=features, # Here we define them above because they are different between the two configurations
110
+ # If there's a common (input, target) tuple from the features, uncomment supervised_keys line below and
111
+ # specify them. They'll be used if as_supervised=True in builder.as_dataset.
112
+ # supervised_keys=("sentence", "label"),
113
+ # Homepage of the dataset for documentation
114
+ homepage=_HOMEPAGE,
115
+ # License for the dataset if available
116
+ license=_LICENSE,
117
+ # Citation for the dataset
118
+ citation=_CITATION,
119
+ )
120
+
121
+ def _split_generators(self, dl_manager):
122
+ # TODO: This method is tasked with downloading/extracting the data and defining the splits depending on the configuration
123
+ # If several configurations are possible (listed in BUILDER_CONFIGS), the configuration selected by the user is in self.config.name
124
+
125
+ # dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
126
+ # It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
127
+ # By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
128
+ urls = _URLS[self.config.name]
129
+ data_dir = dl_manager.download_and_extract(urls)
130
+ data_dir = self.config.data_dir
131
+
132
+ if self.config.name == "input":
133
+ return [
134
+ datasets.SplitGenerator(
135
+ name=datasets.Split.VALIDATION,
136
+ gen_kwargs={
137
+ "input_path": os.path.join(data_dir, "interaction_splits/valid_inter_input.tsv"),
138
+ }
139
+ ),
140
+ datasets.SplitGenerator(
141
+ name=datasets.Split.TEST,
142
+ gen_kwargs={
143
+ "input_path": os.path.join(data_dir, "interaction_splits/test_inter_input.tsv"),
144
+ },
145
+ ),
146
+ ]
147
+ elif self.config.name == "target":
148
+ return [
149
+ datasets.SplitGenerator(
150
+ name=datasets.Split.VALIDATION,
151
+ gen_kwargs={
152
+ "target_path": os.path.join(data_dir, "interaction_splits/valid_inter_target.tsv"),
153
+ },
154
+ ),
155
+ ]
156
+
157
+ elif self.config.name == "mapping":
158
+ return [
159
+ datasets.SplitGenerator(
160
+ name=datasets.Split.TRAIN,
161
+ gen_kwargs={
162
+ "mapping_path": os.path.join(data_dir, "cwid_to_id.tsv"),
163
+ },
164
+ ),
165
+ ]
166
+
167
+
168
+ def _generate_examples(self, input_path=None, target_path=None, mapping_path=None):
169
+ """
170
+ Generates examples based on the input and (optionally) target files.
171
+ If the configuration is `input`, `target`, or `mapping`, this handles each separately.
172
+ """
173
+
174
+ if self.config.name == "input":
175
+ if input_path is None:
176
+ raise ValueError("Input configuration requires an input_path.")
177
+
178
+ # Process the `input` configuration
179
+ with open(input_path, encoding="utf-8") as f:
180
+ input_lines = f.readlines()[1:]
181
+ for idx, line in enumerate(input_lines):
182
+ session_id, cw_internal_id, timestamp = line.strip().split("\t")
183
+ yield idx, {
184
+ "session_id": session_id.strip(),
185
+ "cw_internal_id": int(cw_internal_id.strip()),
186
+ "timestamp": str(Decimal(timestamp)),
187
+ }
188
+
189
+ elif self.config.name == "target":
190
+ # if target_path is None:
191
+ # # Test target is hidden; yield nothing
192
+ # return
193
+
194
+ if target_path is None:
195
+ raise ValueError("Target configuration requires an target_path.")
196
+
197
+ with open(target_path, encoding="utf-8") as f:
198
+ target_lines = f.readlines()[1:]
199
+ for idx, line in enumerate(target_lines):
200
+ session_id, target_cw_internal_id, timestamp = line.strip().split("\t")
201
+ yield idx, {
202
+ "session_id": session_id.strip(),
203
+ "target_cw_internal_id": int(target_cw_internal_id.strip()),
204
+ "timestamp": str(Decimal(timestamp)),
205
+ }
206
+
207
+ elif self.config.name == "mapping":
208
+
209
+ if mapping_path is None:
210
+ raise ValueError("Mapping configuration requires an mapping_path.")
211
+
212
+ # Process the `mapping` configuration
213
+ with open(mapping_path, encoding="utf-8") as f:
214
+ for idx, line in enumerate(f):
215
+ cwid, cw_internal_id = line.strip().split("\t")
216
+ yield idx, {
217
+ "cwid": cwid.strip(),
218
+ "cw_internal_id": int(cw_internal_id.strip()),
219
+ }