File size: 9,352 Bytes
7dab535
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ae9cd16
7dab535
 
 
 
 
 
 
 
 
 
 
 
 
ae9cd16
2f58404
7dab535
 
 
 
 
 
 
 
 
 
 
 
 
bacaffb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2f58404
7dab535
 
 
 
 
 
 
 
bacaffb
 
 
 
 
 
 
 
 
 
 
 
 
138c005
bacaffb
2f58404
7dab535
 
 
bacaffb
7dab535
 
 
 
b8d9887
7dab535
 
 
 
 
b8d9887
9fa7b76
7dab535
9fa7b76
 
 
 
 
7dab535
9fa7b76
 
7dab535
9fa7b76
 
 
 
 
7dab535
9fa7b76
7dab535
 
 
 
1ebe910
7dab535
 
 
 
 
 
 
 
ab2edcb
bacaffb
3a1078c
bacaffb
7dab535
 
 
 
4b0019e
7dab535
 
 
 
 
 
 
 
 
1ebe910
7dab535
 
 
 
 
 
1ebe910
7dab535
 
 
 
 
 
1ebe910
7dab535
 
 
 
 
 
bacaffb
7dab535
 
 
302ddb6
7dab535
816d7d8
 
302ddb6
 
 
 
 
 
 
 
 
 
 
 
1ebe910
302ddb6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# coding=utf-8
# Copyright 2022 CodeQueries Authors and the HuggingFace Datasets 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.

"""The CodeQueries benchmark."""


import json
import os

import datasets

logger = datasets.logging.get_logger(__name__)


_CODEQUERIES_CITATION = """\
@article{codequeries2022,
  title={Learning to Answer Semantic Queries over Code},
  author={A, B, C, D, E, F},
  journal={arXiv preprint arXiv:<.>},
  year={2022}
}
"""

_IDEAL_DESCRIPTION = """\
CodeQueries Ideal setup.

"""

_PREFIX_DESCRIPTION = """\
CodeQueries Prefix setup."""

_FILE_IDEAL_DESCRIPTION = """\
CodeQueries File level Ideal setup."""

_TWOSTEP_DESCRIPTION = """\
CodeQueries Twostep setup."""


