-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata_set_test.py
183 lines (151 loc) · 8.2 KB
/
data_set_test.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
The test cases for data set implementation
@author: yaric
"""
import unittest
import numpy as np
import utils
import config
import data_set as ds
import tree_dict as td
class TestDataSetMethods(unittest.TestCase):
def test_extract_features(self):
text_data = utils.read_json(config.sentence_train_path)
corrections = utils.read_json(config.corrections_train_path)
parse_trees_list = utils.read_json(config.parse_train_path)
glove_indices = utils.read_json(config.glove_train_path)
index = 1
tree, _ = td.treeFromDict(parse_trees_list[index])
features, labels = ds.extractFeatures(node = tree,
sentence = text_data[index],
glove = glove_indices[index],
corrections = corrections[index])
self.assertEqual(len(features), 3, "Wrong features list size")
self.assertEqual(features.shape[1], ds.n_features,
"Wrong feature dimensions: %d" % features.shape[1])
# check labels
labels_test = np.array([0, ds.DT.A, ds.DT.THE], dtype = "int")
self.assertTrue(np.all(labels == labels_test), "Wrong labels generated")
# check features
features_test = np.zeros((3, ds.n_features), dtype = "f")
features_test[0, 0] = 2
features_test[0, 1] = 15189
features_test[0, 2] = 0
features_test[0, ds.offset + ds.POS.DT.value] = 1
features_test[0, ds.offset + ds.POS.NN.value] = 1
features_test[1, 0] = 2
features_test[1, 1] = 19803
features_test[1, 2] = 0
features_test[1, ds.offset + ds.POS.DT.value] = 1
features_test[1, ds.offset + ds.POS.NNP.value] = 1
features_test[1, ds.offset + ds.POS.NN.value] = 1
features_test[2, 0] = 6
features_test[2, 1] = 2062
features_test[2, 2] = 0
features_test[2, ds.offset + ds.POS.DT.value] = 1
features_test[2, ds.offset + ds.POS.NN.value] = 2
features_test[2, ds.offset + ds.POS.IN.value] = 1
features_test[2, ds.offset + ds.POS.PRP_.value] = 1
select = features == features_test
if np.all(select) == False:
print(np.argmin(select, axis = 0))
self.fail("Wrong features generated")
def test_extract_Pos_Tags_Features(self):
text_data = utils.read_json(config.sentence_train_path)
corrections = utils.read_json(config.corrections_train_path)
glove_indices = utils.read_json(config.glove_train_path)
pos_tags = utils.read_json(config.pos_tags_train_path)
s_index = 1
features, labels = ds.extractPosTagsFeatures(text_data[s_index], pos_tags[s_index],
glove_indices[s_index], corrections[s_index])
self.assertEqual(len(features), 3, "Wrong features list size")
self.assertEqual(features.shape[1], ds.n_features_pos_tags,
"Wrong feature dimensions: %d" % features.shape[1])
# check labels
labels_test = np.array([0, ds.DT.A, ds.DT.THE], dtype = "int")
self.assertTrue(np.all(labels == labels_test), "Wrong labels generated")
# check features
"""
PrW | PrW POS | DTa | FlW | FlW POS | FlW2 | FlW2 POS | FlNNs (i > 0) | FlNNs (i > 0) POS |
PrW2 | PrW2 POS | PrW3 (VB, VBD) | PrW3 (VB, VBD) POS | Vowel ([0, 1] | FlW (VB,VBD,VBG,VBN,VBP,VBZ) | FlW (VB,VBD,VBG,VBN,VBP,VBZ) POS
"""
features_test = np.array([
[21176.0, 27.0, 2.0, 15189.0, 12.0, 28.0, 6.0, 15189.0, 12.0, 18.0, 18.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[28.0, 6.0, 2.0, 19803.0, 14.0, 16560.0, 12.0, 16560.0, 12.0, 15189.0, 12.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[365.0, 27.0, 6.0, 2062.0, 12.0, 5.0, 6.0, 2062.0, 12.0, 4.0, 25.0, 0.0, 0.0, 0.0, 0.0, 0.0]], dtype = "f")
select = features == features_test
if np.all(select) == False:
print(np.argmin(select, axis = 0))
self.fail("Wrong features generated")
def test_create_train_data_set_WithPosTags(self):
print("Train -- ")
features, labels = ds.createWithPosTags(
corpus_file = config.sentence_train_path,
pos_tags_file = config.pos_tags_train_path,
glove_file = config.glove_train_path,
corrections_file = config.corrections_train_path)
self.assertEqual(len(features), len(labels),
"The train features list has size not equal to the labels")
self.assertEqual(features.shape[1], ds.n_features_pos_tags,
"Wrong train feature dimensions: %d" % features.shape[1])
def test_create_validate_data_set_WithPosTags(self):
print("Validate -- ")
features, labels = ds.createWithPosTags(
corpus_file = config.sentence_validate_path,
pos_tags_file = config.pos_tags_validate_path,
glove_file = config.glove_validate_path,
corrections_file = config.corrections_validate_path)
self.assertEqual(len(features), len(labels),
"The validate features list has size not equal to the labels")
self.assertEqual(features.shape[1], ds.n_features_pos_tags,
"Wrong validate feature dimensions: %d" % features.shape[1])
def test_create_test_data_set_WithPosTags(self):
print("Test -- ")
features, labels = ds.createWithPosTags(
corpus_file = config.sentence_test_path,
pos_tags_file = config.pos_tags_test_path,
glove_file = config.glove_test_path,
corrections_file = None,
test = True)
self.assertIsNone(labels, "Labels should not be returned")
self.assertGreater(features.shape[0], 0, "Empty test features returned")
self.assertEqual(features.shape[1], ds.n_features_pos_tags,
"Wrong test feature dimensions: %d" % features.shape[1])
def test_create_train_data_set(self):
print("Train - ")
features, labels = ds.create(
corpus_file = config.sentence_train_path,
parse_tree_file = config.parse_train_path,
glove_file = config.glove_train_path,
corrections_file = config.corrections_train_path)
self.assertEqual(len(features), len(labels),
"The train features list has size not equal to the labels")
self.assertEqual(features.shape[1], ds.n_features,
"Wrong feature dimensions: %d" % features.shape[1])
def test_create_validate_data_set(self):
print("Validate - ")
features, labels = ds.create(
corpus_file = config.sentence_validate_path,
parse_tree_file = config.parse_validate_path,
glove_file = config.glove_validate_path,
corrections_file = config.corrections_validate_path)
self.assertEqual(len(features), len(labels),
"The validate features list has size not equal to the labels")
self.assertEqual(features.shape[1], ds.n_features,
"Wrong feature dimensions: %d" % features.shape[1])
def test_create_test_data_set(self):
print("Test - ")
features, labels = ds.create(
corpus_file = config.sentence_test_path,
parse_tree_file = config.parse_test_path,
glove_file = config.glove_test_path,
corrections_file = None,
test = True)
self.assertIsNone(labels, "Labels should not be returned")
self.assertGreater(features.shape[0], 0, "Empty features returned")
self.assertEqual(features.shape[1], ds.n_features,
"Wrong feature dimensions: %d" % features.shape[1])
if __name__ == '__main__':
unittest.main()