File size: 8,821 Bytes
e44b64b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5b6c9ca
 
 
e44b64b
 
 
 
 
 
 
 
 
 
 
5b6c9ca
 
 
 
 
 
 
 
 
 
 
 
 
e44b64b
 
 
 
 
 
5b6c9ca
 
 
e44b64b
 
 
 
 
 
5b6c9ca
 
 
 
 
 
 
 
 
 
 
 
 
e44b64b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
from enum import Enum
from typing import List

import datasets
import pandas as pd

from datasets import Features, Value, Array2D, Sequence, SplitGenerator, Split


_CITATION = """\
@InProceedings{huggingface:dataset,
    title = {philipphager/baidu-ultr_baidu-mlm-ctr},
    author={Philipp Hager, Romain Deffayet},
    year={2023}
}
"""

_DESCRIPTION = """\
Query-document vectors and clicks for a subset of the Baidu Unbiased Learning to Rank
dataset: https://arxiv.org/abs/2207.03051

This dataset uses the BERT cross-encoder with 12 layers from Baidu released
in the official starter-kit to compute query-document vectors (768 dims):
https://github.com/ChuXiaokai/baidu_ultr_dataset/

We link the model checkpoint also under `model/`. 
"""

_HOMEPAGE = "https://huggingface.co/datasets/philipphager/baidu-ultr_baidu-mlm-ctr/"
_LICENSE = "cc-by-nc-4.0"
_VERSION = "0.1.0"


class Config(str, Enum):
    ANNOTATIONS = "annotations"
    CLICKS = "clicks"


