Esempio n. 1
0
    def test_tfidf_params(self):
        corpus = ['a b c', 'b c d d', 'c b e c f']
        ids = ['u1', 'u2', 'u3']

        modality = TextModality(corpus=corpus,
                                ids=ids,
                                max_vocab=6,
                                tfidf_params={
                                    'binary': False,
                                    'norm': 'l2',
                                    'use_idf': True,
                                    'smooth_idf': True,
                                    'sublinear_tf': False
                                }).build({
                                    'u1': 0,
                                    'u2': 1,
                                    'u3': 2
                                })
        npt.assert_array_equal(modality.batch_tfidf([1]),
                               self.modality.batch_tfidf([1]))

        for k, v in {
                'binary': True,
                'norm': 'l1',
                'use_idf': False,
                'smooth_idf': False,
                'sublinear_tf': True
        }.items():
            modality = TextModality(corpus=corpus,
                                    ids=ids,
                                    max_vocab=6,
                                    tfidf_params={k: v})
            modality.build({'u1': 0, 'u2': 1, 'u3': 2})
            self.assertFalse(
                np.array_equal(modality.batch_tfidf([1]),
                               self.modality.batch_tfidf([1])))
Esempio n. 2
0
 def test_batch_bow_fallback(self):
     modality = TextModality(features=np.asarray([[3, 2, 1], [4, 5, 6]]),
                             ids=['a', 'b'])
     modality.build()
     npt.assert_array_equal(np.asarray([[3, 2, 1]]),
                            modality.batch_bow(batch_ids=[0]))
Esempio n. 3
0
class TestTextModality(unittest.TestCase):
    def setUp(self):
        self.tokens = ['a', 'b', 'c', 'd', 'e', 'f']
        corpus = ['a b c', 'b c d d', 'c b e c f']
        ids = ['u1', 'u2', 'u3']
        # frequency ranking: c > b > d > a > e > f
        self.modality = TextModality(corpus=corpus, ids=ids, max_vocab=6)
        self.modality.build({'u1': 0, 'u2': 1, 'u3': 2})
        self.token_ids = (self.modality.vocab.tok2idx[tok]
                          for tok in self.tokens)

    def test_init(self):
        self.assertCountEqual(self.modality.vocab.idx2tok,
                              SPECIAL_TOKENS + self.tokens)

    def test_build(self):
        TextModality().build()
        TextModality(corpus=['abc']).build()
        TextModality(corpus=['abc']).build({'b': 0})
        TextModality(corpus=['abc'], ids=['a']).build({'b': 0})

    def test_sequences(self):
        (a, b, c, d, e, f) = self.token_ids

        self.assertListEqual(self.modality.sequences,
                             [[a, b, c], [b, c, d, d], [c, b, e, c, f]])

    def test_batch_seq(self):
        (a, b, c, d, e, f) = self.token_ids

        batch_seqs = self.modality.batch_seq([2, 1])
        self.assertEqual((2, 5), batch_seqs.shape)
        npt.assert_array_equal(batch_seqs,
                               np.asarray([[c, b, e, c, f], [b, c, d, d, 0]]))

        batch_seqs = self.modality.batch_seq([0, 2], max_length=4)
        self.assertEqual((2, 4), batch_seqs.shape)
        npt.assert_array_equal(batch_seqs,
                               np.asarray([[a, b, c, 0], [c, b, e, c]]))

        self.modality.sequences = None
        try:
            self.modality.batch_seq([0])
        except ValueError:
            assert True

    def test_count_matrix(self):
        (a, b, c, d, e, f) = self.token_ids
        shift = len(SPECIAL_TOKENS)
        expected_counts = np.zeros_like(self.modality.count_matrix.A)
        expected_counts[0, a - shift] = 1
        expected_counts[0, b - shift] = 1
        expected_counts[0, c - shift] = 1
        expected_counts[1, b - shift] = 1
        expected_counts[1, c - shift] = 1
        expected_counts[1, d - shift] = 2
        expected_counts[2, b - shift] = 1
        expected_counts[2, c - shift] = 2
        expected_counts[2, e - shift] = 1
        expected_counts[2, f - shift] = 1
        npt.assert_array_equal(self.modality.count_matrix.A, expected_counts)

    def test_batch_bow(self):
        (a, b, c, d, e, f) = self.token_ids
        shift = len(SPECIAL_TOKENS)

        batch_bows = self.modality.batch_bow([2, 1])
        self.assertEqual((2, self.modality.max_vocab), batch_bows.shape)
        expected_bows = np.zeros_like(batch_bows)
        expected_bows[0, b - shift] = 1
        expected_bows[0, c - shift] = 2
        expected_bows[0, e - shift] = 1
        expected_bows[0, f - shift] = 1
        expected_bows[1, b - shift] = 1
        expected_bows[1, c - shift] = 1
        expected_bows[1, d - shift] = 2
        npt.assert_array_equal(batch_bows, expected_bows)

        batch_bows = self.modality.batch_bow([0, 2],
                                             binary=True,
                                             keep_sparse=True)
        self.assertEqual((2, 6), batch_bows.shape)
        expected_bows = np.zeros_like(batch_bows.A)
        expected_bows[0, np.asarray([a, b, c]) - shift] = 1
        expected_bows[1, np.asarray([b, c, e, f]) - shift] = 1
        npt.assert_array_equal(batch_bows.A, expected_bows)

        self.modality.count_matrix = None
        try:
            self.modality.batch_bow([0])
        except ValueError:
            assert True

    def test_batch_bow_fallback(self):
        modality = TextModality(features=np.asarray([[3, 2, 1], [4, 5, 6]]),
                                ids=['a', 'b'])
        modality.build()
        npt.assert_array_equal(np.asarray([[3, 2, 1]]),
                               modality.batch_bow(batch_ids=[0]))