|
| 1 | +""" |
| 2 | +LMDB Embeddings - Fast word vectors with little memory usage in Python. |
| 3 | + |
| 4 | +
|
| 5 | +Copyright (C) 2018 ThoughtRiver Limited |
| 6 | +
|
| 7 | +This program is free software: you can redistribute it and/or modify |
| 8 | +it under the terms of the GNU General Public License as published by |
| 9 | +the Free Software Foundation, either version 3 of the License, or |
| 10 | +(at your option) any later version. |
| 11 | +
|
| 12 | +This program is distributed in the hope that it will be useful, |
| 13 | +but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | +GNU General Public License for more details. |
| 16 | +
|
| 17 | +You should have received a copy of the GNU General Public License |
| 18 | +along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +import os |
| 23 | + |
| 24 | +import numpy as np |
| 25 | +import pytest |
| 26 | + |
| 27 | +from lmdb_embeddings import exceptions |
| 28 | +from lmdb_embeddings.reader import LmdbEmbeddingsReader |
| 29 | +from lmdb_embeddings.reader import LruCachedLmdbEmbeddingsReader |
| 30 | +from lmdb_embeddings.serializers import MsgpackSerializer |
| 31 | +from lmdb_embeddings.writer import LmdbEmbeddingsWriter |
| 32 | + |
| 33 | + |
| 34 | +class TestEmbeddings: |
| 35 | + |
| 36 | + def test_write_embeddings(self, tmp_path): |
| 37 | + """ Ensure we can write embeddings to disk without error. |
| 38 | +
|
| 39 | + :param pathlib.PosixPath tmp_path: |
| 40 | + :return void: |
| 41 | + """ |
| 42 | + directory_path = str(tmp_path) |
| 43 | + |
| 44 | + LmdbEmbeddingsWriter([ |
| 45 | + ('the', np.random.rand(10)), |
| 46 | + ('is', np.random.rand(10)) |
| 47 | + ]).write(directory_path) |
| 48 | + |
| 49 | + assert os.listdir(directory_path) |
| 50 | + |
| 51 | + def test_write_embeddings_generator(self, tmp_path): |
| 52 | + """ Ensure we can a generator of embeddings to disk without error. |
| 53 | +
|
| 54 | + :param pathlib.PosixPath tmp_path: |
| 55 | + :return void: |
| 56 | + """ |
| 57 | + directory_path = str(tmp_path) |
| 58 | + embeddings_generator = ((str(i), np.random.rand(10)) for i in range(10)) |
| 59 | + |
| 60 | + LmdbEmbeddingsWriter(embeddings_generator).write(directory_path) |
| 61 | + |
| 62 | + assert os.listdir(directory_path) |
| 63 | + |
| 64 | + @pytest.mark.parametrize('reader_class', (LruCachedLmdbEmbeddingsReader, LmdbEmbeddingsReader)) |
| 65 | + def test_reading_embeddings(self, tmp_path, reader_class): |
| 66 | + """ Ensure we can retrieve embeddings from the database. |
| 67 | +
|
| 68 | + :param pathlib.PosixPath tmp_path: |
| 69 | + :return void: |
| 70 | + """ |
| 71 | + directory_path = str(tmp_path) |
| 72 | + |
| 73 | + the_vector = np.random.rand(10) |
| 74 | + LmdbEmbeddingsWriter([ |
| 75 | + ('the', the_vector), |
| 76 | + ('is', np.random.rand(10)) |
| 77 | + ]).write(directory_path) |
| 78 | + |
| 79 | + assert reader_class(directory_path).get_word_vector('the').tolist() == the_vector.tolist() |
| 80 | + |
| 81 | + @pytest.mark.parametrize('reader_class', (LruCachedLmdbEmbeddingsReader, LmdbEmbeddingsReader)) |
| 82 | + def test_missing_word_error(self, tmp_path, reader_class): |
| 83 | + """ Ensure a MissingWordError exception is raised if the word does not exist in the |
| 84 | + database. |
| 85 | +
|
| 86 | + :param pathlib.PosixPath tmp_path: |
| 87 | + :return void: |
| 88 | + """ |
| 89 | + directory_path = str(tmp_path) |
| 90 | + |
| 91 | + LmdbEmbeddingsWriter([ |
| 92 | + ('the', np.random.rand(10)), |
| 93 | + ('is', np.random.rand(10)) |
| 94 | + ]).write(directory_path) |
| 95 | + |
| 96 | + reader = reader_class(directory_path) |
| 97 | + |
| 98 | + with pytest.raises(exceptions.MissingWordError): |
| 99 | + reader.get_word_vector('unknown') |
| 100 | + |
| 101 | + def test_word_too_long(self, tmp_path): |
| 102 | + """ Ensure we do not get an exception if attempting to write aword longer than LMDB's |
| 103 | + maximum key size. |
| 104 | +
|
| 105 | + :param pathlib.PosixPath tmp_path: |
| 106 | + :return void: |
| 107 | + """ |
| 108 | + directory_path = str(tmp_path) |
| 109 | + |
| 110 | + LmdbEmbeddingsWriter([('a' * 1000, np.random.rand(10))]).write(directory_path) |
| 111 | + |
| 112 | + @pytest.mark.parametrize('reader_class', (LruCachedLmdbEmbeddingsReader, LmdbEmbeddingsReader)) |
| 113 | + def test_msgpack_serialization(self, tmp_path, reader_class): |
| 114 | + """ Ensure we can save and retrieve embeddings serialized with msgpack. |
| 115 | +
|
| 116 | + :param pathlib.PosixPath tmp_path: |
| 117 | + :return void: |
| 118 | + """ |
| 119 | + directory_path = str(tmp_path) |
| 120 | + the_vector = np.random.rand(10) |
| 121 | + |
| 122 | + LmdbEmbeddingsWriter( |
| 123 | + [('the', the_vector), ('is', np.random.rand(10))], |
| 124 | + serializer = MsgpackSerializer.serialize |
| 125 | + ).write(directory_path) |
| 126 | + |
| 127 | + reader = reader_class(directory_path, unserializer = MsgpackSerializer.unserialize) |
| 128 | + assert reader.get_word_vector('the').tolist() == the_vector.tolist() |
0 commit comments