Example #1
0
 def test_analogy(self):
     from wordvector import WordVector
     dictionary = {
         'the': 0,
         'quick': 1,
         'brown': 2,
         'fox': 3,
         'jumped': 4,
         'over': 5
     }
     embed_matrix = np.array([[1.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                              [1.0, 0.5, 0.1, 0.1, 0.1, 0.1, 0.1],
                              [-1.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1],
                              [1.0, 0.1, 0.1, 1.1, 1.1, 1.1, 0.1],
                              [1.0, 0.6, 0.1, 1.1, 1.1, 1.1, 0.1],
                              [1.0, 0.7, 0.1, 1.1, 1.1, 1.1, 0.1]])
     word_embedding = WordVector(embed_matrix, dictionary)
     d = word_embedding.analogy('the',
                                'fox',
                                'quick',
                                num=2,
                                metric='euclidean')
     self.assertEqual(2, len(d), 'wrong number of analogies returned')
     self.assertEqual('jumped', d[0], 'wrong most likely analogy returned')
     self.assertEqual('over', d[1],
                      'wrong 2nd most likely analogy returned')