Create scotch_try.py
Browse files- scotch_try.py +162 -0
scotch_try.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
language_map={'python':'py','javascript':'js','go':'go','java':'java'}
|
55 |
+
_URLs = {lang:f'https://scotchdata.s3.amazonaws.com/go.tar.gz' for lang in languages}
|
56 |
+
_URLs['all']=_URLs.copy()
|
57 |
+
|
58 |
+
|
59 |
+
# TODO: Name of the dataset usually match the script name with CamelCase instead of snake_case
|
60 |
+
class ScotchDataset(datasets.GeneratorBasedBuilder):
|
61 |
+
VERSION = datasets.Version("1.0.0")
|
62 |
+
BUILDER_CONFIGS = [
|
63 |
+
datasets.BuilderConfig(name="all", version=VERSION, description="All available data with docstrings"),
|
64 |
+
datasets.BuilderConfig(name="python", version=VERSION, description="Python data"),
|
65 |
+
datasets.BuilderConfig(name="javascript", version=VERSION, description="Javascript data"),
|
66 |
+
datasets.BuilderConfig(name="java", version=VERSION, description="Java data"),
|
67 |
+
datasets.BuilderConfig(name="go", version=VERSION, description="Go data"),
|
68 |
+
]
|
69 |
+
|
70 |
+
DEFAULT_CONFIG_NAME = "all"
|
71 |
+
|
72 |
+
def _info(self):
|
73 |
+
# TODO: This method specifies the datasets.DatasetInfo object which contains informations and typings for the dataset
|
74 |
+
|
75 |
+
features = datasets.Features(
|
76 |
+
{
|
77 |
+
"repository_name": datasets.Value("string"),
|
78 |
+
"function_path": datasets.Value("string"),
|
79 |
+
"function_identifier": datasets.Value("string"),
|
80 |
+
"language": datasets.Value("string"),
|
81 |
+
"function": datasets.Value("string"),
|
82 |
+
"docstring": datasets.Value("string"),
|
83 |
+
"function_url": datasets.Value("string"),
|
84 |
+
"context":datasets.Value("string"),
|
85 |
+
"license":datasets.Value("string"),
|
86 |
+
}
|
87 |
+
)
|
88 |
+
return datasets.DatasetInfo(
|
89 |
+
description=_DESCRIPTION,
|
90 |
+
features=features, # Here we define them above because they are different between the two configurations
|
91 |
+
supervised_keys=None,
|
92 |
+
homepage=_HOMEPAGE,
|
93 |
+
license=_LICENSE,
|
94 |
+
citation=_CITATION,
|
95 |
+
)
|
96 |
+
|
97 |
+
def _split_generators(self, dl_manager):
|
98 |
+
"""Returns SplitGenerators."""
|
99 |
+
my_urls = _URLs[self.config.name]
|
100 |
+
if isinstance(my_urls, str):
|
101 |
+
my_urls = {self.config.name:my_urls}
|
102 |
+
data_dir = [os.path.join(lang_dir,lang) for lang,lang_dir in dl_manager.download_and_extract(my_urls).items()]
|
103 |
+
|
104 |
+
# splitpaths={split:[os.path.join(lang_dir,f'{split}.bin') for lang_dir in data_dir] for split in ['train','valid','test']}
|
105 |
+
splitpaths={}
|
106 |
+
for split in ['train','valid','test']:
|
107 |
+
for lang_dir in data_dir:
|
108 |
+
# Path glob .bin files
|
109 |
+
lang_split_files=sorted(Path(os.path.join(lang_dir,split)).glob('*.bin'))
|
110 |
+
if not split in splitpaths:
|
111 |
+
splitpaths[split]=lang_split_files
|
112 |
+
else:
|
113 |
+
splitpaths[split].extend(lang_split_files)
|
114 |
+
|
115 |
+
return [
|
116 |
+
datasets.SplitGenerator(
|
117 |
+
name=datasets.Split.TRAIN,
|
118 |
+
# These kwargs will be passed to _generate_examples
|
119 |
+
gen_kwargs={
|
120 |
+
"filepath": splitpaths['train'],
|
121 |
+
"split": "train",
|
122 |
+
},
|
123 |
+
),
|
124 |
+
datasets.SplitGenerator(
|
125 |
+
name=datasets.Split.TEST,
|
126 |
+
# These kwargs will be passed to _generate_examples
|
127 |
+
gen_kwargs={
|
128 |
+
"filepath": splitpaths['test'],
|
129 |
+
"split": "test"
|
130 |
+
},
|
131 |
+
),
|
132 |
+
datasets.SplitGenerator(
|
133 |
+
name=datasets.Split.VALIDATION,
|
134 |
+
# These kwargs will be passed to _generate_examples
|
135 |
+
gen_kwargs={
|
136 |
+
"filepath": splitpaths['valid'],
|
137 |
+
"split": "valid",
|
138 |
+
},
|
139 |
+
),
|
140 |
+
]
|
141 |
+
|
142 |
+
def _generate_examples(
|
143 |
+
self, filepath,split # method parameters are unpacked from `gen_kwargs` as given in `_split_generators`
|
144 |
+
):
|
145 |
+
""" Yields examples as (key, example) tuples. """
|
146 |
+
count=-1
|
147 |
+
for i,filepath in enumerate(filepath):
|
148 |
+
loaded_f=pickle.load(open(filepath,'rb'))
|
149 |
+
for j, func in enumerate(loaded_f):
|
150 |
+
count+=1
|
151 |
+
yield count,{
|
152 |
+
"repository_name": str(func['nwo']),
|
153 |
+
"function_path":str(func['path']),
|
154 |
+
"function_identifier": str(func['identifier']),
|
155 |
+
"language": str(func['language']),
|
156 |
+
"function": str(func['function']),
|
157 |
+
"docstring": str(func['docstring']),
|
158 |
+
"function_url": str(func['url']),
|
159 |
+
"context":str(func['context']),
|
160 |
+
"license":str(func['license']),
|
161 |
+
}
|
162 |
+
|