Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed FastTextKeyedVectors handling in add_vector #3389

Merged
merged 1 commit into from
Dec 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion gensim/models/keyedvectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ def add_vectors(self, keys, weights, extras=None, replace=False):

in_vocab_mask = np.zeros(len(keys), dtype=bool)
for idx, key in enumerate(keys):
if key in self:
if key in self.key_to_index:
in_vocab_mask[idx] = True

# add new entities to the vocab
Expand Down
22 changes: 22 additions & 0 deletions gensim/test/test_fasttext.py
Original file line number Diff line number Diff line change
Expand Up @@ -1782,6 +1782,28 @@ def test_identity(self):
self.assertTrue(np.all(np.array([6, 7, 8]) == n[2]))


class FastTextKeyedVectorsTest(unittest.TestCase):
def test_add_vector(self):
wv = FastTextKeyedVectors(vector_size=2, min_n=3, max_n=6, bucket=2000000)
wv.add_vector("test_key", np.array([0, 0]))

self.assertEqual(wv.key_to_index["test_key"], 0)
self.assertEqual(wv.index_to_key[0], "test_key")
self.assertTrue(np.all(wv.vectors[0] == np.array([0, 0])))

def test_add_vectors(self):
wv = FastTextKeyedVectors(vector_size=2, min_n=3, max_n=6, bucket=2000000)
wv.add_vectors(["test_key1", "test_key2"], np.array([[0, 0], [1, 1]]))

self.assertEqual(wv.key_to_index["test_key1"], 0)
self.assertEqual(wv.index_to_key[0], "test_key1")
self.assertTrue(np.all(wv.vectors[0] == np.array([0, 0])))

self.assertEqual(wv.key_to_index["test_key2"], 1)
self.assertEqual(wv.index_to_key[1], "test_key2")
self.assertTrue(np.all(wv.vectors[1] == np.array([1, 1])))


if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s : %(levelname)s : %(message)s', level=logging.DEBUG)
unittest.main()