similarities.docsim
– Document similarity queries¶Compute similarities across a collection of documents in the Vector Space Model.
The main class is Similarity
, which builds an index for a given set of documents.
Once the index is built, you can perform efficient queries like “Tell me how similar is this query document to each document in the index?”. The result is a vector of numbers as large as the size of the initial set of documents, that is, one float for each index document. Alternatively, you can also request only the top-N most similar index documents to the query.
The Similarity
class splits the index into several smaller sub-indexes (“shards”),
which are disk-based. If your entire index fits in memory (~one million documents per 1GB of RAM),
you can also use the MatrixSimilarity
or SparseMatrixSimilarity
classes directly.
These are more simple but do not scale as well: they keep the entire index in RAM, no sharding. They also do not
support adding new document to the index dynamically.
Once the index has been initialized, you can query for document similarity simply by
>>> from gensim.test.utils import common_corpus, common_dictionary, get_tmpfile
>>>
>>> index_tmpfile = get_tmpfile("index")
>>> query = [(1, 2), (6, 1), (7, 2)]
>>>
>>> index = Similarity(index_tmpfile, common_corpus, num_features=len(common_dictionary)) # build the index
>>> similarities = index[query] # get similarities between the query and all index documents
If you have more query documents, you can submit them all at once, in a batch
>>> from gensim.test.utils import common_corpus, common_dictionary, get_tmpfile
>>>
>>> index_tmpfile = get_tmpfile("index")
>>> batch_of_documents = common_corpus[:] # only as example
>>> index = Similarity(index_tmpfile, common_corpus, num_features=len(common_dictionary)) # build the index
>>>
>>> # the batch is simply an iterable of documents, aka gensim corpus:
>>> for similarities in index[batch_of_documents]:
... pass
The benefit of this batch (aka “chunked”) querying is a much better performance.
To see the speed-up on your machine, run python -m gensim.test.simspeed
(compare to my results here).
There is also a special syntax for when you need similarity of documents in the index to the index itself (i.e. queries = the indexed documents themselves). This special syntax uses the faster, batch queries internally and is ideal for all-vs-all pairwise similarities:
>>> from gensim.test.utils import common_corpus, common_dictionary, get_tmpfile
>>>
>>> index_tmpfile = get_tmpfile("index")
>>> index = Similarity(index_tmpfile, common_corpus, num_features=len(common_dictionary)) # build the index
>>>
>>> for similarities in index: # yield similarities of the 1st indexed document, then 2nd...
... pass
gensim.similarities.docsim.
MatrixSimilarity
(corpus, num_best=None, dtype=<class 'numpy.float32'>, num_features=None, chunksize=256, corpus_len=None)¶Compute cosine similarity against a corpus of documents by storing the index matrix in memory.
Unless the entire matrix fits into main memory, use Similarity
instead.
Examples
>>> from gensim.test.utils import common_corpus, common_dictionary
>>> from gensim.similarities import MatrixSimilarity
>>>
>>> query = [(1, 2), (5, 4)]
>>> index = MatrixSimilarity(common_corpus, num_features=len(common_dictionary))
>>> sims = index[query]
corpus (iterable of list of (int, number)) – Corpus in streamed Gensim bag-of-words format.
num_best (int, optional) – If set, return only the num_best most similar documents, always leaving out documents with similarity = 0. Otherwise, return a full vector with one float for every document in the index.
num_features (int) – Size of the dictionary (number of features).
corpus_len (int, optional) – Number of documents in corpus. If not specified, will scan the corpus to determine the matrix size.
chunksize (int, optional) – Size of query chunks. Used internally when the query is an entire corpus.
dtype (numpy.dtype, optional) – Datatype to store the internal matrix in.
get_similarities
(query)¶Get similarity between query and this index.
Warning
Do not use this function directly, use the __getitem__
instead.
query ({list of (int, number), iterable of list of (int, number), scipy.sparse.csr_matrix
}) – Document or collection of documents.
Similarity matrix.
numpy.ndarray
load
(fname, mmap=None)¶Load an object previously saved using save()
from a file.
fname (str) – Path to file that contains needed object.
mmap (str, optional) – Memory-map option. If the object was saved with large arrays stored separately, you can load these arrays via mmap (shared memory) using mmap=’r’. If the file being loaded is compressed (either ‘.gz’ or ‘.bz2’), then `mmap=None must be set.
See also
save()
Save object to file.
Object loaded from fname.
object
AttributeError – When called on an object instance instead of class (this is a class method).
save
(fname_or_handle, separately=None, sep_limit=10485760, ignore=frozenset({}), pickle_protocol=2)¶Save the object to a file.
fname_or_handle (str or file-like) – Path to output file or already opened file-like object. If the object is a file handle, no special array handling will be performed, all attributes will be saved to the same file.
separately (list of str or None, optional) –
If None, automatically detect large numpy/scipy.sparse arrays in the object being stored, and store them into separate files. This prevent memory errors for large objects, and also allows memory-mapping the large arrays for efficient loading and sharing the large arrays in RAM between multiple processes.
If list of str: store these attributes into separate files. The automated size check is not performed in this case.
sep_limit (int, optional) – Don’t store arrays smaller than this separately. In bytes.
ignore (frozenset of str, optional) – Attributes that shouldn’t be stored at all.
pickle_protocol (int, optional) – Protocol number for pickle.
See also
load()
Load object from file.
gensim.similarities.docsim.
Shard
(fname, index)¶A proxy that represents a single shard instance within Similarity
index.
Basically just wraps MatrixSimilarity
,
SparseMatrixSimilarity
, etc, so that it mmaps from disk on request (query).
fname (str) – Path to top-level directory (file) to traverse for corpus documents.
index (SimilarityABC
) – Index object.
fullname
()¶Get full path to shard file.
Path to shard instance.
str
get_document_id
(pos)¶Get index vector at position pos.
pos (int) – Vector position.
Index vector. Type depends on underlying index.
{scipy.sparse.csr_matrix
, numpy.ndarray
}
Notes
The vector is of the same type as the underlying index (ie., dense for
MatrixSimilarity
and scipy.sparse for SparseMatrixSimilarity
.
get_index
()¶Load & get index.
Index instance.
load
(fname, mmap=None)¶Load an object previously saved using save()
from a file.
fname (str) – Path to file that contains needed object.
mmap (str, optional) – Memory-map option. If the object was saved with large arrays stored separately, you can load these arrays via mmap (shared memory) using mmap=’r’. If the file being loaded is compressed (either ‘.gz’ or ‘.bz2’), then `mmap=None must be set.
See also
save()
Save object to file.
Object loaded from fname.
object
AttributeError – When called on an object instance instead of class (this is a class method).
save
(fname_or_handle, separately=None, sep_limit=10485760, ignore=frozenset({}), pickle_protocol=2)¶Save the object to a file.
fname_or_handle (str or file-like) – Path to output file or already opened file-like object. If the object is a file handle, no special array handling will be performed, all attributes will be saved to the same file.
separately (list of str or None, optional) –
If None, automatically detect large numpy/scipy.sparse arrays in the object being stored, and store them into separate files. This prevent memory errors for large objects, and also allows memory-mapping the large arrays for efficient loading and sharing the large arrays in RAM between multiple processes.
If list of str: store these attributes into separate files. The automated size check is not performed in this case.
sep_limit (int, optional) – Don’t store arrays smaller than this separately. In bytes.
ignore (frozenset of str, optional) – Attributes that shouldn’t be stored at all.
pickle_protocol (int, optional) – Protocol number for pickle.
See also
load()
Load object from file.
gensim.similarities.docsim.
Similarity
(output_prefix, corpus, num_features, num_best=None, chunksize=256, shardsize=32768, norm='l2')¶Compute cosine similarity of a dynamic query against a corpus of documents (‘the index’).
The index supports adding new documents dynamically.
Notes
Scalability is achieved by sharding the index into smaller pieces, each of which fits into core memory The shards themselves are simply stored as files to disk and mmap’ed back as needed.
Examples
>>> from gensim.corpora.textcorpus import TextCorpus
>>> from gensim.test.utils import datapath, get_tmpfile
>>> from gensim.similarities import Similarity
>>>
>>> corpus = TextCorpus(datapath('testcorpus.mm'))
>>> index_temp = get_tmpfile("index")
>>> index = Similarity(index_temp, corpus, num_features=400) # create index
>>>
>>> query = next(iter(corpus))
>>> result = index[query] # search similar to `query` in index
>>>
>>> for sims in index[corpus]: # if you have more query documents, you can submit them all at once, in a batch
... pass
>>>
>>> # There is also a special syntax for when you need similarity of documents in the index
>>> # to the index itself (i.e. queries=indexed documents themselves). This special syntax
>>> # uses the faster, batch queries internally and **is ideal for all-vs-all pairwise similarities**:
>>> for similarities in index: # yield similarities of the 1st indexed document, then 2nd...
... pass
See also
MatrixSimilarity
Index similarity (dense with cosine distance).
SparseMatrixSimilarity
Index similarity (sparse with cosine distance).
WmdSimilarity
Index similarity (with word-mover distance).
output_prefix (str) – Prefix for shard filename. If None, a random filename in temp will be used.
corpus (iterable of list of (int, number)) – Corpus in streamed Gensim bag-of-words format.
num_features (int) – Size of the dictionary (number of features).
num_best (int, optional) – If set, return only the num_best most similar documents, always leaving out documents with similarity = 0. Otherwise, return a full vector with one float for every document in the index.
chunksize (int, optional) – Size of query chunks. Used internally when the query is an entire corpus.
shardsize (int, optional) – Maximum shard size, in documents. Choose a value so that a shardsize x chunksize matrix of floats fits comfortably into your RAM.
norm ({'l1', 'l2'}, optional) – Normalization to use.
Notes
Documents are split (internally, transparently) into shards of shardsize documents each, and each shard converted to a matrix, for faster BLAS calls. Each shard is stored to disk under output_prefix.shard_number.
If you don’t specify an output prefix, a random filename in temp will be used.
If your entire index fits in memory (~1 million documents per 1GB of RAM), you can also use the
MatrixSimilarity
or
SparseMatrixSimilarity
classes directly.
These are more simple but do not scale as well (they keep the entire index in RAM, no sharding).
They also do not support adding new document dynamically.
add_documents
(corpus)¶Extend the index with new documents.
corpus (iterable of list of (int, number)) – Corpus in BoW format.
Notes
Internally, documents are buffered and then spilled to disk when there’s self.shardsize of them (or when a query is issued).
Examples
>>> from gensim.corpora.textcorpus import TextCorpus
>>> from gensim.test.utils import datapath, get_tmpfile
>>> from gensim.similarities import Similarity
>>>
>>> corpus = TextCorpus(datapath('testcorpus.mm'))
>>> index_temp = get_tmpfile("index")
>>> index = Similarity(index_temp, corpus, num_features=400) # create index
>>>
>>> one_more_corpus = TextCorpus(datapath('testcorpus.txt'))
>>> index.add_documents(one_more_corpus) # add more documents in corpus
check_moved
()¶Update shard locations, for case where the server prefix location changed on the filesystem.
close_shard
()¶Do nothing if no new documents added since last call.
Notes
The shard is closed even if it is not full yet (its size is smaller than self.shardsize).
If documents are added later via add_documents()
this incomplete shard will be loaded again and completed.
destroy
()¶Delete all files under self.output_prefix Index is not usable anymore after calling this method.
get_similarities
(doc)¶Get similarities of the given document or corpus against this index.
doc ({list of (int, number), iterable of list of (int, number)}) – Document in the sparse Gensim bag-of-words format, or a streamed corpus of such documents.
iter_chunks
(chunksize=None)¶Iteratively yield the index as chunks of document vectors, each of size <= chunksize.
chunksize (int, optional) – Size of chunk,, if None - self.chunksize will be used.
numpy.ndarray
or scipy.sparse.csr_matrix
– Chunks of the index as 2D arrays. The arrays are either dense or sparse, depending on
whether the shard was storing dense or sparse vectors.
load
(fname, mmap=None)¶Load an object previously saved using save()
from a file.
fname (str) – Path to file that contains needed object.
mmap (str, optional) – Memory-map option. If the object was saved with large arrays stored separately, you can load these arrays via mmap (shared memory) using mmap=’r’. If the file being loaded is compressed (either ‘.gz’ or ‘.bz2’), then `mmap=None must be set.
See also
save()
Save object to file.
Object loaded from fname.
object
AttributeError – When called on an object instance instead of class (this is a class method).
query_shards
(query)¶Apply shard[query] to each shard in self.shards. Used internally.
query ({iterable of list of (int, number) , list of (int, number))}) – Document in BoW format or corpus of documents.
Query results.
(None, list of individual shard query results)
reopen_shard
()¶Reopen an incomplete shard.
save
(fname=None, *args, **kwargs)¶Save the index object via pickling under fname. See also load()
.
fname (str, optional) – Path for save index, if not provided - will be saved to self.output_prefix.
*args (object) – Arguments, see gensim.utils.SaveLoad.save()
.
**kwargs (object) – Keyword arguments, see gensim.utils.SaveLoad.save()
.
Notes
Will call close_shard()
internally to spill
any unfinished shards to disk first.
Examples
>>> from gensim.corpora.textcorpus import TextCorpus
>>> from gensim.test.utils import datapath, get_tmpfile
>>> from gensim.similarities import Similarity
>>>
>>> temp_fname = get_tmpfile("index")
>>> output_fname = get_tmpfile("saved_index")
>>>
>>> corpus = TextCorpus(datapath('testcorpus.txt'))
>>> index = Similarity(output_fname, corpus, num_features=400)
>>>
>>> index.save(output_fname)
>>> loaded_index = index.load(output_fname)
shardid2filename
(shardid)¶Get shard file by shardid.
shardid (int) – Shard index.
Path to shard file.
str
similarity_by_id
(docpos)¶Get similarity of a document specified by its index position docpos.
docpos (int) – Document position in the index.
Similarities of the given document against this index.
numpy.ndarray
or scipy.sparse.csr_matrix
Examples
>>> from gensim.corpora.textcorpus import TextCorpus
>>> from gensim.test.utils import datapath
>>> from gensim.similarities import Similarity
>>>
>>> corpus = TextCorpus(datapath('testcorpus.txt'))
>>> index = Similarity('temp', corpus, num_features=400)
>>> similarities = index.similarity_by_id(1)
vector_by_id
(docpos)¶Get the indexed vector corresponding to the document at position docpos.
docpos (int) – Document position
Indexed vector.
scipy.sparse.csr_matrix
Examples
>>> from gensim.corpora.textcorpus import TextCorpus
>>> from gensim.test.utils import datapath
>>> from gensim.similarities import Similarity
>>>
>>> # Create index:
>>> corpus = TextCorpus(datapath('testcorpus.txt'))
>>> index = Similarity('temp', corpus, num_features=400)
>>> vector = index.vector_by_id(1)
gensim.similarities.docsim.
SoftCosineSimilarity
(corpus, similarity_matrix, num_best=None, chunksize=256)¶Compute soft cosine similarity against a corpus of documents by storing the index matrix in memory.
Examples
>>> from gensim.test.utils import common_texts
>>> from gensim.corpora import Dictionary
>>> from gensim.models import Word2Vec, WordEmbeddingSimilarityIndex
>>> from gensim.similarities import SoftCosineSimilarity, SparseTermSimilarityMatrix
>>>
>>> model = Word2Vec(common_texts, size=20, min_count=1) # train word-vectors
>>> termsim_index = WordEmbeddingSimilarityIndex(model.wv)
>>> dictionary = Dictionary(common_texts)
>>> bow_corpus = [dictionary.doc2bow(document) for document in common_texts]
>>> similarity_matrix = SparseTermSimilarityMatrix(termsim_index, dictionary) # construct similarity matrix
>>> docsim_index = SoftCosineSimilarity(bow_corpus, similarity_matrix, num_best=10)
>>>
>>> query = 'graph trees computer'.split() # make a query
>>> sims = docsim_index[dictionary.doc2bow(query)] # calculate similarity of query to each doc from bow_corpus
Check out Tutorial Notebook for more examples.
corpus (iterable of list of (int, float)) – A list of documents in the BoW format.
similarity_matrix (gensim.similarities.SparseTermSimilarityMatrix
) – A term similarity matrix.
num_best (int, optional) – The number of results to retrieve for a query, if None - return similarities with all elements from corpus.
chunksize (int, optional) – Size of one corpus chunk.
See also
gensim.similarities.SparseTermSimilarityMatrix
A sparse term similarity matrix build using a term similarity index.
gensim.similarities.LevenshteinSimilarityIndex
A term similarity index that computes Levenshtein similarities between terms.
gensim.models.WordEmbeddingSimilarityIndex
A term similarity index that computes cosine similarities between word embeddings.
get_similarities
(query)¶Get similarity between query and this index.
Warning
Do not use this function directly; use the self[query] syntax instead.
query ({list of (int, number), iterable of list of (int, number)}) – Document or collection of documents.
Similarity matrix.
numpy.ndarray
load
(fname, mmap=None)¶Load an object previously saved using save()
from a file.
fname (str) – Path to file that contains needed object.
mmap (str, optional) – Memory-map option. If the object was saved with large arrays stored separately, you can load these arrays via mmap (shared memory) using mmap=’r’. If the file being loaded is compressed (either ‘.gz’ or ‘.bz2’), then `mmap=None must be set.
See also
save()
Save object to file.
Object loaded from fname.
object
AttributeError – When called on an object instance instead of class (this is a class method).
save
(fname_or_handle, separately=None, sep_limit=10485760, ignore=frozenset({}), pickle_protocol=2)¶Save the object to a file.
fname_or_handle (str or file-like) – Path to output file or already opened file-like object. If the object is a file handle, no special array handling will be performed, all attributes will be saved to the same file.
separately (list of str or None, optional) –
If None, automatically detect large numpy/scipy.sparse arrays in the object being stored, and store them into separate files. This prevent memory errors for large objects, and also allows memory-mapping the large arrays for efficient loading and sharing the large arrays in RAM between multiple processes.
If list of str: store these attributes into separate files. The automated size check is not performed in this case.
sep_limit (int, optional) – Don’t store arrays smaller than this separately. In bytes.
ignore (frozenset of str, optional) – Attributes that shouldn’t be stored at all.
pickle_protocol (int, optional) – Protocol number for pickle.
See also
load()
Load object from file.
gensim.similarities.docsim.
SparseMatrixSimilarity
(corpus, num_features=None, num_terms=None, num_docs=None, num_nnz=None, num_best=None, chunksize=500, dtype=<class 'numpy.float32'>, maintain_sparsity=False)¶Compute cosine similarity against a corpus of documents by storing the index matrix in memory.
Notes
Use this if your input corpus contains sparse vectors (such as TF-IDF documents) and fits into RAM.
The matrix is internally stored as a scipy.sparse.csr_matrix
matrix. Unless the entire
matrix fits into main memory, use Similarity
instead.
Takes an optional maintain_sparsity argument, setting this to True causes get_similarities to return a sparse matrix instead of a dense representation if possible.
See also
Similarity
Index similarity (wrapper for other inheritors of SimilarityABC
).
MatrixSimilarity
Index similarity (dense with cosine distance).
corpus (iterable of list of (int, float)) – A list of documents in the BoW format.
num_features (int, optional) – Size of the dictionary. Must be either specified, or present in corpus.num_terms.
num_terms (int, optional) – Alias for num_features, you can use either.
num_docs (int, optional) – Number of documents in corpus. Will be calculated if not provided.
num_nnz (int, optional) – Number of non-zero elements in corpus. Will be calculated if not provided.
num_best (int, optional) – If set, return only the num_best most similar documents, always leaving out documents with similarity = 0. Otherwise, return a full vector with one float for every document in the index.
chunksize (int, optional) – Size of query chunks. Used internally when the query is an entire corpus.
dtype (numpy.dtype, optional) – Data type of the internal matrix.
maintain_sparsity (bool, optional) – Return sparse arrays from get_similarities()
?
get_similarities
(query)¶Get similarity between query and this index.
Warning
Do not use this function directly; use the self[query] syntax instead.
query ({list of (int, number), iterable of list of (int, number), scipy.sparse.csr_matrix
}) – Document or collection of documents.
numpy.ndarray
– Similarity matrix (if maintain_sparsity=False) OR
scipy.sparse.csc
– otherwise
load
(fname, mmap=None)¶Load an object previously saved using save()
from a file.
fname (str) – Path to file that contains needed object.
mmap (str, optional) – Memory-map option. If the object was saved with large arrays stored separately, you can load these arrays via mmap (shared memory) using mmap=’r’. If the file being loaded is compressed (either ‘.gz’ or ‘.bz2’), then `mmap=None must be set.
See also
save()
Save object to file.
Object loaded from fname.
object
AttributeError – When called on an object instance instead of class (this is a class method).
save
(fname_or_handle, separately=None, sep_limit=10485760, ignore=frozenset({}), pickle_protocol=2)¶Save the object to a file.
fname_or_handle (str or file-like) – Path to output file or already opened file-like object. If the object is a file handle, no special array handling will be performed, all attributes will be saved to the same file.
separately (list of str or None, optional) –
If None, automatically detect large numpy/scipy.sparse arrays in the object being stored, and store them into separate files. This prevent memory errors for large objects, and also allows memory-mapping the large arrays for efficient loading and sharing the large arrays in RAM between multiple processes.
If list of str: store these attributes into separate files. The automated size check is not performed in this case.
sep_limit (int, optional) – Don’t store arrays smaller than this separately. In bytes.
ignore (frozenset of str, optional) – Attributes that shouldn’t be stored at all.
pickle_protocol (int, optional) – Protocol number for pickle.
See also
load()
Load object from file.
gensim.similarities.docsim.
WmdSimilarity
(corpus, w2v_model, num_best=None, normalize_w2v_and_replace=True, chunksize=256)¶Compute negative WMD similarity against a corpus of documents.
See WordEmbeddingsKeyedVectors
for more information.
Also, tutorial notebook for more examples.
When using this code, please consider citing the following papers:
Ofir Pele and Michael Werman, “A linear time histogram metric for improved SIFT matching”
Ofir Pele and Michael Werman, “Fast and robust earth mover’s distances”
Matt Kusner et al. “From Word Embeddings To Document Distances”
Example
>>> from gensim.test.utils import common_texts
>>> from gensim.models import Word2Vec
>>> from gensim.similarities import WmdSimilarity
>>>
>>> model = Word2Vec(common_texts, size=20, min_count=1) # train word-vectors
>>>
>>> index = WmdSimilarity(common_texts, model)
>>> # Make query.
>>> query = ['trees']
>>> sims = index[query]
corpus (iterable of list of str) – A list of documents, each of which is a list of tokens.
w2v_model (Word2VecTrainables
) – A trained word2vec model.
num_best (int, optional) – Number of results to retrieve.
normalize_w2v_and_replace (bool, optional) – Whether or not to normalize the word2vec vectors to length 1.
chunksize (int, optional) – Size of chunk.
get_similarities
(query)¶Get similarity between query and this index.
Warning
Do not use this function directly; use the self[query] syntax instead.
query ({list of str, iterable of list of str}) – Document or collection of documents.
Similarity matrix.
numpy.ndarray
load
(fname, mmap=None)¶Load an object previously saved using save()
from a file.
fname (str) – Path to file that contains needed object.
mmap (str, optional) – Memory-map option. If the object was saved with large arrays stored separately, you can load these arrays via mmap (shared memory) using mmap=’r’. If the file being loaded is compressed (either ‘.gz’ or ‘.bz2’), then `mmap=None must be set.
See also
save()
Save object to file.
Object loaded from fname.
object
AttributeError – When called on an object instance instead of class (this is a class method).
save
(fname_or_handle, separately=None, sep_limit=10485760, ignore=frozenset({}), pickle_protocol=2)¶Save the object to a file.
fname_or_handle (str or file-like) – Path to output file or already opened file-like object. If the object is a file handle, no special array handling will be performed, all attributes will be saved to the same file.
separately (list of str or None, optional) –
If None, automatically detect large numpy/scipy.sparse arrays in the object being stored, and store them into separate files. This prevent memory errors for large objects, and also allows memory-mapping the large arrays for efficient loading and sharing the large arrays in RAM between multiple processes.
If list of str: store these attributes into separate files. The automated size check is not performed in this case.
sep_limit (int, optional) – Don’t store arrays smaller than this separately. In bytes.
ignore (frozenset of str, optional) – Attributes that shouldn’t be stored at all.
pickle_protocol (int, optional) – Protocol number for pickle.
See also
load()
Load object from file.
gensim.similarities.docsim.
query_shard
(args)¶Helper for request query from shard, same as shard[query].
args ((list of (int, number), SimilarityABC
)) – Query and Shard instances
Similarities of the query against documents indexed in this shard.
numpy.ndarray
or scipy.sparse.csr_matrix