from typing import List import os import glob import datasets _DESCRIPTION = """Given two codes as the input, the task is to do binary classification (0/1), where 1 stands for semantic equivalence and 0 for others.""" _CITATION = """@inproceedings{10.1145/3236024.3236068, author = {Zhao, Gang and Huang, Jeff}, title = {DeepSim: Deep Learning Code Functional Similarity}, year = {2018}, isbn = {9781450355735}, publisher = {Association for Computing Machinery}, address = {New York, NY, USA}, url = {https://doi.org/10.1145/3236024.3236068}, doi = {10.1145/3236024.3236068}, booktitle = {Proceedings of the 2018 26th ACM Joint Meeting on European Software Engineering Conference and Symposium on the Foundations of Software Engineering}, pages = {141–151}, numpages = {11}, keywords = {Classification, Control/Data flow, Code functional similarity, Deep Learning}, location = {Lake Buena Vista, FL, USA}, series = {ESEC/FSE 2018} } """ SPLITS = { 'test': [5, 6, 7, 8, 12], # For test in `Language Models are Universal Embedders` https://arxiv.org/pdf/2310.08232.pdf 'deepsim': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] } _URL = "https://huggingface.co/datasets/izhx/google-code-jam/resolve/main/googlejam4.tar.gz" class GoogleCodeJam(datasets.GeneratorBasedBuilder): BUILDER_CONFIGS = [ datasets.BuilderConfig(name='default', version=datasets.Version("1.0.0"), description=_DESCRIPTION) ] DEFAULT_CONFIG_NAME = "default" def _info(self): return datasets.DatasetInfo( description=_DESCRIPTION, features=datasets.Features( { "fn1": datasets.Value("string"), "code1": datasets.Value("string"), "fn2": datasets.Value("string"), "code2": datasets.Value("string"), "label": datasets.Value("int32"), } ), homepage="https://github.com/parasol-aser/deepsim", citation=_CITATION, ) def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]: folder = dl_manager.download_and_extract(_URL) folder = os.path.join(folder, 'googlejam4_src') return [ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"folder": folder, "problems": SPLITS["test"]}), datasets.SplitGenerator(name='deepsim', gen_kwargs={"folder": folder, "problems": SPLITS["deepsim"]}), ] def _generate_examples(self, folder, problems: list): raw = dict() for i in problems: group = list() for path in sorted(glob.glob(f'{folder}/{i}/*.java')): with open(path) as file: lines = [l for l in file] name = os.path.basename(path) group.append((name, ''.join(lines[1:]))) # remove name line raw[i] = group _id = 0 reverse = False for i in range(len(problems)): vi = raw[problems[i]] for n1, (fn1, code1) in enumerate(vi): for j in range(i, len(problems)): vj = raw[problems[j]] match = i == j for n2, (fn2, code2) in enumerate(vj): if match and n2 <= n1: continue ins = {'fn1': fn1, 'code1': code1, 'fn2': fn2, 'code2': code2, 'label': int(match)} if reverse: ins['fn1'], ins['fn2'] = ins['fn2'], ins['fn1'] ins['code1'], ins['code2'] = ins['code2'], ins['code1'] yield _id, ins _id += 1 reverse = not reverse