Previous topic

corpora.lowcorpus – Corpus in List-of-Words format

Next topic

corpora.svmlightcorpus – Corpus in SVMlight format

corpora.mmcorpus – Corpus in Matrix Market format

Corpus in the Matrix Market format.

class gensim.corpora.mmcorpus.MmCorpus(fname)

Corpus in the Matrix Market format.

docbyoffset(offset)

Return document at file offset offset (in bytes)

classmethod load(fname)

Load a previously saved object from file (also see save).

save(fname)

Save the object to file via pickling (also see load).

static save_corpus(fname, corpus, id2word=None, progress_cnt=1000)

Save a corpus in the Matrix Market format to disk.

This function is automatically called by MmCorpus.serialize; don’t call it directly, call serialize instead.

classmethod serialize(serializer, fname, corpus, id2word=None, index_fname=None, progress_cnt=None, labels=None)

Iterate through the document stream corpus, saving the documents to fname and recording byte offset of each document. Save the resulting index structure to file index_fname (or fname.index is not set).

This relies on the underlying corpus class serializer providing (in addition to standard iteration):

  • save_corpus method that returns a sequence of byte offsets, one for

    each saved document,

  • the docbyoffset(offset) method, which returns a document positioned at offset bytes within the persistent storage (file).

Example:

>>> MmCorpus.serialize('test.mm', corpus)
>>> mm = MmCorpus('test.mm') # `mm` document stream now has random access
>>> print mm[42] # retrieve document no. 42, etc.
skip_headers(input_file)

Skip file headers that appear before the first document.