class BaiduUltrBuilder(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version(_VERSION)
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name=Config.CLICKS,
            version=VERSION,
            description="Load train/val/test clicks from the Baidu ULTR dataset",
        ),
        datasets.BuilderConfig(
            name=Config.ANNOTATIONS,
            version=VERSION,
            description="Load expert annotations from the Baidu ULTR dataset",
        ),
    ]

    CLICK_FEATURES = Features(
        {
            "query_id": Value("string"),
            "query": Array2D((None, 128), "int32"),
            "title": Array2D((None, 128), "int32"),
            "abstract": Array2D((None, 128), "int32"),
            "query_md5": Value("string"),
            "url_md5": Sequence(Value("string")),
            "text_md5": Sequence(Value("string")),
            "query_document_embedding": Array2D((None, 768), "float16"),
            "click": Sequence(Value("int32")),
            "n": Value("int32"),
            "position": Sequence(Value("int32")),
            "media_type": Sequence(Value("int32")),
            "displayed_time": Sequence(Value("float32")),
            "serp_height": Sequence(Value("int32")),
            "slipoff_count_after_click": Sequence(Value("int32")),
            "bm25": Sequence(Value("float32")),
            "title_bm25": Sequence(Value("float32")),
            "abstract_bm25": Sequence(Value("float32")),
            "tf_idf": Sequence(Value("float32")),
            "tf": Sequence(Value("float32")),
            "idf": Sequence(Value("float32")),
            "ql_jelinek_mercer_short": Sequence(Value("float32")),
            "ql_jelinek_mercer_long": Sequence(Value("float32")),
            "ql_dirichlet": Sequence(Value("float32")),
            "query_length": Sequence(Value("int32")),
            "document_length": Sequence(Value("int32")),
            "title_length": Sequence(Value("int32")),
            "abstract_length": Sequence(Value("int32")),
        }
    )

    ANNOTATION_FEATURES = Features(
        {
            "query_id": Value("string"),
            "query": Array2D((None, 128), "int32"),
            "title": Array2D((None, 128), "int32"),
            "abstract": Array2D((None, 128), "int32"),
            "query_md5": Value("string"),
            "text_md5": Value("string"),
            "query_document_embedding": Array2D((None, 768), "float16"),
            "label": Sequence(Value("int32")),
            "n": Value("int32"),
            "frequency_bucket": Value("int32"),
            "bm25": Sequence(Value("float32")),
            "title_bm25": Sequence(Value("float32")),
            "abstract_bm25": Sequence(Value("float32")),
            "tf_idf": Sequence(Value("float32")),
            "tf": Sequence(Value("float32")),
            "idf": Sequence(Value("float32")),
            "ql_jelinek_mercer_short": Sequence(Value("float32")),
            "ql_jelinek_mercer_long": Sequence(Value("float32")),
            "ql_dirichlet": Sequence(Value("float32")),
            "query_length": Sequence(Value("int32")),
            "document_length": Sequence(Value("int32")),
            "title_length": Sequence(Value("int32")),
            "abstract_length": Sequence(Value("int32")),
        }
    )

    DEFAULT_CONFIG_NAME = Config.CLICKS

    def _info(self):
        if self.config.name == Config.CLICKS:
            features = self.CLICK_FEATURES
        elif self.config.name == Config.ANNOTATIONS:
            features = self.ANNOTATION_FEATURES
        else:
            raise ValueError(
                f"Config {self.config.name} must be in ['clicks', 'annotations']"
            )

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        if self.config.name == Config.CLICKS:
            train_files = self.download_clicks(dl_manager, parts=[1, 2, 3])
            test_files = self.download_clicks(dl_manager, parts=[0])

            query_columns = [
                "query_id",
                "query_md5",
            ]

            agg_columns = [
                "query_md5",
                "url_md5",
                "text_md5",
                "position",
                "click",
                "query_document_embedding",
                "media_type",
                "displayed_time",
                "serp_height",
                "slipoff_count_after_click",
            ]

            return [
                SplitGenerator(
                    name=Split.TRAIN,
                    gen_kwargs={
                        "files": train_files,
                        "query_columns": query_columns,
                        "agg_columns": agg_columns,
                    },
                ),
                SplitGenerator(
                    name=Split.TEST,
                    gen_kwargs={
                        "files": test_files,
                        "query_columns": query_columns,
                        "agg_columns": agg_columns,
                    },
                ),
            ]
        elif self.config.name == Config.ANNOTATIONS:
            test_files = dl_manager.download(["parts/validation.feather"])
            query_columns = [
                "query_id",
                "query_md5",
                "frequency_bucket",
            ]
            agg_columns = [
                "text_md5",
                "label",
                "query_document_embedding",
            ]

            return [
                SplitGenerator(
                    name=Split.TEST,
                    gen_kwargs={
                        "files": test_files,
                        "query_columns": query_columns,
                        "agg_columns": agg_columns,
                    },
                )
            ]
        else:
            raise ValueError("Config name must be in ['clicks', 'annotations']")

    def download_clicks(self, dl_manager, parts: List[int], splits_per_part: int = 10):
        urls = [
            f"parts/part-{p}_split-{s}.feather"
            for p in parts
            for s in range(splits_per_part)
        ]

        return dl_manager.download(urls)

    def _generate_examples(
        self,
        files: List[str],
        query_columns: List[str],
        agg_columns: List[str],
    ):
        """
        Reads dataset partitions and aggregates document features per query.
        :param files: List of .feather files to load from disk.
        :param query_columns: Columns with one value per query. E.g., query_id,
        frequency bucket, etc.
        :param agg_columns: Columns with one value per document that should be
        aggregated per query. E.g., click, position, query_document_embeddings, etc.
        :return:
        """
        for file in files:
            df = pd.read_feather(file)
            current_query_id = None
            sample_key = None
            sample = None

            for i in range(len(df)):
                row = df.iloc[i]

                if current_query_id != row["query_id"]:
                    if current_query_id is not None:
                        yield sample_key, sample

                    current_query_id = row["query_id"]
                    sample_key = f"{file}-{current_query_id}"
                    sample = {"n": 0}

                    for column in query_columns:
                        sample[column] = row[column]
                    for column in agg_columns:
                        sample[column] = []

                for column in agg_columns:
                    sample[column].append(row[column])

                sample["n"] += 1

            yield sample_key, sample