-
Notifications
You must be signed in to change notification settings - Fork 139
/
Copy pathdata_helpers.py
127 lines (106 loc) · 4.21 KB
/
data_helpers.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
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
import numpy as np
import pandas as pd
import nltk
import re
import utils
from configure import FLAGS
def clean_str(text):
text = text.lower()
# Clean the text
text = re.sub(r"[^A-Za-z0-9^,!.\/'+-=]", " ", text)
text = re.sub(r"what's", "what is ", text)
text = re.sub(r"that's", "that is ", text)
text = re.sub(r"there's", "there is ", text)
text = re.sub(r"it's", "it is ", text)
text = re.sub(r"\'s", " ", text)
text = re.sub(r"\'ve", " have ", text)
text = re.sub(r"can't", "can not ", text)
text = re.sub(r"n't", " not ", text)
text = re.sub(r"i'm", "i am ", text)
text = re.sub(r"\'re", " are ", text)
text = re.sub(r"\'d", " would ", text)
text = re.sub(r"\'ll", " will ", text)
text = re.sub(r",", " ", text)
text = re.sub(r"\.", " ", text)
text = re.sub(r"!", " ! ", text)
text = re.sub(r"\/", " ", text)
text = re.sub(r"\^", " ^ ", text)
text = re.sub(r"\+", " + ", text)
text = re.sub(r"\-", " - ", text)
text = re.sub(r"\=", " = ", text)
text = re.sub(r"'", " ", text)
text = re.sub(r"(\d+)(k)", r"\g<1>000", text)
text = re.sub(r":", " : ", text)
text = re.sub(r" e g ", " eg ", text)
text = re.sub(r" b g ", " bg ", text)
text = re.sub(r" u s ", " american ", text)
text = re.sub(r"\0s", "0", text)
text = re.sub(r" 9 11 ", "911", text)
text = re.sub(r"e - mail", "email", text)
text = re.sub(r"j k", "jk", text)
text = re.sub(r"\s{2,}", " ", text)
return text.strip()
def load_data_and_labels(path):
data = []
lines = [line.strip() for line in open(path)]
max_sentence_length = 0
for idx in range(0, len(lines), 4):
id = lines[idx].split("\t")[0]
relation = lines[idx + 1]
sentence = lines[idx].split("\t")[1][1:-1]
sentence = sentence.replace('<e1>', ' _e11_ ')
sentence = sentence.replace('</e1>', ' _e12_ ')
sentence = sentence.replace('<e2>', ' _e21_ ')
sentence = sentence.replace('</e2>', ' _e22_ ')
sentence = clean_str(sentence)
tokens = nltk.word_tokenize(sentence)
if max_sentence_length < len(tokens):
max_sentence_length = len(tokens)
sentence = " ".join(tokens)
data.append([id, sentence, relation])
print(path)
print("max sentence length = {}\n".format(max_sentence_length))
df = pd.DataFrame(data=data, columns=["id", "sentence", "relation"])
df['label'] = [utils.class2label[r] for r in df['relation']]
# Text Data
x_text = df['sentence'].tolist()
# Label Data
y = df['label']
labels_flat = y.values.ravel()
labels_count = np.unique(labels_flat).shape[0]
# convert class labels from scalars to one-hot vectors
# 0 => [1 0 0 0 0 ... 0 0 0 0 0]
# 1 => [0 1 0 0 0 ... 0 0 0 0 0]
# ...
# 18 => [0 0 0 0 0 ... 0 0 0 0 1]
def dense_to_one_hot(labels_dense, num_classes):
num_labels = labels_dense.shape[0]
index_offset = np.arange(num_labels) * num_classes
labels_one_hot = np.zeros((num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
return labels_one_hot
labels = dense_to_one_hot(labels_flat, labels_count)
labels = labels.astype(np.uint8)
return x_text, labels
def batch_iter(data, batch_size, num_epochs, shuffle=True):
"""
Generates a batch iterator for a dataset.
"""
data = np.array(data)
data_size = len(data)
num_batches_per_epoch = int((len(data) - 1) / batch_size) + 1
for epoch in range(num_epochs):
# Shuffle the data at each epoch
if shuffle:
shuffle_indices = np.random.permutation(np.arange(data_size))
shuffled_data = data[shuffle_indices]
else:
shuffled_data = data
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)
yield shuffled_data[start_index:end_index]
if __name__ == "__main__":
trainFile = 'SemEval2010_task8_all_data/SemEval2010_task8_training/TRAIN_FILE.TXT'
testFile = 'SemEval2010_task8_all_data/SemEval2010_task8_testing_keys/TEST_FILE_FULL.TXT'
load_data_and_labels(testFile)