Datasets:
gabeorlanski
commited on
Commit
•
d73f58b
1
Parent(s):
8ecb6f4
Upload bc-transcoder.py
Browse files- bc-transcoder.py +135 -0
bc-transcoder.py
ADDED
@@ -0,0 +1,135 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import json
|
2 |
+
|
3 |
+
import datasets
|
4 |
+
|
5 |
+
|
6 |
+
_DESCRIPTION = """The Transcoder dataset in BabelCode format. Currently supports translation from C++ and Python."""
|
7 |
+
|
8 |
+
_URL = "https://raw.githubusercontent.com/google-research/babelcode/main/data/hf_datasets/transcoder.jsonl"
|
9 |
+
|
10 |
+
_LANGUAGES = {
|
11 |
+
"C++",
|
12 |
+
"CSharp",
|
13 |
+
"Dart",
|
14 |
+
"Go",
|
15 |
+
"Haskell",
|
16 |
+
"Java",
|
17 |
+
"Javascript",
|
18 |
+
"Julia",
|
19 |
+
"Kotlin",
|
20 |
+
"Lua",
|
21 |
+
"PHP",
|
22 |
+
"Python",
|
23 |
+
"R",
|
24 |
+
"Rust",
|
25 |
+
"Scala",
|
26 |
+
"TypeScript",
|
27 |
+
}
|
28 |
+
|
29 |
+
_CITATION = """\
|
30 |
+
@article{orlanski2023measuring,
|
31 |
+
title={Measuring The Impact Of Programming Language Distribution},
|
32 |
+
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},
|
33 |
+
journal={arXiv preprint arXiv:2302.01973},
|
34 |
+
year={2023}
|
35 |
+
}
|
36 |
+
@article{roziere2020unsupervised,
|
37 |
+
title={Unsupervised translation of programming languages},
|
38 |
+
author={Roziere, Baptiste and Lachaux, Marie-Anne and Chanussot, Lowik and Lample, Guillaume},
|
39 |
+
journal={Advances in Neural Information Processing Systems},
|
40 |
+
volume={33},
|
41 |
+
year={2020}
|
42 |
+
}"""
|
43 |
+
|
44 |
+
_HOMEPAGE = "https://github.com/google-research/babelcode"
|
45 |
+
|
46 |
+
_LICENSE = "CC-BY-4.0"
|
47 |
+
|
48 |
+
_VERSION = "1.0.0"
|
49 |
+
|
50 |
+
_KEYS_REMOVE = {
|
51 |
+
"header"
|
52 |
+
}
|
53 |
+
|
54 |
+
class BCTranscoder(datasets.GeneratorBasedBuilder):
|
55 |
+
"""BC-Transcoder"""
|
56 |
+
|
57 |
+
VERSION = datasets.Version(_VERSION)
|
58 |
+
|
59 |
+
BUILDER_CONFIGS = [
|
60 |
+
datasets.BuilderConfig(
|
61 |
+
name="all",
|
62 |
+
version=datasets.Version(_VERSION),
|
63 |
+
description=_DESCRIPTION,
|
64 |
+
),
|
65 |
+
] + [
|
66 |
+
datasets.BuilderConfig(
|
67 |
+
name=lang,
|
68 |
+
version=datasets.Version(_VERSION),
|
69 |
+
description=_DESCRIPTION + f" Examples are only in {lang}.",
|
70 |
+
)
|
71 |
+
for lang in _LANGUAGES
|
72 |
+
]
|
73 |
+
|
74 |
+
DEFAULT_CONFIG_NAME = "all"
|
75 |
+
|
76 |
+
def _info(self):
|
77 |
+
features = datasets.Features(
|
78 |
+
{
|
79 |
+
"qid": datasets.Value("string"),
|
80 |
+
"title": datasets.Value("string"),
|
81 |
+
"language": datasets.Value("string"),
|
82 |
+
"text":datasets.Value("string"),
|
83 |
+
"signature_with_docstring": datasets.Value("string"),
|
84 |
+
"signature": datasets.Value("string"),
|
85 |
+
"arguments": datasets.Sequence(datasets.Value("string")),
|
86 |
+
"entry_fn_name": datasets.Value("string"),
|
87 |
+
"entry_cls_name": datasets.Value("string"),
|
88 |
+
"test_code": datasets.Value("string"),
|
89 |
+
"source_py":datasets.Value("string"),
|
90 |
+
"source_cpp":datasets.Value("string")
|
91 |
+
}
|
92 |
+
)
|
93 |
+
description = _DESCRIPTION
|
94 |
+
if self.config.name != 'all':
|
95 |
+
description = _DESCRIPTION + f" Examples are only in {self.config.name}."
|
96 |
+
return datasets.DatasetInfo(
|
97 |
+
description=description,
|
98 |
+
features=features,
|
99 |
+
supervised_keys=None,
|
100 |
+
homepage=_HOMEPAGE,
|
101 |
+
license=_LICENSE,
|
102 |
+
citation=_CITATION,
|
103 |
+
)
|
104 |
+
|
105 |
+
def _split_generators(self, dl_manager):
|
106 |
+
"""Returns SplitGenerators."""
|
107 |
+
data_dir = dl_manager.download_and_extract(_URL)
|
108 |
+
return [
|
109 |
+
datasets.SplitGenerator(
|
110 |
+
name=datasets.Split.TEST,
|
111 |
+
gen_kwargs={"filepath": data_dir},
|
112 |
+
),
|
113 |
+
]
|
114 |
+
|
115 |
+
def _generate_examples(self, filepath):
|
116 |
+
""" Yields the examples from the dataset"""
|
117 |
+
with open(filepath, encoding='utf-8') as file:
|
118 |
+
id_ = 0
|
119 |
+
for l in file:
|
120 |
+
if not l.strip():
|
121 |
+
continue
|
122 |
+
d = json.loads(l)
|
123 |
+
|
124 |
+
if self.config.name != 'all' and d['language'] != self.config.name:
|
125 |
+
continue
|
126 |
+
|
127 |
+
d['source_py'] = d.pop('solution_python')
|
128 |
+
d['source_cpp'] = d.pop('solution_cpp')
|
129 |
+
|
130 |
+
for k in _KEYS_REMOVE:
|
131 |
+
d.pop(k)
|
132 |
+
yield id_, d
|
133 |
+
id_+=1
|
134 |
+
|
135 |
+
|