Samip commited on
Commit
7e57faa
·
1 Parent(s): 9cbb583

Upload Scotch.py

Browse files
Files changed (1) hide show
  1. Scotch.py +159 -0
Scotch.py ADDED
@@ -0,0 +1,159 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # coding=utf-8
2
+ # Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
3
+ #
4
+ # Licensed under the Apache License, Version 2.0 (the "License");
5
+ # you may not use this file except in compliance with the License.
6
+ # You may obtain a copy of the License at
7
+ #
8
+ # http://www.apache.org/licenses/LICENSE-2.0
9
+ #
10
+ # Unless required by applicable law or agreed to in writing, software
11
+ # distributed under the License is distributed on an "AS IS" BASIS,
12
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ # See the License for the specific language governing permissions and
14
+ # limitations under the License.
15
+ """TODO: Add a description here."""
16
+
17
+
18
+ import csv
19
+ import json
20
+ import os
21
+
22
+ import datasets
23
+ import pickle
24
+ from pathlib import Path
25
+
26
+ # TODO: Add BibTeX citation
27
+ # Find for instance the citation on arxiv or on the dataset repo/website
28
+ _CITATION = """
29
+ @inproceedings{
30
+ dahal2022scotch,
31
+ title={Scotch: A Semantic Code Search Engine for {IDE}s},
32
+ author={Samip Dahal and Adyasha Maharana and Mohit Bansal},
33
+ booktitle={Deep Learning for Code Workshop},
34
+ year={2022},
35
+ url={https://openreview.net/forum?id=rSxfCiOZk-c}
36
+ }
37
+ """
38
+
39
+ # TODO: Add description of the dataset here
40
+ # You can copy an official description
41
+ _DESCRIPTION = """\
42
+ Scotch is a dataset of about 19 million functions collected from open-source repositiories from GitHub with permissive licenses. Each function has its corresponding code context and about 4 million functions have corresponding docstrings. The dataset includes functions written in programming languages Python, Java, Javascript, and Go."""
43
+
44
+ # TODO: Add a link to an official homepage for the dataset here
45
+ _HOMEPAGE = "https://github.com/sdpmas/Scotch"
46
+
47
+ # TODO: Add the licence for the dataset here if you can find it
48
+ _LICENSE = "The MIT License"
49
+
50
+ # TODO: Add link to the official dataset URLs here
51
+ # The HuggingFace dataset library don't host the datasets but only point to the original files
52
+ # This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)
53
+ languages=['python','javascript','java','go']
54
+ _URLs = {lang:f'https://scotch.s3.amazonaws.com/{lang}.tar.gz' for lang in languages}
55
+ _URLs['all']=_URLs.copy()
56
+
57
+
58
+ # TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
59
+ class ScotchDataset(datasets.GeneratorBasedBuilder):
60
+ VERSION = datasets.Version("1.0.0")
61
+ BUILDER_CONFIGS = [
62
+ datasets.BuilderConfig(name="all", version=VERSION, description="All available data with docstrings"),
63
+ datasets.BuilderConfig(name="python", version=VERSION, description="Python data"),
64
+ datasets.BuilderConfig(name="javascript", version=VERSION, description="Javascript data"),
65
+ datasets.BuilderConfig(name="java", version=VERSION, description="Java data"),
66
+ datasets.BuilderConfig(name="go", version=VERSION, description="Go data"),
67
+ ]
68
+
69
+ DEFAULT_CONFIG_NAME = "all"
70
+
71
+ def _info(self):
72
+ # TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
73
+
74
+ features = datasets.Features(
75
+ {
76
+ "repository_name": datasets.Value("string"),
77
+ "function_path": datasets.Value("string"),
78
+ "function_identifier": datasets.Value("string"),
79
+ "language": datasets.Value("string"),
80
+ "function": datasets.Value("string"),
81
+ "docstring": datasets.Value("string"),
82
+ "function_url": datasets.Value("string"),
83
+ "license":datasets.Value("string"),
84
+ }
85
+ )
86
+ return datasets.DatasetInfo(
87
+ description=_DESCRIPTION,
88
+ features=features, # Here we define them above because they are different between the two configurations
89
+ supervised_keys=None,
90
+ homepage=_HOMEPAGE,
91
+ license=_LICENSE,
92
+ citation=_CITATION,
93
+ )
94
+
95
+ def _split_generators(self, dl_manager):
96
+ """Returns SplitGenerators."""
97
+ my_urls = _URLs[self.config.name]
98
+ if isinstance(my_urls, str):
99
+ my_urls = {self.config.name:my_urls}
100
+ data_dir = [os.path.join(lang_dir,lang) for lang,lang_dir in dl_manager.download_and_extract(my_urls).items()]
101
+
102
+ # splitpaths={split:[os.path.join(lang_dir,f'{split}.bin') for lang_dir in data_dir] for split in ['train','valid','test']}
103
+ splitpaths={}
104
+ for split in ['train','valid','test']:
105
+ for lang_dir in data_dir:
106
+ # Path glob .bin files
107
+ lang_split_files=sorted(Path(os.path.join(lang_dir,split)).glob('*.bin'))
108
+ if not split in splitpaths:
109
+ splitpaths[split]=lang_split_files
110
+ else:
111
+ splitpaths.extend(lang_split_files)
112
+
113
+ return [
114
+ datasets.SplitGenerator(
115
+ name=datasets.Split.TRAIN,
116
+ # These kwargs will be passed to _generate_examples
117
+ gen_kwargs={
118
+ "filepath": splitpaths['train'],
119
+ "split": "train",
120
+ },
121
+ ),
122
+ datasets.SplitGenerator(
123
+ name=datasets.Split.TEST,
124
+ # These kwargs will be passed to _generate_examples
125
+ gen_kwargs={
126
+ "filepath": splitpaths['test'],
127
+ "split": "test"
128
+ },
129
+ ),
130
+ datasets.SplitGenerator(
131
+ name=datasets.Split.VALIDATION,
132
+ # These kwargs will be passed to _generate_examples
133
+ gen_kwargs={
134
+ "filepath": splitpaths['valid'],
135
+ "split": "valid",
136
+ },
137
+ ),
138
+ ]
139
+
140
+ def _generate_examples(
141
+ self, filepath,split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
142
+ ):
143
+ """ Yields examples as (key, example) tuples. """
144
+ count=-1
145
+ for i,filepath in enumerate(filepath):
146
+ loaded_f=pickle.load(open(filepath,'rb'))
147
+ for j, func in enumerate(loaded_f):
148
+ count+=1
149
+ yield count,{
150
+ "repository_name": str(func['nwo']),
151
+ "function_path":str(func['path']),
152
+ "function_identifier": str(func['identifier']),
153
+ "language": str(func['language']),
154
+ "function": str(func['function']),
155
+ "docstring": str(func['docstring']),
156
+ "function_url": str(func['url']),
157
+ "license":str(func['license']),
158
+ }
159
+