forked from aleju/CharVectorizer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCharVectorizer.py
268 lines (225 loc) · 11.2 KB
/
TestCharVectorizer.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import unittest
import random
import numpy as np
from CharVectorizer import CharVectorizer
ALPHABET_LOWERCASE = "abcdefghijklmnopqrstuvwxyz"
ALPHABET_UPPERCASE = "abcdefghijklmnopqrstuvwxyz".upper()
#PUNCTUATION = ".,:;?!-()"
DIGITS = "0123456789"
def create_random_text(chars, length):
text = []
max_l = len(chars)
for _ in range(0, length):
text.append(chars[random.randint(0, max_l - 1)])
return "".join(text)
def create_random_texts(chars, length_per_string, count):
result = []
for _ in range(0, count):
result.append(create_random_text(chars, length_per_string))
return result
class TestCharVectorizer(unittest.TestCase):
def test_sanity(self):
count_chars = 20
count_texts = 10
vectorizer = CharVectorizer(ALPHABET_LOWERCASE)
texts = create_random_texts(ALPHABET_LOWERCASE, count_chars,
count_texts)
matrix = vectorizer.transform(texts, count_chars)
ones = 0
zeros = 0
for cell in np.nditer(matrix):
if cell == 1:
ones += 1
else:
zeros += 1
self.assertEqual(ones, count_chars * count_texts)
self.assertEqual(ones + zeros,
vectorizer.get_one_char_vector_length()
* count_chars * count_texts)
def test_transform(self):
# 1000 texts only with only known chars
count_chars = 20
count_texts = 1000
vectorizer = CharVectorizer(ALPHABET_LOWERCASE)
texts = create_random_texts(ALPHABET_LOWERCASE, count_chars,
count_texts)
matrix = vectorizer.transform(texts, count_chars)
reverse_transformed = vectorizer.reverse_transform(matrix)
for text_is, text_exp in zip(reverse_transformed, texts):
self.assertEqual(text_is, text_exp)
# 1000 texts only with only unknown chars
count_chars = 20
count_texts = 1000
vectorizer = CharVectorizer(ALPHABET_LOWERCASE,
map_unknown_chars_to="X")
texts = create_random_texts(DIGITS, count_chars,
count_texts)
expected_str = "X" * count_chars
matrix = vectorizer.transform(texts, count_chars)
reverse_transformed = vectorizer.reverse_transform(matrix)
for text_is, text_exp in zip(reverse_transformed, texts):
self.assertEqual(text_is, expected_str)
# 1000 texts with 20% unknown chars
count_chars = 100
count_texts = 1000
map_unknown_chars_to = "X"
known_chars = "abcdefghijklmnopqrstuvwx"
unknown_chars = "!?&/()"
vectorizer = CharVectorizer(known_chars,
map_unknown_chars_to=map_unknown_chars_to)
# abcdefghijklmnopqrstuvwx (24 chars) are known to the vectorizer,
# ?!&/() (6 chars) unknown
# => 6/30 = 1/5 = roughly 20% unknown chars
texts = create_random_texts(known_chars + unknown_chars,
count_chars,
count_texts)
matrix = vectorizer.transform(texts, count_chars)
reverse_transformed = vectorizer.reverse_transform(matrix)
count_known = 0
count_unknown = 0
for text_is in reverse_transformed:
count_unknown_this = text_is.count(map_unknown_chars_to)
count_unknown += count_unknown_this
count_known += len(text_is) - count_unknown_this
count_total = count_known + count_unknown
fraction_is = float(count_unknown) / float(count_total)
self.assertTrue(fraction_is > 0.17 and fraction_is < 0.23,
"Fraction is %f (%d of %d), expected about 0.2" %
(fraction_is, count_unknown, count_total))
def test_vector_lengths(self):
vectorizer = CharVectorizer("abc", map_unknown_chars_to="1",
fill_left_char="2", fill_right_char="3")
self.assertEquals(vectorizer.get_one_char_vector_length(),
len("abc123"))
self.assertEquals(vectorizer.get_vector_length(2),
len("abc123") * 2)
def test_auto_lower(self):
vectorizer = CharVectorizer("abcD", map_unknown_chars_to="X",
auto_lowercase=True, auto_uppercase=False)
texts = ["aaa", "bbb", "ccc", "abc", "AAA", "BBB", "AdD", "EEe", "EeF"]
expected = ["aaa", "bbb", "ccc", "abc", "aaa", "bbb", "aXD", "XXX", "XXX"]
matrix = vectorizer.transform(texts, len(texts[0]))
reverse_transformed = vectorizer.reverse_transform(matrix)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
def test_auto_upper(self):
vectorizer = CharVectorizer("ABCd", map_unknown_chars_to="X",
auto_lowercase=False, auto_uppercase=True)
texts = ["AAA", "BBB", "CCC", "ABC", "aaa", "bbb", "aDd", "eeE", "eEf"]
expected = ["AAA", "BBB", "CCC", "ABC", "AAA", "BBB", "AXd", "XXX", "XXX"]
matrix = vectorizer.transform(texts, len(texts[0]))
reverse_transformed = vectorizer.reverse_transform(matrix)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
def test_fill_left(self):
vectorizer = CharVectorizer("abc", fill_left_char="+",
map_unknown_chars_to="X")
texts = ["a", "aa", "aaa", "b", "bc", "d", "ddd", "abcd"]
expected = ["++a", "+aa", "aaa", "++b", "+bc", "++X", "XXX", "abc"]
matrix = vectorizer.transform(texts, 3, fill_right=False)
reverse_transformed = vectorizer.reverse_transform(matrix)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
def test_fill_right(self):
vectorizer = CharVectorizer("abc", fill_right_char="+",
map_unknown_chars_to="X")
texts = ["a", "aa", "aaa", "b", "bc", "d", "ddd", "abcd"]
expected = ["a++", "aa+", "aaa", "b++", "bc+", "X++", "XXX", "abc"]
matrix = vectorizer.transform(texts, 3, fill_right=True)
reverse_transformed = vectorizer.reverse_transform(matrix)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
def test_reverse_transform(self):
vectorizer = CharVectorizer("abc", map_unknown_chars_to="X")
texts = ["aaa", "bbb", "ccc", "abc", "???", "a?a"]
expected = ["aaa", "bbb", "ccc", "abc", "XXX", "aXa"]
matrix = vectorizer.transform(texts, len(texts[0]))
reverse_transformed = vectorizer.reverse_transform(matrix)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
def test_reverse_transform_string(self):
vectorizer = CharVectorizer("abc", map_unknown_chars_to="X")
texts = ["aaa", "bbb", "ccc", "abc", "???", "a?a"]
expected = ["aaa", "bbb", "ccc", "abc", "XXX", "aXa"]
matrices = []
for text in texts:
matrices.append(vectorizer.transform_string(text, len(texts[0])))
reverse_transformed = []
for matrix in matrices:
for row in matrix:
reverse_transformed.append(vectorizer.reverse_transform_string(row))
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
def test_reverse_transform_char(self):
vectorizer = CharVectorizer("abc", map_unknown_chars_to="X")
texts = ["a", "b", "c", "X", "?"]
expected = ["a", "b", "c", "X", "X"]
matrices = []
for charr in texts:
matrices.append(vectorizer.transform_char(charr))
reverse_transformed = []
for matrix in matrices:
for row in matrix:
reverse_transformed.append(
vectorizer.reverse_transform_char(row)
)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
def test_reverse_transform_maxval(self):
# test on static example texts
vectorizer = CharVectorizer("abc", map_unknown_chars_to="X")
texts = ["aaa", "bbb", "ccc", "abc", "???", "a?a"]
expected = ["aaa", "bbb", "ccc", "abc", "XXX", "aXa"]
matrix = vectorizer.transform(texts, len(texts[0]))
rand = np.random.random_sample(matrix.shape)
matrix = matrix + rand
reverse_transformed = vectorizer.reverse_transform_maxval(matrix)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
# test on 1000 random texts
vectorizer = CharVectorizer(ALPHABET_LOWERCASE,
map_unknown_chars_to="X")
texts = create_random_texts(ALPHABET_LOWERCASE, 20, 1000)
expected = texts
matrix = vectorizer.transform(texts, len(texts[0]))
rand = np.random.random_sample(matrix.shape)
matrix = matrix + rand
reverse_transformed = vectorizer.reverse_transform_maxval(matrix)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
def test_reverse_transform_string_maxval(self):
vectorizer = CharVectorizer("abc", map_unknown_chars_to="X")
texts = ["aaa", "bbb", "ccc", "abc", "???", "a?a"]
expected = ["aaa", "bbb", "ccc", "abc", "XXX", "aXa"]
matrices = []
for text in texts:
matrix = vectorizer.transform_string(text, len(texts[0]))
matrix = matrix + np.random.random_sample(matrix.shape)
matrices.append(matrix)
reverse_transformed = []
for matrix in matrices:
for row in matrix:
reverse_transformed.append(
vectorizer.reverse_transform_string_maxval(row)
)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
def test_reverse_transform_char_maxval(self):
vectorizer = CharVectorizer("abc", map_unknown_chars_to="X")
texts = ["a", "b", "c", "X", "?"]
expected = ["a", "b", "c", "X", "X"]
matrices = []
for charr in texts:
matrix = vectorizer.transform_char(charr)
matrix = matrix + np.random.random_sample(matrix.shape)
matrices.append(matrix)
reverse_transformed = []
for matrix in matrices:
for row in matrix:
reverse_transformed.append(
vectorizer.reverse_transform_char_maxval(row)
)
for text_is, text_exp in zip(reverse_transformed, expected):
self.assertEqual(text_is, text_exp)
if __name__ == '__main__':
unittest.main()