Datasets:

Modalities:
Text
Languages:
code
ArXiv:
Libraries:
Datasets
License:
loubnabnl HF staff commited on
Commit
517d89b
1 Parent(s): aeeba31

Create new file

Browse files
Files changed (1) hide show
  1. xlcost_conversion.py +91 -0
xlcost_conversion.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from tqdm import tqdm
2
+ from datasets import Dataset
3
+
4
+ """to run inside XLCOST_DATA folder after downloading XLCost data from this repo https://github.com/reddy-lab-code-research/XLCoST"""
5
+
6
+
7
+ class Example(object):
8
+ """A single training/test example."""
9
+ def __init__(self,
10
+ idx,
11
+ source,
12
+ target,
13
+ ):
14
+ self.idx = idx
15
+ self.source = source
16
+ self.target = target
17
+
18
+ def read_examples(filename):
19
+ """Read examples from filename."""
20
+ examples=[]
21
+ assert len(filename.split(','))==2
22
+ src_filename = filename.split(',')[0]
23
+ trg_filename = filename.split(',')[1]
24
+ idx = 0
25
+ with open(src_filename) as f1,open(trg_filename) as f2:
26
+ for line1,line2 in zip(f1,f2):
27
+ examples.append(
28
+ Example(
29
+ idx = idx,
30
+ source=line1.strip(),
31
+ target=line2.strip(),
32
+ )
33
+ )
34
+ idx+=1
35
+ return examples
36
+
37
+ def create_data(filename):
38
+ examples = read_examples(filename)
39
+ text = []
40
+ code = []
41
+ print(len(examples))
42
+ for i in tqdm(range(len(examples))):
43
+ text.append(examples[i].source)
44
+ code.append(examples[i].target)
45
+ data = {"text": text, "code": code}
46
+ data = Dataset.from_dict(data)
47
+ return data
48
+
49
+ if __name__ == "__main__":
50
+ #clone xlcost-text-to-code hub repo
51
+ LANG = ["Python", "C", "C#", "Java", "PHP", "Javascript", "C++"]
52
+ EXTENSION = ["py", "c", "cs", "java", "php", "js", "cpp"]
53
+
54
+ for i in range(len(LANG)):
55
+ # for each language this saves train test and validation subsets for both snippet and program levels
56
+ lang = LANG[i]
57
+ ext = EXTENSION[i]
58
+ print(f"language: {lang}")
59
+ if lang == "C#":
60
+ path_snippet = f"Csharp-snippet-level"
61
+ path_program = f"Csharp-program-level"
62
+ else:
63
+ path_snippet = f"{lang}-snippet-level"
64
+ path_program = f"{lang}-program-level"
65
+
66
+ train_filename = f"generation/pair_data_tok_1_comment/{lang}-comment/train-{lang}-comment-tok.txt,generation/pair_data_tok_1_comment/{lang}-comment/train-{lang}-comment-tok.{ext}"
67
+ valid_filename = f"generation/pair_data_tok_1_comment/{lang}-comment/val-{lang}-comment-tok.txt,generation/pair_data_tok_1_comment/{lang}-comment/val-{lang}-comment-tok.{ext}"
68
+ test_filename = f"generation/pair_data_tok_1_comment/{lang}-comment/test-{lang}-comment-tok.txt,generation/pair_data_tok_1_comment/{lang}-comment/test-{lang}-comment-tok.{ext}"
69
+
70
+ train = create_data(train_filename)
71
+ valid = create_data(valid_filename)
72
+ test = create_data(test_filename)
73
+
74
+ train.to_json(f"xlcost-text-to-code/data/{path_snippet}/train.json", lines=True)
75
+ valid.to_json(f"xlcost-text-to-code/data/{path_snippet}/valid.json", lines=True)
76
+ test.to_json(f"xlcost-text-to-code/data/{path_snippet}/test.json", lines=True)
77
+
78
+ train_filename = f"generation/pair_data_tok_full_desc_comment/{lang}-desc/train-{lang}-desc-tok.txt,generation/pair_data_tok_full_desc_comment/{lang}-desc/train-{lang}-desc-tok.{ext}"
79
+ valid_filename = f"generation/pair_data_tok_full_desc_comment/{lang}-desc/val-{lang}-desc-tok.txt,generation/pair_data_tok_full_desc_comment/{lang}-desc/val-{lang}-desc-tok.{ext}"
80
+ test_filename = f"generation/pair_data_tok_full_desc_comment/{lang}-desc/test-{lang}-desc-tok.txt,generation/pair_data_tok_full_desc_comment/{lang}-desc/test-{lang}-desc-tok.{ext}"
81
+
82
+ train = create_data(train_filename)
83
+ valid = create_data(valid_filename)
84
+ test = create_data(test_filename)
85
+
86
+ train.to_json(f"xlcost-text-to-code/data/{path_program}/train.json", lines=True)
87
+ valid.to_json(f"xlcost-text-to-code/data/{path_program}/valid.json", lines=True)
88
+ test.to_json(f"xlcost-text-to-code/data/{path_program}/test.json", lines=True)
89
+
90
+ #push to hub the folder xlcost (containing data/ and xlcost.py dataset builder script)
91
+