class CodequeriesConfig(datasets.BuilderConfig):
    """BuilderConfig for Codequeries."""

    def __init__(self, features, citation, data_url, url, **kwargs):
        """BuilderConfig for Codequeries.

        Args:
          features: `list[string]`, list of the features that will appear in the
            feature dict. Should not include "label".
          citation: `string`, citation for the data set.
          **kwargs: keyword arguments forwarded to super.
        """
        # Version history:
        # 1.0.0: Initial version.
        super(CodequeriesConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
        self.features = features
        self.citation = citation
        self.data_url = data_url
        self.url = url


class Codequeries(datasets.GeneratorBasedBuilder):
    """The Codequeries benchmark."""

    BUILDER_CONFIGS = [
        CodequeriesConfig(
            name="ideal",
            description=_IDEAL_DESCRIPTION,
            features=["query_name", "context_blocks", "answer_spans",
                      "supporting_fact_spans", "code_file_path", "example_type",
                      "subtokenized_input_sequence", "label_sequence"],
            citation=_CODEQUERIES_CITATION,
            data_url={
                "train": "ideal_train.json",
                "dev": "ideal_val.json",
                "test": "ideal_test.json"
            },
            url="",
        ),
        CodequeriesConfig(
            name="prefix",
            description=_PREFIX_DESCRIPTION,
            features=["query_name", "answer_spans",
                      "supporting_fact_spans", "code_file_path", "example_type",
                      "subtokenized_input_sequence", "label_sequence"],
            citation=_CODEQUERIES_CITATION,
            data_url={
                "test": "prefix_test.json"
            },
            url="",
        ),
        CodequeriesConfig(
            name="file_ideal",
            description=_FILE_IDEAL_DESCRIPTION,
            features=["query_name", "context_blocks", "answer_spans",
                      "supporting_fact_spans", "code_file_path", "example_type",
                      "subtokenized_input_sequence", "label_sequence"],
            citation=_CODEQUERIES_CITATION,
            data_url={
                "test": "file_ideal_test.json"
            },
            url="",
        ),
        CodequeriesConfig(
            name="twostep",
            description=_TWOSTEP_DESCRIPTION,
            features=["query_name", "context_blocks", "answer_spans",
                      "supporting_fact_spans", "code_file_path", "example_type",
                      "subtokenized_input_sequence", "label_sequence"],
            citation=_CODEQUERIES_CITATION,
            data_url={
                "test": ["twostep_relevance/" + "twostep_relevance_test_" + str(i) + ".json" for i in range(0,10)]
            },
            url="",
        ),
    ]

    DEFAULT_CONFIG_NAME = "ideal"

    def _info(self):
        features = {}
        features["query_name"] = datasets.Value("string")
        features["context_blocks"] = [
            {
                "content": datasets.Value("string"),
                "metadata": datasets.Value("string"),
                "header": datasets.Value("string")
            }
        ]
        features["answer_spans"] = [
            {
                'span': datasets.Value("string"),
                'start_line': datasets.Value("int32"),
                'start_column': datasets.Value("int32"),
                'end_line': datasets.Value("int32"),
                'end_column': datasets.Value("int32")
            }
        ]
        features["supporting_fact_spans"] = [
            {
                'span': datasets.Value("string"),
                'start_line': datasets.Value("int32"),
                'start_column': datasets.Value("int32"),
                'end_line': datasets.Value("int32"),
                'end_column': datasets.Value("int32")
            }
        ]
        features["code_file_path"] = datasets.Value("string")
        features["example_type"] = datasets.Value("int32")
        features["subtokenized_input_sequence"] = datasets.features.Sequence(datasets.Value("string"))
        features["label_sequence"] = datasets.features.Sequence(datasets.Value("int32"))
        features["relevance_label"] = datasets.Value("int32")

        return datasets.DatasetInfo(
            description=self.config.description,
            features=datasets.Features(features),
            homepage=self.config.url,
            citation=_CODEQUERIES_CITATION,
        )

    def _split_generators(self, dl_manager):
        dl_dir = dl_manager.download_and_extract(self.config.data_url)
        print(dl_dir)
        if self.config.name in ["prefix", "file_ideal", "twostep"]:
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    gen_kwargs={
                        "filepath": dl_dir["test"],
                        "split": datasets.Split.TEST,
                    },
                ),
            ]
        else:
            return [
                datasets.SplitGenerator(
                    name=datasets.Split.TRAIN,
                    gen_kwargs={
                        "filepath": dl_dir["train"],
                        "split": datasets.Split.TRAIN,
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.VALIDATION,
                    gen_kwargs={
                        "filepath": dl_dir["dev"],
                        "split": datasets.Split.VALIDATION,
                    },
                ),
                datasets.SplitGenerator(
                    name=datasets.Split.TEST,
                    gen_kwargs={
                        "filepath": dl_dir["test"],
                        "split": datasets.Split.TEST,
                    },
                ),
            ]

    def _generate_examples(self, filepath, split):
        if self.config.name in ["prefix", "file_ideal", "twostep"]:
            assert split == datasets.Split.TEST
        logger.info("generating examples from = %s", filepath)

        if self.config.name == "twostep":
            key = 0
            for fp in filepath:
                with open(fp, encoding="utf-8") as f:
                    for line in f:
                        row = json.loads(line)

                        instance_key = str(key) + "_" + row["query_name"] + "_" + row["code_file_path"]
                        yield instance_key, {
                            "query_name": row["query_name"],
                            "context_blocks": row["context_blocks"],
                            "answer_spans": row["answer_spans"],
                            "supporting_fact_spans": row["supporting_fact_spans"],
                            "code_file_path": row["code_file_path"],
                            "example_type": row["example_type"],
                            "subtokenized_input_sequence": row["subtokenized_input_sequence"],
                            "relevance_label": row["relevance_label"],
                        }
                        key += 1
        else:
            with open(filepath, encoding="utf-8") as f:
                key = 0
                for line in f:
                    row = json.loads(line)

                    instance_key = str(key) + "_" + row["query_name"] + "_" + row["code_file_path"]
                    yield instance_key, {
                        "query_name": row["query_name"],
                        "context_blocks": row["context_blocks"],
                        "answer_spans": row["answer_spans"],
                        "supporting_fact_spans": row["supporting_fact_spans"],
                        "code_file_path": row["code_file_path"],
                        "example_type": row["example_type"],
                        "subtokenized_input_sequence": row["subtokenized_input_sequence"],
                        "label_sequence": row["label_sequence"],
                    }
                    key += 1