-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathdata_utils.py
executable file
·88 lines (75 loc) · 2.82 KB
/
data_utils.py
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
import numpy as np
import re
import csv
class Data(object):
"""
Class to handle loading and processing of raw datasets.
"""
def __init__(self, data_source,
alphabet="abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{}",
input_size=1014, num_of_classes=4):
"""
Initialization of a Data object.
Args:
data_source (str): Raw data file path
alphabet (str): Alphabet of characters to index
input_size (int): Size of input features
num_of_classes (int): Number of classes in data
"""
self.alphabet = alphabet
self.alphabet_size = len(self.alphabet)
self.dict = {} # Maps each character to an integer
self.no_of_classes = num_of_classes
for idx, char in enumerate(self.alphabet):
self.dict[char] = idx + 1
self.length = input_size
self.data_source = data_source
def load_data(self):
"""
Load raw data from the source file into data variable.
Returns: None
"""
data = []
with open(self.data_source, 'r', encoding='utf-8') as f:
rdr = csv.reader(f, delimiter=',', quotechar='"')
for row in rdr:
txt = ""
for s in row[1:]:
txt = txt + " " + re.sub("^\s*(.-)\s*$", "%1", s).replace("\\n", "\n")
data.append((int(row[0]), txt)) # format: (label, text)
self.data = np.array(data)
print("Data loaded from " + self.data_source)
def get_all_data(self):
"""
Return all loaded data from data variable.
Returns:
(np.ndarray) Data transformed from raw to indexed form with associated one-hot label.
"""
data_size = len(self.data)
start_index = 0
end_index = data_size
batch_texts = self.data[start_index:end_index]
batch_indices = []
one_hot = np.eye(self.no_of_classes, dtype='int64')
classes = []
for c, s in batch_texts:
batch_indices.append(self.str_to_indexes(s))
c = int(c) - 1
classes.append(one_hot[c])
return np.asarray(batch_indices, dtype='int64'), np.asarray(classes)
def str_to_indexes(self, s):
"""
Convert a string to character indexes based on character dictionary.
Args:
s (str): String to be converted to indexes
Returns:
str2idx (np.ndarray): Indexes of characters in s
"""
s = s.lower()
max_length = min(len(s), self.length)
str2idx = np.zeros(self.length, dtype='int64')
for i in range(1, max_length + 1):
c = s[-i]
if c in self.dict:
str2idx[i - 1] = self.dict[c]
return str2idx