Create dataset_creation.py
Browse files- dataset_creation.py +138 -0
dataset_creation.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import load_dataset, Dataset
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from collections import defaultdict
|
| 4 |
+
import pygments
|
| 5 |
+
|
| 6 |
+
list_languages = ['ada', 'agda', 'alloy', 'antlr', 'applescript', 'assembly', 'augeas', 'awk', 'batchfile', 'bison',
|
| 7 |
+
'bluespec', 'c', 'c++', 'c-sharp', 'clojure', 'cmake', 'coffeescript', 'common-lisp', 'css', 'cuda', 'dart', 'dockerfile', 'elixir',
|
| 8 |
+
'elm', 'emacs-lisp','erlang', 'f-sharp', 'fortran', 'glsl', 'go', 'groovy', 'haskell','html', 'idris', 'isabelle', 'java',
|
| 9 |
+
'java-server-pages', 'javascript', 'stan', 'julia', 'kotlin', 'lean', 'literate-agda', 'literate-coffeescript', 'literate-haskell',
|
| 10 |
+
'lua', 'makefile', 'maple', 'markdown', 'mathematica', 'matlab', 'ocaml', 'pascal', 'perl', 'php', 'powershell', 'prolog',
|
| 11 |
+
'protocol-buffer', 'python', 'r', 'racket', 'restructuredtext', 'rmarkdown', 'ruby', 'rust', 'sas', 'scala', 'scheme',
|
| 12 |
+
'shell', 'smalltalk', 'solidity', 'sparql', 'sql', 'stan', 'standard-ml', 'stata', 'systemverilog', 'tcl', 'tcsh', 'tex',
|
| 13 |
+
'thrift', 'typescript', 'verilog', 'vhdl', 'visual-basic', 'xslt', 'yacc', 'zig']
|
| 14 |
+
|
| 15 |
+
lmap = {'c-sharp':'csharp', 'f-sharp':'fsharp', 'standard-ml':'sml', 'batchfile':'batch','java-server-pages':'jsp'}
|
| 16 |
+
|
| 17 |
+
extra_columns = [
|
| 18 |
+
"hexsha",
|
| 19 |
+
"max_stars_repo_path",
|
| 20 |
+
"max_stars_repo_name",
|
| 21 |
+
"max_stars_repo_head_hexsha",
|
| 22 |
+
"max_stars_repo_stars_event_min_datetime",
|
| 23 |
+
"max_stars_repo_stars_event_max_datetime",
|
| 24 |
+
"max_issues_repo_path",
|
| 25 |
+
"max_issues_repo_name",
|
| 26 |
+
"max_issues_repo_head_hexsha",
|
| 27 |
+
"max_issues_repo_licenses",
|
| 28 |
+
"max_issues_count",
|
| 29 |
+
"max_issues_repo_issues_event_min_datetime",
|
| 30 |
+
"max_issues_repo_issues_event_max_datetime",
|
| 31 |
+
"max_forks_repo_path",
|
| 32 |
+
"max_forks_repo_name",
|
| 33 |
+
"max_forks_repo_head_hexsha",
|
| 34 |
+
"max_forks_repo_licenses",
|
| 35 |
+
"max_forks_count",
|
| 36 |
+
"max_forks_repo_forks_event_min_datetime",
|
| 37 |
+
"max_forks_repo_forks_event_max_datetime",
|
| 38 |
+
]
|
| 39 |
+
|
| 40 |
+
seed = 0
|
| 41 |
+
size = 20_000
|
| 42 |
+
buffer_size = 40_000
|
| 43 |
+
max_data_per_ext = 1000
|
| 44 |
+
df = pd.DataFrame(
|
| 45 |
+
columns=[
|
| 46 |
+
"extension",
|
| 47 |
+
"language",
|
| 48 |
+
"count",
|
| 49 |
+
"low_alphanum_count",
|
| 50 |
+
"long_lines_count",
|
| 51 |
+
"non_lexable_count",
|
| 52 |
+
]
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
def low_alphanum(example):
|
| 56 |
+
return {"low_alphanum": example["alphanum_fraction"] < 0.25}
|
| 57 |
+
|
| 58 |
+
def long_line(example):
|
| 59 |
+
return {"long_lines": example["max_line_length"] > 1000 or example["avg_line_length"] > 100}
|
| 60 |
+
|
| 61 |
+
def pygments_language_id_to_thestack_language_id(str):
|
| 62 |
+
if str in lmap:
|
| 63 |
+
return lmap[str]
|
| 64 |
+
return str
|
| 65 |
+
|
| 66 |
+
def can_lex_without_errors(lexer, contents: str):
|
| 67 |
+
tokens = pygments.lex(contents, lexer)
|
| 68 |
+
for (tok_type, tok_text) in tokens:
|
| 69 |
+
if tok_type == pygments.token.Token.Error:
|
| 70 |
+
return False
|
| 71 |
+
return True
|
| 72 |
+
|
| 73 |
+
def lexable(example, language):
|
| 74 |
+
try:
|
| 75 |
+
lexer = pygments.lexers.get_lexer_by_name(pygments_language_id_to_thestack_language_id(language))
|
| 76 |
+
except:
|
| 77 |
+
return {"lexable": "notfound"}
|
| 78 |
+
return {"lexable": can_lex_without_errors(lexer, example["content"])}
|
| 79 |
+
|
| 80 |
+
|
| 81 |
+
for language in list_languages:
|
| 82 |
+
thestack = load_dataset(
|
| 83 |
+
"bigcode/the-stack",
|
| 84 |
+
use_auth_token=True,
|
| 85 |
+
split="train",
|
| 86 |
+
streaming=True,
|
| 87 |
+
data_dir=f"data/{language}",
|
| 88 |
+
)
|
| 89 |
+
thestack = thestack.shuffle(seed=seed, buffer_size=buffer_size)
|
| 90 |
+
print(f"subset {language} ready, now selecting {size} samples")
|
| 91 |
+
|
| 92 |
+
# 20k subset of random samples from ds, convert to Datasets
|
| 93 |
+
small_ds = list(thestack.take(size))
|
| 94 |
+
small_ds = Dataset.from_pandas(pd.DataFrame(data=small_ds))
|
| 95 |
+
small_ds = small_ds.remove_columns(extra_columns)
|
| 96 |
+
print(f"Dataset of {size} samples of {language} creaded")
|
| 97 |
+
|
| 98 |
+
# get extension distribution
|
| 99 |
+
dict_extensions = defaultdict(int)
|
| 100 |
+
for extension in small_ds["ext"]:
|
| 101 |
+
dict_extensions[extension] += 1
|
| 102 |
+
dict_extensions = dict(dict_extensions)
|
| 103 |
+
print(f"Initial extension dist: {dict_extensions}")
|
| 104 |
+
|
| 105 |
+
# filter for extension
|
| 106 |
+
for ext in dict_extensions:
|
| 107 |
+
ext_ds = small_ds.filter(lambda x: x["ext"] == ext)
|
| 108 |
+
real_count = min(max_data_per_ext, len(ext_ds))
|
| 109 |
+
ext_ds = ext_ds.select(range(real_count))
|
| 110 |
+
|
| 111 |
+
# let's add extra info
|
| 112 |
+
ext_ds = ext_ds.map(low_alphanum)
|
| 113 |
+
ext_ds = ext_ds.map(long_line)
|
| 114 |
+
ext_ds = ext_ds.map(lambda x: lexable(x, language))
|
| 115 |
+
|
| 116 |
+
low_alphanum_count = sum(
|
| 117 |
+
low_alphanum for low_alphanum in ext_ds["low_alphanum"]
|
| 118 |
+
)
|
| 119 |
+
long_lines_count = sum(long_line for long_line in ext_ds["long_lines"])
|
| 120 |
+
non_lexable_count = sum(not lexable for lexable in ext_ds["lexable"])
|
| 121 |
+
|
| 122 |
+
new_dict = {
|
| 123 |
+
"extension": ext,
|
| 124 |
+
"language": language,
|
| 125 |
+
"count": real_count,
|
| 126 |
+
"low_alphanum_count": low_alphanum_count,
|
| 127 |
+
"long_lines_count": long_lines_count,
|
| 128 |
+
"non_lexable_count": non_lexable_count,
|
| 129 |
+
}
|
| 130 |
+
df = df.append(new_dict, ignore_index=True)
|
| 131 |
+
print(f"New extension count: {new_dict}")
|
| 132 |
+
|
| 133 |
+
path = f"./data/{language}/{ext}/data.json"
|
| 134 |
+
ext_ds.to_json(path)
|
| 135 |
+
print(f"Subset of langugae: {language}, and extension: {ext} saved")
|
| 136 |
+
|
| 137 |
+
# save the dataframe to csv
|
| 138 |
+
df.to_csv("./data/extension_distribution.csv")
|