gensim.corpora.Dictionary.merge_with

Dictionary.merge_with(other)[source]

Merge another dictionary into this dictionary, mapping same tokens to the same ids and new tokens to new ids. The purpose is to merge two corpora created using two different dictionaries, one from self and one from other.

other can be any id=>word mapping (a dict, a Dictionary object, ...).

Return a transformation object which, when accessed as result[doc_from_other_corpus], will convert documents from a corpus built using the other dictionary into a document using the new, merged dictionary (see gensim.interfaces.TransformationABC).

Example:

>>> dict1 = Dictionary(some_documents)
>>> dict2 = Dictionary(other_documents)  # ids not compatible with dict1!
>>> dict2_to_dict1 = dict1.merge_with(dict2)
>>> # now we can merge corpora from the two incompatible dictionaries into one
>>> merged_corpus = itertools.chain(some_corpus_from_dict1, dict2_to_dict1[some_corpus_from_dict2])