-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path06-sample.py
More file actions
executable file
·66 lines (54 loc) · 1.96 KB
/
Copy path06-sample.py
File metadata and controls
executable file
·66 lines (54 loc) · 1.96 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
"""Create a sample list of java files
This script analyses all java files, count the included tokens, and filters the files depending in the amount of tokens.
The result is a json file including the final sample.
Lower and upper bound for the filtering can be set in config.ini under section [06-sample]
"""
import configparser
import logging
import os
from multiprocessing import Pool
import javalang
import pandas as pd
from javalang.tokenizer import LexerError
from tqdm import tqdm
def process(file_name):
"""
Opens a file and counts the tokens.
Returns a object with filename and the token count depicted as len.
If filename is a directory, a len of 0 will be returned.
"""
if os.path.isdir(file_name) or not os.path.isfile(file_name):
return {'filename': file_name, 'len': 0}
with open(file_name, encoding="ISO-8859-1") as f:
data = f.read()
try:
tokens = list(javalang.tokenizer.tokenize(data))
except LexerError:
tokens = []
return {'filename': file_name, 'len': len(tokens)}
def main():
config = configparser.ConfigParser()
config.read('config.ini')
upper = config['06-sample']['UpperBound']
lower = config['06-sample']['LowerBound']
logging.info('Load javafiles list')
files = []
with open('java-files.txt') as fp:
for line in fp:
files.append('git' + line[1:].replace('\n', ''))
logging.info('Start tokenize processes')
pool = Pool(processes=28)
results = []
for elem in tqdm(pool.imap_unordered(process, files), total=len(files)):
results.append(elem)
df = pd.DataFrame.from_dict(results)
df.describe()
df = df[((df['len'] > lower) & df['len'] < upper)]
df.describe()
df = df.sample(frac=0.5)
train = df.sample(frac=0.7, random_state=42)
test = df.drop(train.index)
train.to_json('train-sample.json', orient='records')
test.to_json('test-sample.json', orient='records')
if __name__ == "__main__":
main()