-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget_data_classes.py
More file actions
84 lines (70 loc) · 2.54 KB
/
get_data_classes.py
File metadata and controls
84 lines (70 loc) · 2.54 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr 12 10:29:31 2020
Prepares data in classes
@author: odrec
"""
from glob import glob
from os import path
import matplotlib.pyplot as plt
import numpy as np
from tika import parser
import json, random
def find_ext(dr, ext):
return glob(path.join(dr,"*.{}".format(ext)))
def plot_bar_x(unique_classes, classes_counts):
# this is for plotting purpose
index = np.arange(len(unique_classes))
plt.bar(index, classes_counts)
plt.xlabel('Classes', fontsize=5)
plt.ylabel('Samples in each class', fontsize=5)
plt.xticks(index, unique_classes, fontsize=5, rotation=30)
plt.title('Classes and their amount of samples')
plt.show()
def get_train_test_split(data, split=0.8):
num_training_samples = int(len(data) * 0.8)
random_keys = random.sample(list(data), num_training_samples)
all_keys = list(data.keys())
training_data = {}
testing_data = {}
for key in all_keys:
if key in random_keys:
training_data[key] = data[key]
else: testing_data[key] = data[key]
return training_data, testing_data
def load_extracted_data(path_to_extracted_data='extracted_data/'):
data_file = open(path_to_extracted_data+'saved_data.json')
data_str = data_file.read()
data = json.loads(data_str)
return data
def extract_data(path_to_data='files/', path_to_extracted_data='extracted_data/'):
pdf_files = find_ext(path_to_data, 'pdf')
data = {}
classes = []
for i,f in enumerate(pdf_files):
pdf_name = pdf_files[i].split('/')[1]
pdf_name = pdf_name.split('.')[0]
data[pdf_name] = {}
raw = parser.from_file(f)
raw_content = raw['content']
raw = raw_content.replace("\n"," ")
raw = raw_content.replace("\t"," ")
data[pdf_name]['content'] = raw_content
tmp = pdf_files[i].split('_')[-1]
filling_type = tmp.split('.')[0]
#remove all unnecessary characters
filling_type = filling_type.replace("-","")
filling_type = filling_type.replace(" ","")
data[pdf_name]['label'] = filling_type
classes.append(filling_type)
with open(path_to_extracted_data+'saved_data.json', 'w+') as fp:
json.dump(data, fp)
unique_classes = set(classes)
classes_counts = []
for uc in unique_classes:
classes_counts.append(classes.count(uc))
#Visualize data differences
plot_bar_x(unique_classes, classes_counts)
if __name__ == "__main__":
extract_data()