Datasets:

ArXiv:
License:
File size: 9,608 Bytes
4268477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
475548c
 
 
 
 
4268477
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# Copyright 2020 The HuggingFace Datasets Authors and Serivce Now Authors.
#
# 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.
"""This is a loading script for the Stack-Repo dataset"""


import csv
import json
import os

from pathlib import Path

import datasets


_CITATION = """\
@article{shrivastava2023repofusion,
  title={RepoFusion: Training Code Models to Understand Your Repository},
  author={Shrivastava, Disha and Kocetkov, Denis and de Vries, Harm and Bahdanau, Dzmitry and Scholak, Torsten},
  journal={arXiv preprint arXiv:2306.10998},
  year={2023}
}
"""

_DESCRIPTION = """\
This is the Stack-Repo dataset
"""

_HOMEPAGE = "https://huggingface.co/datasets/RepoFusion/Stack-Repo"

_LICENSE = "other"

_SOURCES = {
    'test': [
        'thekoc', 'JellyBrother', 'shuli495', 'xiao-ren-wu', 'UzcDevelopmentTeam', 'Penguinz22', 'andrewvmail',
        'richard-trotter', 'SlimPay', 'teknux-org', 'FIXTradingCommunity', 'Juliana-Felipe', 'exasol', 'iBiber',
        'sebaslogen', 'billmcchesney1', 'ms-showcase', 'mP1', 'yokotaso', 'ErikHage', 'shadaileng', 'regular-project',
        'wiwilin', '1273091433', 'liangxiong3403', 'wangzlei', 'Vitaliy-Yakovchuk', 'anshul-mohil', 'MarceloCavassani',
        'cwhelan', 'score-server', 'meyer5', 'SeSac-Cloud-Backend-4', 'Hugo-Gao', 'Olivia0202', 'IceIce1ce', '4pxzhou',
        'bboylin', 'acolebourne', '1725762388', 'iViolinSolo', 'diaolingzc', 'trungtuan', 'havenmoney', 'speak2me',
        'VincenzoArceri', 'exadel-inc', 'JohnReeson', 'CyiceK', 'Vooy'
    ], 
    'train': [
        'ValeriyKnyazhev', 'leshiv', 'MfromAzeroth', 'wugjin', 'MarbleDice', 'hivijayreddy', 'sidpatel-github',
        'hellorizon', 'CreativePenguin', 'maulikpandey', 'a630140621', 'luminiusa', 'ot-maksim', 'akash-coded',
        'eddiewgj', 'a616707902', 'oasis9', 'DeyanZhelyazkov', 'yweng47', 'JCThePants', 'Gorlem', 'dongjihui666',
        'mediatrue', 'david2999999', 'comme-il-faut', 'jannal', 'tfjybj', 'DimkaGorhover', 'ZhangTizo', 'CIDARLAB',
        'DwArFeng', 'mbarkley', 'VladRomanchuk', 'vivekmalhotragithub', 'android-little-boy', 'patrik999', 'madanalogy',
        'TheTrueColonel', 'TreeZhiyuan', 'sistcoop', 'java-ea', 'DarwinReforged', 'DougEdey', '402d', 'wangdingfeng',
        'WeiziPlus', 'fabionery', 'famartinrh', 'rghvgrv', 'QuincySx', 'raphaelcohn', 'hYdos', 'tacticalrce',
        'danielkrajnik', 'SeanDecker1', 'bahaahussein', 'Team1772', 'zengfanmao', 'Hopp-Stu', 'My-DIGI-ID', 'slickqa',
        'opentok', 'Manolomon', 'denhatmong', 'baishuo', 'zli78122', 'kenichi-ando', 'pengcash', 'Princelo',
        'RuthDirnfeld', 'HRI-EU', 'jan438', 'sd2438892', 'rsofista', 'mpsitech', 'AvaN0x', 'JDode', 'dreadwail',
        'pcjs156', 'MrWe', 'smallxiongxiong', 'wuxinlingluan', 'FLxmw', 'TransFICC', 'fengpod', 'orient33', 'hamsterxc',
        'diegolemospadilha', 'CheRut', 'ToreAad', 'AndrewHYC', 'Azagwen', 'Ndivhuwo', 'gou02rav32', 'CDZR0',
        'MFunction96', 'google', 'mariodavid', 'jacksontenorio8', 'DieguinhoHR'
    ],
    'val': [
        'malcolmgreaves', 'atlasapi', 'HeXavi8', 'fank243', 'StealWonders', 'asyakasimova', 'truthiswill',
        'swiatecki', 'yanjingfan', 'vamnoize', 'Eugenemdk', 'explodingnuggets', 'MartinItsLinda',
        'Web-Programmer-B-P', 'SergeyZhernovoy', 'sunhaidong', 'marcogrella', 'C0DEC0L0NY', 'JustinZuidgeest',
        'ShareWiFi', 'ConusDaktis', 'lqhshishen', 'vaginessa', 'Juansero29', 'meghbhalerao', 'YunLemon',
        'MeiVinEight', 'Huawei-DTSE-India-Delhi-NCR', 'ym-monkey', 'Selim042', 'ValentinGiboulot', 'jesusbmx',
        'FantasybabyChange', 'zegelin', 'MeasureAuthoringTool', 'death-knight', 'abargnesi', 'cmunilla',
        'sladyn98', 'jhaprabhatt', 'smartsv-dev', 'Lavaong', 'ShahabGT', 'WXB2010', 'digideskio', 'p1r9t3',
        'chathamabate', 'heymysweetheart', 'blockchain-etl', 'Jeniel557'
    ]
}

