1 | import unittest2 as unittest |
---|
2 | import numpy as np |
---|
3 | from scipy.sparse import coo_matrix |
---|
4 | from vsm.model import tfidf |
---|
5 | from vsm.model import tf |
---|
6 | |
---|
7 | |
---|
8 | class TestTfIdf(unittest.TestCase): |
---|
9 | |
---|
10 | def setUp(self): |
---|
11 | self.corpus = np.array([0, 1, 3, 1, 1, 0, 3, 0, 3, |
---|
12 | 3, 0, 1, 0, |
---|
13 | 3, |
---|
14 | 1, 3]) |
---|
15 | self.docs = [slice(0,9), slice(9,13), |
---|
16 | slice(13,14), slice(14,16)] |
---|
17 | self.V = 4 |
---|
18 | |
---|
19 | self.tf_mat = coo_matrix(np.array([[3, 2, 0, 0], |
---|
20 | [3, 1, 0, 1], |
---|
21 | [0, 0, 0, 0], |
---|
22 | [3, 1, 1, 1]], dtype=np.int)) |
---|
23 | self.tfidf_mat = np.array(\ |
---|
24 | [[2.0794415, 1.3862944, 0, 0], |
---|
25 | [0.86304623, 0.28768209, 0, 0.28768209], |
---|
26 | [0, 0, 0, 0], |
---|
27 | [0, 0, 0, 0]]) |
---|
28 | self.undefined_rows = [2] |
---|
29 | |
---|
30 | |
---|
31 | def test_TfIdf_train(self): |
---|
32 | m = tfidf.TfIdf() |
---|
33 | m.train() |
---|
34 | self.assertTrue(m.matrix.size == 0) |
---|
35 | self.assertTrue(len(m.undefined_rows) == 0) |
---|
36 | |
---|
37 | m = tfidf.TfIdf(tf_matrix=self.tf_mat) |
---|
38 | m.train() |
---|
39 | np.testing.assert_almost_equal(self.tfidf_mat, m.matrix.toarray()) |
---|
40 | self.assertEqual(m.undefined_rows, self.undefined_rows) |
---|
41 | |
---|
42 | def test_TfIdf_from_tf(self): |
---|
43 | tf_model = tf.TF() |
---|
44 | tf_model.corpus = self.corpus |
---|
45 | tf_model.docs = self.docs |
---|
46 | tf_model.V = self.V |
---|
47 | tf_model.train() |
---|
48 | self.assertTrue((self.tf_mat == tf_model.matrix.toarray()).all()) |
---|
49 | |
---|
50 | m = tfidf.TfIdf.from_tf(tf_model) |
---|
51 | self.assertTrue((m.matrix == tf_model.matrix.toarray()).all()) |
---|
52 | m.train() |
---|
53 | np.testing.assert_almost_equal(self.tfidf_mat, m.matrix.toarray()) |
---|
54 | self.assertEqual(m.undefined_rows, self.undefined_rows) |
---|
55 | |
---|
56 | |
---|
57 | #Define and run test suite |
---|
58 | suite = unittest.TestLoader().loadTestsFromTestCase(TestTfIdf) |
---|
59 | unittest.TextTestRunner(verbosity=2).run(suite) |
---|