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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# coding=utf-8
|
| 2 |
+
# Copyright 2022 CodeQueries Authors and the HuggingFace Datasets Authors.
|
| 3 |
+
#
|
| 4 |
+
# Licensed under the Apache License, Version 2.0 (the "License");
|
| 5 |
+
# you may not use this file except in compliance with the License.
|
| 6 |
+
# You may obtain a copy of the License at
|
| 7 |
+
#
|
| 8 |
+
# http://www.apache.org/licenses/LICENSE-2.0
|
| 9 |
+
#
|
| 10 |
+
# Unless required by applicable law or agreed to in writing, software
|
| 11 |
+
# distributed under the License is distributed on an "AS IS" BASIS,
|
| 12 |
+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 13 |
+
# See the License for the specific language governing permissions and
|
| 14 |
+
# limitations under the License.
|
| 15 |
+
|
| 16 |
+
"""The CodeQueries benchmark."""
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
import json
|
| 20 |
+
import os
|
| 21 |
+
|
| 22 |
+
import datasets
|
| 23 |
+
|
| 24 |
+
logger = datasets.logging.get_logger(__name__)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
_CODEQUERIES_CITATION = """\
|
| 28 |
+
@article{codequeries2022,
|
| 29 |
+
title={Learning to Answer Semantic Queries over Code},
|
| 30 |
+
author={A, B, C, D, E, F},
|
| 31 |
+
journal={arXiv preprint arXiv:<.>},
|
| 32 |
+
year={2022}
|
| 33 |
+
}
|
| 34 |
+
"""
|
| 35 |
+
|
| 36 |
+
_IDEAL_DESCRIPTION = """\
|
| 37 |
+
CodeQueries Ideal setup.
|
| 38 |
+
|
| 39 |
+
"""
|
| 40 |
+
|
| 41 |
+
_PREFIX_DESCRIPTION = """\
|
| 42 |
+
CodeQueries Prefix setup."""
|
| 43 |
+
|
| 44 |
+
_SLIDING_WINDOW_DESCRIPTION = """\
|
| 45 |
+
CodeQueries Sliding window setup."""
|
| 46 |
+
|
| 47 |
+
_FILE_IDEAL_DESCRIPTION = """\
|
| 48 |
+
CodeQueries File level Ideal setup."""
|
| 49 |
+
|
| 50 |
+
_TWOSTEP_DESCRIPTION = """\
|
| 51 |
+
CodeQueries Twostep setup."""
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
class CodequeriesConfig(datasets.BuilderConfig):
|
| 55 |
+
"""BuilderConfig for Codequeries."""
|
| 56 |
+
|
| 57 |
+
def __init__(self, features, citation, **kwargs):
|
| 58 |
+
"""BuilderConfig for Codequeries.
|
| 59 |
+
|
| 60 |
+
Args:
|
| 61 |
+
features: `list[string]`, list of the features that will appear in the
|
| 62 |
+
feature dict. Should not include "label".
|
| 63 |
+
citation: `string`, citation for the data set.
|
| 64 |
+
**kwargs: keyword arguments forwarded to super.
|
| 65 |
+
"""
|
| 66 |
+
# Version history:
|
| 67 |
+
# 1.0.0: Initial version.
|
| 68 |
+
super(CodequeriesConfig, self).__init__(version=datasets.Version("1.0.0"), **kwargs)
|
| 69 |
+
self.features = features
|
| 70 |
+
self.citation = citation
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
class Codequeries(datasets.GeneratorBasedBuilder):
|
| 74 |
+
"""The Codequeries benchmark."""
|
| 75 |
+
|
| 76 |
+
BUILDER_CONFIGS = [
|
| 77 |
+
CodequeriesConfig(
|
| 78 |
+
name="ideal",
|
| 79 |
+
description=_IDEAL_DESCRIPTION,
|
| 80 |
+
features=["query_name", "context_blocks", "answer_spans",
|
| 81 |
+
"supporting_fact_spans", "code_file_path", "example_type",
|
| 82 |
+
"subtokenized_input_sequence", "label_sequence"],
|
| 83 |
+
citation=_CODEQUERIES_CITATION,
|
| 84 |
+
# homepage="",
|
| 85 |
+
# data_url="",
|
| 86 |
+
# url="https://github.com/google-research-datasets/boolean-questions",
|
| 87 |
+
),
|
| 88 |
+
# CodequeriesConfig(
|
| 89 |
+
# name="prefix",
|
| 90 |
+
# description=_PREFIX_DESCRIPTION,
|
| 91 |
+
# features=["query_name", "context_blocks", "answer_spans",
|
| 92 |
+
# "supporting_fact_spans", "code_file_path", "example_type",
|
| 93 |
+
# "subtokenized_input_sequence", "label_sequence"],
|
| 94 |
+
# citation=_CODEQUERIES_CITATION,
|
| 95 |
+
# ),
|
| 96 |
+
# CodequeriesConfig(
|
| 97 |
+
# name="sliding_window",
|
| 98 |
+
# description=_SLIDING_WINDOW_DESCRIPTION,
|
| 99 |
+
# features=["query_name", "context_blocks", "answer_spans",
|
| 100 |
+
# "supporting_fact_spans", "code_file_path", "example_type",
|
| 101 |
+
# "subtokenized_input_sequence", "label_sequence"],
|
| 102 |
+
# citation=_CODEQUERIES_CITATION,
|
| 103 |
+
# ),
|
| 104 |
+
CodequeriesConfig(
|
| 105 |
+
name="file_ideal",
|
| 106 |
+
description=_FILE_IDEAL_DESCRIPTION,
|
| 107 |
+
features=["query_name", "context_blocks", "answer_spans",
|
| 108 |
+
"supporting_fact_spans", "code_file_path", "example_type",
|
| 109 |
+
"subtokenized_input_sequence", "label_sequence"],
|
| 110 |
+
citation=_CODEQUERIES_CITATION,
|
| 111 |
+
),
|
| 112 |
+
# CodequeriesConfig(
|
| 113 |
+
# name="twostep",
|
| 114 |
+
# description=_TWOSTEP_DESCRIPTION,
|
| 115 |
+
# features=["query_name", "context_blocks", "answer_spans",
|
| 116 |
+
# "supporting_fact_spans", "code_file_path", "example_type",
|
| 117 |
+
# "subtokenized_input_sequence", "label_sequence"],
|
| 118 |
+
# citation=_CODEQUERIES_CITATION,
|
| 119 |
+
# ),
|
| 120 |
+
]
|
| 121 |
+
|
| 122 |
+
# DEFAULT_CONFIG_NAME = "ideal"
|
| 123 |
+
|
| 124 |
+
def _info(self):
|
| 125 |
+
# features = {feature: datasets.Value("string") for feature in self.config.features}
|
| 126 |
+
features = {}
|
| 127 |
+
features["query_name"] = datasets.Value("string")
|
| 128 |
+
features["context_blocks"] = datasets.features.Sequence(
|
| 129 |
+
{
|
| 130 |
+
"content": datasets.Value("string"),
|
| 131 |
+
"metadata": datasets.Value("string"),
|
| 132 |
+
"header": datasets.Value("string")
|
| 133 |
+
}
|
| 134 |
+
)
|
| 135 |
+
features["answer_spans"] = datasets.features.Sequence(
|
| 136 |
+
{
|
| 137 |
+
'span': datasets.Value("string"),
|
| 138 |
+
'start_line': datasets.Value("int32"),
|
| 139 |
+
'start_column': datasets.Value("int32"),
|
| 140 |
+
'end_line': datasets.Value("int32"),
|
| 141 |
+
'end_column': datasets.Value("int32")
|
| 142 |
+
}
|
| 143 |
+
)
|
| 144 |
+
features["supporting_fact_spans"] = datasets.features.Sequence(
|
| 145 |
+
{
|
| 146 |
+
'span': datasets.Value("string"),
|
| 147 |
+
'start_line': datasets.Value("int32"),
|
| 148 |
+
'start_column': datasets.Value("int32"),
|
| 149 |
+
'end_line': datasets.Value("int32"),
|
| 150 |
+
'end_column': datasets.Value("int32")
|
| 151 |
+
}
|
| 152 |
+
)
|
| 153 |
+
features["code_file_path"] = datasets.Value("string")
|
| 154 |
+
features["example_type"] = datasets.Value("int32")
|
| 155 |
+
features["subtokenized_input_sequence"] = datasets.features.Sequence(datasets.Value("string"))
|
| 156 |
+
features["label_sequence"] = datasets.features.Sequence(datasets.Value("int32"))
|
| 157 |
+
|
| 158 |
+
return datasets.DatasetInfo(
|
| 159 |
+
description=self.config.description,
|
| 160 |
+
features=datasets.Features(features),
|
| 161 |
+
homepage=self.config.url,
|
| 162 |
+
citation=_CODEQUERIES_CITATION,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
def _split_generators(self):
|
| 166 |
+
dl_dir = ""
|
| 167 |
+
if self.config.name in ["prefix", "sliding_window", "file_ideal", "twostep"]:
|
| 168 |
+
return [
|
| 169 |
+
datasets.SplitGenerator(
|
| 170 |
+
name=datasets.Split.TEST,
|
| 171 |
+
gen_kwargs={
|
| 172 |
+
"filepath": os.path.join(dl_dir, self.config.name + "_test.json"),
|
| 173 |
+
"split": datasets.Split.TEST,
|
| 174 |
+
},
|
| 175 |
+
),
|
| 176 |
+
]
|
| 177 |
+
else:
|
| 178 |
+
return [
|
| 179 |
+
datasets.SplitGenerator(
|
| 180 |
+
name=datasets.Split.TRAIN,
|
| 181 |
+
gen_kwargs={
|
| 182 |
+
"filepath": os.path.join(dl_dir, self.config.name + "_train.json"),
|
| 183 |
+
"split": datasets.Split.TRAIN,
|
| 184 |
+
},
|
| 185 |
+
),
|
| 186 |
+
datasets.SplitGenerator(
|
| 187 |
+
name=datasets.Split.VALIDATION,
|
| 188 |
+
gen_kwargs={
|
| 189 |
+
"filepath": os.path.join(dl_dir, self.config.name + "_val.json"),
|
| 190 |
+
"split": datasets.Split.VALIDATION,
|
| 191 |
+
},
|
| 192 |
+
),
|
| 193 |
+
datasets.SplitGenerator(
|
| 194 |
+
name=datasets.Split.TEST,
|
| 195 |
+
gen_kwargs={
|
| 196 |
+
"filepath": os.path.join(dl_dir, self.config.name + "_test.json"),
|
| 197 |
+
"split": datasets.Split.TEST,
|
| 198 |
+
},
|
| 199 |
+
),
|
| 200 |
+
]
|
| 201 |
+
|
| 202 |
+
def _generate_examples(self, filepath, split):
|
| 203 |
+
if self.config.name in ["prefix", "sliding_window", "file_ideal", "twostep"]:
|
| 204 |
+
assert split == datasets.Split.TEST
|
| 205 |
+
logger.info("generating examples from = %s", filepath)
|
| 206 |
+
|
| 207 |
+
with open(filepath, encoding="utf-8") as f:
|
| 208 |
+
key = 0
|
| 209 |
+
for line in f:
|
| 210 |
+
row = json.loads(line)
|
| 211 |
+
|
| 212 |
+
instance_key = key + "_" + row["query_name"] + "_" + row["code_file_path"]
|
| 213 |
+
yield instance_key, {
|
| 214 |
+
"query_name": row["query_name"],
|
| 215 |
+
"context_blocks": row["context_blocks"],
|
| 216 |
+
"answer_spans": row["answer_spans"],
|
| 217 |
+
"supporting_fact_spans": row["supporting_fact_spans"],
|
| 218 |
+
"code_file_path": row["code_file_path"],
|
| 219 |
+
"example_type": row["example_type"],
|
| 220 |
+
"subtokenized_input_sequence ": row["subtokenized_input_sequence "],
|
| 221 |
+
"label_sequence": row["label_sequence"],
|
| 222 |
+
}
|
| 223 |
+
key += 1
|