Example #1
0
 def test_get_vector_by_name(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.05, 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.1, 0.1, 0.9, 0.9, 0.9, 0.1],
                              [1.0, 0.1, 0.1, 1.0, 1.0, 0.8, 0.1]])
     word_embedding = WordVector(embed_matrix, dictionary)
     self.assertTrue(
         np.sum(
             np.abs(
                 np.array([1.0, 0.1, 0.1, 1.1, 1.1, 1.1, 0.1]) -
                 word_embedding.get_vector_by_name('fox'))) < 0.1,
         'incorrest closest indices')
     self.assertTrue(
         np.sum(
             np.abs(
                 np.array([1.0, 0.1, 0.1, 1.0, 1.0, 0.8, 0.1]) -
                 word_embedding.get_vector_by_name('over'))) < 0.1,
         'incorrest closest indices')
     self.assertTrue(
         np.sum(
             np.abs(
                 np.array([1.0, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1]) -
                 word_embedding.get_vector_by_name('the'))) < 0.1,
         'incorrest closest indices')