class StackRepo(datasets.GeneratorBasedBuilder):
    """This is a builder class for the Stack-Repo dataset"""

    VERSION = datasets.Version("1.0.0")

    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="bm25_contexts",
            version=VERSION,
            description="This part of the dataset covers bm25 contexts"
        ),
        datasets.BuilderConfig(
            name="PP_contexts",
            version=VERSION,
            description="This part of the dataset covers PP contexts"
        ),
        datasets.BuilderConfig(
            name="randomNN_contexts",
            version=VERSION,
            description="This part of the dataset covers randomNN contexts"
        ),
        datasets.BuilderConfig(
            name="sources",
            version=VERSION,
            description="This part of the dataset covers java file sources used in building other configurations"
        ),
    ]

    def _info(self):
        if self.config.name == 'sources':
            features = datasets.Features({
                'file': datasets.Value('string'),
                'content': datasets.Value('string')
            })
        else:
            features = datasets.Features({
                'id': datasets.Value('string'),
                'hole_file': datasets.Value('string'),
                'hole_line': datasets.Value('int32'),
                'hole_pos': datasets.Value('int32'),
                'question': datasets.Value('string'),
                'target': datasets.Value('string'),
                'answers': datasets.Sequence(
                    datasets.Value('string')
                ),
                'ctxs': [{
                    'title': datasets.Value('string'),
                    'text': datasets.Value('string'),
                    'score': datasets.Value('float64')
                }]
        })
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=features,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    @property
    def manual_download_instructions(self):
        # We are ok if user specifies local path instead of dataset name and data_dir
        # for sources configuration and other configurations can be downloaded from the hub
        # automatically. So, we manipulate return value here to get around manual_dir is None
        # check in _check_manual_download
        # NOTE: this hack can broke if implementaion of the _check_manual_download will change
        # TODO: override _check_manual_download instead
        if self.config.name == 'sources':
            if not self.dl_manager.manual_dir is None:
                base_path = Path(self.dl_manager.manual_dir)
            else:
                base_path = Path(self.base_path)

            if base_path.expanduser().absolute().is_dir():
                return None

            return (
                "first download the dataset to a local folder with "
                "git clone https://huggingface.co/datasets/RepoFusion/Stack-Repo "
                "<path/to/manual/data>"
            )
        else:
            return None
              
    
    def _split_generators(self, dl_manager):
        splits = [
            (datasets.Split.TRAIN, 'train'),
            (datasets.Split.TEST, 'test'),
            (datasets.Split.VALIDATION, 'val')
        ]
        
        if not dl_manager.manual_dir is None:
            base_path = Path(dl_manager.manual_dir)
        else:
            base_path = Path(self.base_path)

        if self.config.name == 'sources':
            src = {
                split[0]: [
                    el.relative_to(base_path)
                    for el in (base_path / f'data/{split[1]}/').rglob('*.java')
                ]
                for split in splits
            }
        else:
            src = {
                split[0]: [
                    f'data/{split[1]}/{el}/hole_and_{self.config.name}.json'
                    for el in _SOURCES[split[1]]
                ]
                for split in splits
            }

        data_dir = dl_manager.download_and_extract(src)
        return [
            datasets.SplitGenerator(
                name=split[0],
                gen_kwargs={
                    "filepaths": data_dir[split[0]],
                    "base_path": base_path
                },
            )
            for split in splits
        ]

    def _generate_examples(self, filepaths, base_path):
        for filepath in filepaths:
            if self.config.name == 'sources':
                filepath = Path(filepath)
                file = str(filepath.relative_to(base_path))
                yield file, {
                    'file': file,
                    'content': filepath.read_text()
                }
            else:
                with open(filepath, encoding="utf-8") as f:
                    for row in f:
                        data = json.loads(row)
                        parts = data['id'].rsplit('_', 2)
                        data['hole_file'] = str(base_path / parts[0])
                        data['hole_line'] = int(parts[1])
                        data['hole_pos'] = int(parts[2])
                        yield data['id'], data