def test_transform_without_character(self): # No character feature. it = IndexTransformer(use_char=False) x, y = it.fit_transform(self.x, self.y) # Check sequence length. self.assertEqual(len(x), len(self.x)) self.assertEqual(len(y), len(self.y)) # Check sequence type. self.assertIsInstance(x, np.ndarray) self.assertIsInstance(y, np.ndarray)
def test_transform_with_character(self): # With character feature. it = IndexTransformer(use_char=True) X, y = it.fit_transform(self.x, self.y) words, chars, length = X # Check sequence length. self.assertEqual(len(words), len(self.x)) self.assertEqual(len(chars), len(self.x)) self.assertEqual(len(y), len(self.y)) # Check sequence type. self.assertIsInstance(words, np.ndarray) self.assertIsInstance(chars, np.ndarray) self.assertIsInstance(y, np.ndarray)
def test_save_and_load(self): it = IndexTransformer(lower=False) x1, y1 = it.fit_transform(self.x, self.y) x1_word, x1_char, x1_length = x1 self.assertFalse(os.path.exists(self.preprocessor_file)) it.save(self.preprocessor_file) self.assertTrue(os.path.exists(self.preprocessor_file)) it = IndexTransformer.load(self.preprocessor_file) x2, y2 = it.transform(self.x, self.y) x2_word, x2_char, x2_length = x2 np.testing.assert_array_equal(x1_word, x2_word) np.testing.assert_array_equal(x1_char, x2_char) np.testing.assert_array_equal(y1, y2)
def test_inverse_transform(self): it = IndexTransformer() x, y = it.fit_transform(self.x, self.y) _, _, length = x inv_y = it.inverse_transform(y, length) self.assertEqual(inv_y, self.y)
def test_inverse_transform(self): it = IndexTransformer() x, y = it.fit_transform(self.x, self.y) lengths = map(len, self.y) inv_y = it.inverse_transform(y, lengths) self.assertEqual(inv_y, self.y)