import json import datasets _DESCRIPTION = """Translating Python Programming Puzzles (TP3) is a code translation benchmark created from the verification functions from the questions in the original Python Programming Puzzles dataset (Schuster et al., 2021) to create this dataset. These functions are hand-crafted by the authors and are used to check if an answer satisfies the constraints of the puzzle. These puzzles range in difficulty from basic character checking to competitive programming problems. Thus, each verification function is written by an expert python programmer and requires a significant understanding of programming to translate. In total, there are 370 python functions to translate.""" _URL = "https://raw.githubusercontent.com/google-research/babelcode/main/data/hf_datasets/tp3.jsonl" _LANGUAGES = { "C++", "CSharp", "Dart", "Go", "Haskell", "Java", "Javascript", "Julia", "Kotlin", "Lua", "PHP", "R", "Rust", "Scala", "TypeScript", } _CITATION = """\ @article{orlanski2023measuring, title={Measuring The Impact Of Programming Language Distribution}, author={Orlanski, Gabriel and Xiao, Kefan and Garcia, Xavier and Hui, Jeffrey and Howland, Joshua and Malmaud, Jonathan and Austin, Jacob and Singh, Rishah and Catasta, Michele}, journal={arXiv preprint arXiv:2302.01973}, year={2023} } @inproceedings{ schuster2021programming, title={Programming Puzzles}, author={Tal Schuster and Ashwin Kalyan and Alex Polozov and Adam Tauman Kalai}, booktitle={Thirty-fifth Conference on Neural Information Processing Systems Datasets and Benchmarks Track}, year={2021}, url={https://arxiv.org/abs/2106.05784} }""" _HOMEPAGE = "https://github.com/google-research/babelcode" _LICENSE = "CC-BY-4.0" _VERSION = "1.0.0" _QUESTION_INFO_KEYS = { "entry_fn_name", "entry_cls_name", "test_code", "test_list", "test_case_ids", "commands", "timeouts", "extension" } class TP3(datasets.GeneratorBasedBuilder): """TP3""" VERSION = datasets.Version(_VERSION) BUILDER_CONFIGS = [ datasets.BuilderConfig( name="all", version=datasets.Version(_VERSION), description=_DESCRIPTION, ), ] + [ datasets.BuilderConfig( name=lang, version=datasets.Version(_VERSION), description=_DESCRIPTION + f" Examples are only in {lang}.", ) for lang in _LANGUAGES ] DEFAULT_CONFIG_NAME = "all" def _info(self): list_keys = ['timeouts', 'commands', 'test_case_ids'] question_info_type = { k: datasets.Value(dtype="string") for k in _QUESTION_INFO_KEYS if k not in list_keys } question_info_type['test_case_ids'] = datasets.Sequence( datasets.Value('string')) question_info_type['commands'] = datasets.Sequence( datasets.Sequence(datasets.Value('string'))) question_info_type['timeouts'] = datasets.Sequence(datasets.Value('int32')) features = datasets.Features({ "qid": datasets.Value("string"), "title": datasets.Value("string"), "language": datasets.Value("string"), "text": datasets.Value("string"), "signature_with_docstring": datasets.Value("string"), "signature": datasets.Value("string"), "arguments": datasets.Sequence(datasets.Value("string")), "source": datasets.Value("string"), "question_info": datasets.Features(question_info_type) }) description = _DESCRIPTION if self.config.name != 'all': description = _DESCRIPTION + f" Examples are only in {self.config.name}." return datasets.DatasetInfo( description=description, features=features, supervised_keys=None, homepage=_HOMEPAGE, license=_LICENSE, citation=_CITATION, ) def _split_generators(self, dl_manager): """Returns SplitGenerators.""" data_dir = dl_manager.download_and_extract(_URL) return [ datasets.SplitGenerator( name=datasets.Split.TEST, gen_kwargs={"filepath": data_dir}, ), ] def _generate_examples(self, filepath): """ Yields the examples from the dataset""" with open(filepath, encoding='utf-8') as file: id_ = 0 for l in file: if not l.strip(): continue d = json.loads(l) if self.config.name != 'all' and d['language'] != self.config.name: continue question_info = {} for k in _QUESTION_INFO_KEYS: question_info[k] = d.pop(k) question_info['test_list'] = json.dumps(question_info['test_list']) d['question_info'] = question_info d['source'] = d.pop('solution_python') yield id_, d id_ += 1