-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLine_Of_Dataset.py
More file actions
196 lines (141 loc) · 6.09 KB
/
Line_Of_Dataset.py
File metadata and controls
196 lines (141 loc) · 6.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import os
from collections import Counter
import pickle
import numpy as np
from sklearn.model_selection import train_test_split
import pandas as pd
from imblearn.over_sampling import RandomOverSampler
import re
def convert_triplets(triplets):
converted = []
for triplet in triplets:
# regex pattern that captures the three parts of the triplet separately
pattern = r"(.*?),(b\"[^\"]*\"),(.*)"
matches = re.match(pattern, triplet)
if matches:
# Append a list of the three parts to converted
converted.append([matches.group(1), matches.group(2), matches.group(3)])
return converted
def convert_triplets_new(triplets):
converted = []
for triplet in triplets:
# regex pattern that captures the three parts of both types of triplets separately
pattern = r'(.*?),((?:b\"[^\"]*\"|[a-fA-F0-9]{64})),(.*)'
matches = re.match(pattern, triplet)
if matches:
# Append a list of the three parts to converted
converted.append([matches.group(1), matches.group(2), matches.group(3)])
return converted
def find_triplets(input_string):
# regex pattern that captures triplets in the form "string, b"[...]", string"
pattern = r"([^,]*,b\"[^\"]*\",[^,\s]*)"
matches = re.findall(pattern, input_string)
matches = [match.lstrip() for match in matches]
return matches
def find_triplets_new(input_string):
# regex pattern that captures triplets in the forms:
# "string, b"[...]", string" and "string, hash256, string"
pattern = r'([^,]*,(?:b\"[^\"]*\"|[a-fA-F0-9]{64}),[^,\s]*)'
matches = re.findall(pattern, input_string)
matches = [match.lstrip() for match in matches]
return matches
def split_string(input_string):
# Split the string at the first occurrence of whitespace
split_list = input_string.split(' ', 1)
# If there is no whitespace in the string, return the whole string as the first item and an empty string as the second
if len(split_list) == 1:
return split_list[0], ''
else:
return split_list[0], split_list[1]
def parse_line(line):
"""
Takes a string 'x y1,p1,z1 y2,p2,z2 ... yn,pn,zn and splits into name (x) and tree [[y1,p1,z1], ...]
"""
name, *tree = line.split(' ')
#print(tree[0])
tree = [t.split(',') for t in tree if t != '' and t != '\n']
return name, tree
def create_dict(dest1):
curr=os.getcwd()
datar= os.path.join(curr, dest1, "train1.txt")
with open(datar, 'r') as file_:
listname=[]
listpath=[]
listtoken=[]
for line in file_:
line = line. rstrip('\n')
name,tree = split_string(line)
matches = find_triplets_new(tree)
elements = convert_triplets_new(matches)
for el in elements:
listtoken.append(el[0]) #inserisco lo start token e l'end token in una lista per contare le occorrenze
listtoken.append(el[2])
listpath.append(el[1]) #inserisco il path in una lista per contare le occorrenze
listname.append(name) #inserisco il target in una lsta per contare le occorrenze
target2count=dict(Counter(listname))
path2count= dict(Counter(listpath))
word2count= dict(Counter(listtoken))
with open(os.path.join(dest1,'target2count1.pkl'),'wb') as file:
pickle.dump(target2count, file)
with open(os.path.join(dest1,'path2count1.pkl'), 'wb') as file:
pickle.dump(path2count, file)
with open(os.path.join(dest1,'word2count1.pkl'), 'wb') as file:
pickle.dump(word2count, file)
print("dict created")
def expand2columns(df, col, sep):
r = df[col].str.split(sep,1)
dfinale=pd.DataFrame.from_records(r)
return dfinale
def obtain_label(data):
df= pd.DataFrame(data)
datanew = expand2columns(df,0,sep=' ')
label = datanew.iloc[:,0]
return label
def obtain_X(data):
df= pd.DataFrame(data)
datanew = expand2columns(df,0,sep=' ')
return datanew
def preprocessing(trainset):
ros = RandomOverSampler()
y=obtain_label(trainset)
X= obtain_X(trainset)
X_ros, y_ros = ros.fit_resample(X, y)
return X_ros
def split_train_test_val(pre, dest1):
curr = os.getcwd()
datar = os.path.join(curr, dest1, "datasetgrezzo.txt")
train, test, val = "train1.txt", "test1.txt", "val1.txt"
train = os.path.join(dest1, train)
test = os.path.join(dest1, test)
val = os.path.join(dest1, val)
with open(datar, 'r') as f:
data = np.array(f.read().splitlines())
y=obtain_label(data)
data_train, data_val, y_train, y_val = train_test_split(data,y,test_size=0.2,stratify=y)
y=obtain_label(data_train)
data_train, data_test, y_train, y_test = train_test_split(data_train,y,test_size=0.25,stratify=y)
print('train',y_train.value_counts(),'val',y_val.value_counts(),'test',y_test.value_counts())
if pre:
data_train= preprocessing(data_train) #rimuovere se non si vuole fare l'oversapling
with open(train, 'w', encoding="utf-8") as dataset:
text = '\n'.join(f'{row[0]} {row[1]}' for _, row in data_train.iterrows())
dataset.write(text + '\n')
if not pre:
print('preprocessing offline')
with open(train, 'w', encoding="utf-8") as dataset:
dataset.write('\n'.join(data_train))
print("n_examples:",len(data_train))
with open(os.path.join(dest1,'train1.pkl'), 'wb') as f:
pickle.dump(data_train, f)
with open(test, 'w', encoding="utf-8") as dataset1:
dataset1.write('\n'.join(data_test))
with open(os.path.join(dest1,'test1.pkl'), 'wb') as f1:
pickle.dump(data_test, f1)
with open(val, 'w', encoding="utf-8") as dataset2:
dataset2.write('\n'.join(data_val))
with open(os.path.join(dest1,'val1.pkl'), 'wb') as f2:
pickle.dump(data_val, f2)
print("dataset splitted in train test val")
return data_train, data_test, data_val
#split_train_test_val(True,'cancella')
#create_dict('datasets\processed_dataset\gcjpyredAST')