File size: 3,552 Bytes
3d4e2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03496d5
 
3d4e2ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
03496d5
3d4e2ee
 
 
 
 
03496d5
3d4e2ee
 
 
 
 
03496d5
3d4e2ee
 
03496d5
3d4e2ee
 
 
 
 
 
 
 
 
 
03496d5
3d4e2ee
 
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
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""AI-MO Olympiad Reference Dataset"""



import re
import json
from pathlib import Path

import datasets
from huggingface_hub import HfApi


# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_CITATION = """"""

# TODO: Add description of the dataset here
# You can copy an official description
_DESCRIPTION = """"""

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = ""

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""


class OlympiadReferenceDataset(datasets.GeneratorBasedBuilder):
    VERSION = datasets.Version("0.0.1")

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._hfapi = HfApi()
        self.pattern = re.compile(r'.*/segmented/[^/]+\.jsonl$')

    def _info(self):
        features = datasets.Features(
            {   
                "problem_type": datasets.Value("string"),
                "problem_label": datasets.Value("string"),
                "problem": datasets.Value("string"),
                "solution": datasets.Value("string"),
                "year": datasets.Value("int32"),
                "tier": datasets.Value("int32"),
                "resource_path": datasets.Value("string")
            }
        )

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

    def _split_generators(self, dl_manager):
        data_root_path = Path(dl_manager._base_path)

        repo_files = self._hfapi.list_repo_files(repo_id="AI-MO/olympiads-ref", repo_type="dataset")
        seg_jsonl_files = [s for s in repo_files if self.pattern.match(s)]

        data_files = [(sjf, dl_manager.extract(dl_manager.download(data_root_path / sjf))) for sjf in seg_jsonl_files]

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "data_files": data_files,
                    "split": "train",
                },
            )
        ]

    def _generate_examples(self, data_files, split):
        key = 0

        for resource_path, file in data_files:
            with open(file, "r", encoding="utf-8") as f:
                for line in f:
                    data = json.loads(line)
                    yield key, {
                        "problem_type": data.get("problem_type"),
                        "problem_label": data.get("problem_label") or data.get("label"),
                        "problem": data.get("problem"),
                        "solution": data.get("solution"),
                        "year": data.get("year"),
                        "tier": data.get("tier"),
                        "resource_path": resource_path
                    }
                    key += 1