File size: 8,495 Bytes
717eb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26f3a83
717eb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
feba2d4
717eb0c
feba2d4
717eb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8
# 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.

# Lint as: python3
"""CodeSearchNet corpus: proxy dataset for semantic code search"""

# TODO: add licensing info in the examples
# TODO: log richer informations (especially while extracting the jsonl.gz files)
# TODO: enable custom configs; such as: "java+python"
# TODO: enable fetching examples with a given license, eg: "java_MIT"


import json
import os

import datasets


_CITATION = """\
@article{husain2019codesearchnet,
    title={{CodeSearchNet} challenge: Evaluating the state of semantic code search},
    author={Husain, Hamel and Wu, Ho-Hsiang and Gazit, Tiferet and Allamanis, Miltiadis and Brockschmidt, Marc},
    journal={arXiv preprint arXiv:1909.09436},
    year={2019}
}
"""

_DESCRIPTION = """\
CodeSearchNet corpus contains about 6 million functions from open-source code \
spanning six programming languages (Go, Java, JavaScript, PHP, Python, and Ruby). \
The CodeSearchNet Corpus also contains automatically generated query-like \
natural language for 2 million functions, obtained from mechanically scraping \
and preprocessing associated function documentation.
"""

_HOMEPAGE = "https://github.com/github/CodeSearchNet"

_LICENSE = "Various"

_S3_BUCKET_URL = "https://s3.amazonaws.com/code-search-net/CodeSearchNet/v2/"
_AVAILABLE_LANGUAGES = ["python", "java", "javascript", "go", "ruby", "php"]
_URLs = {language: _S3_BUCKET_URL + f"{language}.zip" for language in _AVAILABLE_LANGUAGES}
# URLs for "all" are just the concatenation of URLs for all languages
_URLs["all"] = _URLs.copy()


class CodeSearchNet(datasets.GeneratorBasedBuilder):
    """ "CodeSearchNet corpus: proxy dataset for semantic code search."""

    VERSION = datasets.Version("1.0.0", "Add CodeSearchNet corpus dataset")
    BUILDER_CONFIGS = [
        datasets.BuilderConfig(
            name="all",
            version=VERSION,
            description="All available languages: Java, Go, Javascript, Python, PHP, Ruby",
        ),
        datasets.BuilderConfig(
            name="java",
            version=VERSION,
            description="Java language",
        ),
        datasets.BuilderConfig(
            name="go",
            version=VERSION,
            description="Go language",
        ),
        datasets.BuilderConfig(
            name="python",
            version=VERSION,
            description="Pyhton language",
        ),
        datasets.BuilderConfig(
            name="javascript",
            version=VERSION,
            description="Javascript language",
        ),
        datasets.BuilderConfig(
            name="ruby",
            version=VERSION,
            description="Ruby language",
        ),
        datasets.BuilderConfig(
            name="php",
            version=VERSION,
            description="PHP language",
        ),
    ]

    DEFAULT_CONFIG_NAME = "all"

    def _info(self):
        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(
                {
                    "repository_name": datasets.Value("string"),
                    "func_path_in_repository": datasets.Value("string"),
                    "func_name": datasets.Value("string"),
                    "whole_func_string": datasets.Value("string"),
                    "language": datasets.Value("string"),
                    "func_code_string": datasets.Value("string"),
                    "func_code_tokens": datasets.Sequence(datasets.Value("string")),
                    "func_documentation_string": datasets.Value("string"),
                    "func_documentation_tokens": datasets.Sequence(datasets.Value("string")),
                    "split_name": datasets.Value("string"),
                    "func_code_url": datasets.Value("string"),
                    # TODO - add licensing info in the examples
                }
            ),
            # No default supervised keys
            supervised_keys=None,
            homepage=_HOMEPAGE,
            license=_LICENSE,
            citation=_CITATION,
        )

    def _split_generators(self, dl_manager):
        """Returns SplitGenerators.

        Note: The original data is stored in S3, and follows this unusual directory structure:
            ```
            .
            β”œβ”€β”€ <language_name>  # e.g. python
            β”‚Β Β  └── final
            β”‚Β Β      └── jsonl
            β”‚Β Β          β”œβ”€β”€ test
            β”‚Β Β          β”‚Β Β  └── <language_name>_test_0.jsonl.gz
            β”‚Β Β          β”œβ”€β”€ train
            β”‚Β Β          β”‚Β Β  β”œβ”€β”€ <language_name>_train_0.jsonl.gz
            β”‚Β Β          β”‚Β Β  β”œβ”€β”€ <language_name>_train_1.jsonl.gz
            β”‚Β Β          β”‚Β Β  β”œβ”€β”€ ...
            β”‚Β Β          β”‚Β Β  └── <language_name>_train_n.jsonl.gz
            β”‚Β Β          └── valid
            β”‚Β Β              └── <language_name>_valid_0.jsonl.gz
            β”œβ”€β”€ <language_name>_dedupe_definitions_v2.pkl
            └── <language_name>_licenses.pkl
            ```
        """
        data_urls = _URLs[self.config.name]
        if isinstance(data_urls, str):
            data_urls = {self.config.name: data_urls}
        # Download & extract the language archives
        data_dirs = [
            os.path.join(directory, lang, "final", "jsonl")
            for lang, directory in dl_manager.download_and_extract(data_urls).items()
        ]

        split2dirs = {
            split_name: [os.path.join(directory, split_name) for directory in data_dirs]
            for split_name in ["train", "test", "valid"]
        }

        split2paths = dl_manager.extract(
            {
                split_name: [
                    os.path.join(directory, entry_name)
                    for directory in split_dirs
                    for entry_name in os.listdir(directory)
                ]
                for split_name, split_dirs in split2dirs.items()
            }
        )

        return [
            datasets.SplitGenerator(
                name=datasets.Split.TRAIN,
                gen_kwargs={
                    "filepaths": split2paths["train"],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.TEST,
                gen_kwargs={
                    "filepaths": split2paths["test"],
                },
            ),
            datasets.SplitGenerator(
                name=datasets.Split.VALIDATION,
                gen_kwargs={
                    "filepaths": split2paths["valid"],
                },
            ),
        ]

    def _generate_examples(self, filepaths):
        """Yields the examples by iterating through the available jsonl files."""
        for file_id_, filepath in enumerate(filepaths):
            with open(filepath, encoding="utf-8") as f:
                for row_id_, row in enumerate(f):
                    # Key of the example = file_id + row_id,
                    # to ensure all examples have a distinct key
                    id_ = f"{file_id_}_{row_id_}"
                    data = json.loads(row)
                    yield id_, {
                        "repository_name": data["repo"],
                        "func_path_in_repository": data["path"],
                        "func_name": data["func_name"],
                        "whole_func_string": data["original_string"],
                        "language": data["language"],
                        "func_code_string": data["code"],
                        "func_code_tokens": data["code_tokens"],
                        "func_documentation_string": data["docstring"],
                        "func_documentation_tokens": data["docstring_tokens"],
                        "split_name": data["partition"],
                        "func_code_url": data["url"],
                    }