izhx commited on
Commit
4d5586e
1 Parent(s): 05bae75

Create google-code-jam.py

Browse files
Files changed (1) hide show
  1. google-code-jam.py +94 -0
google-code-jam.py ADDED
@@ -0,0 +1,94 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import List
2
+ import os
3
+ import glob
4
+
5
+ import datasets
6
+
7
+
8
+ _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."""
9
+
10
+ _CITATION = """@inproceedings{10.1145/3236024.3236068,
11
+ author = {Zhao, Gang and Huang, Jeff},
12
+ title = {DeepSim: Deep Learning Code Functional Similarity},
13
+ year = {2018},
14
+ isbn = {9781450355735},
15
+ publisher = {Association for Computing Machinery},
16
+ address = {New York, NY, USA},
17
+ url = {https://doi.org/10.1145/3236024.3236068},
18
+ doi = {10.1145/3236024.3236068},
19
+ booktitle = {Proceedings of the 2018 26th ACM Joint Meeting on European Software Engineering Conference and Symposium on the Foundations of Software Engineering},
20
+ pages = {141–151},
21
+ numpages = {11},
22
+ keywords = {Classification, Control/Data flow, Code functional similarity, Deep Learning},
23
+ location = {Lake Buena Vista, FL, USA},
24
+ series = {ESEC/FSE 2018}
25
+ }
26
+ """
27
+
28
+ SPLITS = {
29
+ 'test': [5, 6, 7, 8, 12], # For test in `Language Models are Universal Embedders` https://arxiv.org/pdf/2310.08232.pdf
30
+ 'all': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
31
+ }
32
+ _URL = "https://huggingface.co/datasets/izhx/google-code-jam/resolve/main/googlejam4_src.zip"
33
+
34
+ VERSION = datasets.Version("1.0")
35
+
36
+ class GoogleCodeJam(datasets.GeneratorBasedBuilder):
37
+ BUILDER_CONFIGS = [
38
+ datasets.BuilderConfig(name=name, version=VERSION, description=_DESCRIPTION) for name in SPLITS.items()
39
+ ]
40
+ DEFAULT_CONFIG_NAME = "all"
41
+
42
+ def _info(self):
43
+ return datasets.DatasetInfo(
44
+ description=_DESCRIPTION,
45
+ features=datasets.Features(
46
+ {
47
+ "fn1": datasets.Value("string"),
48
+ "code1": datasets.Value("string"),
49
+ "fn1": datasets.Value("string"),
50
+ "code1": datasets.Value("string"),
51
+ "label": datasets.Value("int32"),
52
+ }
53
+ ),
54
+ supervised_keys="label",
55
+ homepage="https://github.com/parasol-aser/deepsim",
56
+ citation=_CITATION,
57
+ )
58
+
59
+ def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
60
+ folder = dl_manager.download_and_extract(_URL)
61
+ return [
62
+ datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={"folder": folder, "problems": SPLITS["test"]}),
63
+ datasets.SplitGenerator(name='all', gen_kwargs={"folder": folder, "problems": SPLITS["all"]}),
64
+ ]
65
+
66
+ def _generate_examples(self, folder, problems: list):
67
+ raw = dict()
68
+ for i in problems:
69
+ group = list()
70
+ for path in sorted(glob.glob(f'{folder}/{i}/*.java')):
71
+ with open(path) as file:
72
+ lines = [l for l in file]
73
+ name = os.path.basename(path)
74
+ group.append((name, ''.join(lines[1:]))) # remove name line
75
+ raw[i] = group
76
+
77
+ _id = 0
78
+ reverse = False
79
+ for i in range(len(problems)):
80
+ vi = raw[problems[i]]
81
+ for n1, (fn1, code1) in enumerate(vi):
82
+ for j in range(i, len(problems)):
83
+ vj = raw[problems[j]]
84
+ match = i == j
85
+ for n2, (fn2, code2) in enumerate(vj):
86
+ if match and n1 <= n2:
87
+ continue
88
+ ins = {'fn1': fn1, 'code1': code1, 'fn2': fn2, 'code2': code2, 'label': int(match)}
89
+ if reverse:
90
+ ins['fn1'], ins['fn2'] = ins['fn2'], ins['fn1']
91
+ ins['code1'], ins['code2'] = ins['code2'], ins['code1']
92
+ yield _id, ins
93
+ _id += 1
94
+ reverse = not reverse