-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreprocessing.py
96 lines (81 loc) · 3.55 KB
/
preprocessing.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
89
90
91
92
93
94
95
96
# based on ideas from https://github.com/dennybritz/cnn-text-classification-tf
import numpy as np
import json
def load_yelp(alphabet):
examples = []
labels = []
with open('./yelp-review-dataset/yelp_academic_dataset_review.json') as f:
i = 0
for line in f:
review = json.loads(line)
stars = review["stars"]
text = review["text"]
if stars != 3:
text_end_extracted = extract_end(list(text.lower()))
padded = pad_sentence(text_end_extracted)
text_int8_repr = string_to_int8_conversion(padded, alphabet)
if stars == 1 or stars == 2:
labels.append([1, 0])
examples.append(text_int8_repr)
elif stars == 4 or stars == 5:
labels.append([0, 1])
examples.append(text_int8_repr)
i += 1
if i % 10000 == 0:
print("Non-neutral instances processed: " + str(i))
return examples, labels
def extract_end(char_seq):
if len(char_seq) > 1014:
char_seq = char_seq[-1014:]
return char_seq
def pad_sentence(char_seq, padding_char=" "):
char_seq_length = 1014
num_padding = char_seq_length - len(char_seq)
new_char_seq = char_seq + [padding_char] * num_padding
return new_char_seq
def string_to_int8_conversion(char_seq, alphabet):
x = np.array([alphabet.find(char) for char in char_seq], dtype=np.int8)
return x
def get_batched_one_hot(char_seqs_indices, labels, start_index, end_index):
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{}\n"
x_batch = char_seqs_indices[start_index:end_index]
y_batch = labels[start_index:end_index]
x_batch_one_hot = np.zeros(shape=[len(x_batch), len(alphabet), len(x_batch[0]), 1])
for example_i, char_seq_indices in enumerate(x_batch):
for char_pos_in_seq, char_seq_char_ind in enumerate(char_seq_indices):
if char_seq_char_ind != -1:
x_batch_one_hot[example_i][char_seq_char_ind][char_pos_in_seq][0] = 1
return [x_batch_one_hot, y_batch]
def load_data():
# TODO Add the new line character later for the yelp'cause it's a multi-line review
alphabet = "abcdefghijklmnopqrstuvwxyz0123456789-,;.!?:'\"/\\|_@#$%^&*~`+-=<>()[]{}\n"
examples, labels = load_yelp(alphabet)
x = np.array(examples, dtype=np.int8)
y = np.array(labels, dtype=np.int8)
print("x_char_seq_ind=" + str(x.shape))
print("y shape=" + str(y.shape))
return [x, y]
def batch_iter(x, y, batch_size, num_epochs, shuffle=True):
"""
Generates a batch iterator for a dataset.
"""
# data = np.array(data)
data_size = len(x)
num_batches_per_epoch = int(data_size/batch_size) + 1
for epoch in range(num_epochs):
print("In epoch >> " + str(epoch + 1))
print("num batches per epoch is: " + str(num_batches_per_epoch))
# Shuffle the data at each epoch
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
x_shuffled = x[shuffle_indices]
y_shuffled = y[shuffle_indices]
else:
x_shuffled = x
y_shuffled = y
for batch_num in range(num_batches_per_epoch):
start_index = batch_num * batch_size
end_index = min((batch_num + 1) * batch_size, data_size)
x_batch, y_batch = get_batched_one_hot(x_shuffled, y_shuffled, start_index, end_index)
batch = list(zip(x_batch, y_batch))
yield batch