class_docstr: Store a corpus in `Matrix Market format <https://math.nist.gov/MatrixMarket/formats.html>`_,
  using :class:`~gensim.corpora.mmcorpus.MmCorpus`.
  
  Notes
  -----
  The output is written one document at a time, not the whole matrix at once (unlike e.g. `scipy.io.mmread`).
  This allows you to write corpora which are larger than the available RAM.
  
  The output file is created in a single pass through the input corpus, so that the input can be
  a once-only stream (generator).
  
  To achieve this, a fake MM header is written first, corpus statistics are collected
  during the pass (shape of the matrix, number of non-zeroes), followed by a seek back to the beginning of the file,
  rewriting the fake header with the final values.
class_docstr_tok: ["Store","a","corpus","in","`","Matrix","Market","format","<","https",":","\/\/math.nist.gov\/MatrixMarket\/formats.html",">","`","_",",","using",":","class",":","`","~gensim.corpora.mmcorpus.MmCorpus","`",".","Notes","--","--","-","The","output","is","written","one","document","at","a","time",",","not","the","whole","matrix","at","once","(","unlike","e.g",".","`","scipy.io.mmread","`",")",".","This","allows","you","to","write","corpora","which","are","larger","than","the","available","RAM",".","The","output","file","is","created","in","a","single","pass","through","the","input","corpus",",","so","that","the","input","can","be","a","once-only","stream","(","generator",")",".","To","achieve","this",",","a","fake","MM","header","is","written","first",",","corpus","statistics","are","collected","during","the","pass","(","shape","of","the","matrix",",","number","of","non-zeroes",")",",","followed","by","a","seek","back","to","the","beginning","of","the","file",",","rewriting","the","fake","header","with","the","final","values","."]
docstr: Save the corpus to disk in `Matrix Market format <https://math.nist.gov/MatrixMarket/formats.html>`_.
  
  Parameters
  ----------
  fname : str
      Filename of the resulting file.
  corpus : iterable of list of (int, number)
      Corpus in streamed bag-of-words format.
  progress_cnt : int, optional
      Print progress for every `progress_cnt` number of documents.
  index : bool, optional
      Return offsets?
  num_terms : int, optional
      Number of terms in the corpus. If provided, the `corpus.num_terms` attribute (if any) will be ignored.
  metadata : bool, optional
      Generate a metadata file?
  
  Returns
  -------
  offsets : {list of int, None}
      List of offsets (if index=True) or nothing.
  
  Notes
  -----
  Documents are processed one at a time, so the whole corpus is allowed to be larger than the available RAM.
  
  See Also
  --------
  :func:`gensim.corpora.mmcorpus.MmCorpus.save_corpus`
      Save corpus to disk.
docstr_tok: ["Save","the","corpus","to","disk","in","`","Matrix","Market","format","<","https",":","\/\/math.nist.gov\/MatrixMarket\/formats.html",">","`","_",".","Parameters","--","--","--","--","--","fname",":","str","Filename","of","the","resulting","file",".","corpus",":","iterable","of","list","of","(","int",",","number",")","Corpus","in","streamed","bag-of-words","format",".","progress_cnt",":","int",",","optional","Print","progress","for","every","`","progress_cnt","`","number","of","documents",".","index",":","bool",",","optional","Return","offsets","?","num_terms",":","int",",","optional","Number","of","terms","in","the","corpus",".","If","provided",",","the","`","corpus.num_terms","`","attribute","(","if","any",")","will","be","ignored",".","metadata",":","bool",",","optional","Generate","a","metadata","file","?","Returns","--","--","--","-","offsets",":","{","list","of","int",",","None","}","List","of","offsets","(","if","index=True",")","or","nothing",".","Notes","--","--","-","Documents","are","processed","one","at","a","time",",","so","the","whole","corpus","is","allowed","to","be","larger","than","the","available","RAM",".","See","Also","--","--","--","--",":","func",":","`","gensim.corpora.mmcorpus.MmCorpus.save_corpus","`","Save","corpus","to","disk","."]
code: def write_corpus(
      fname,
      corpus,
      progress_cnt=1000,
      index=False,
      num_terms=None,
      metadata=False,
  ):
      """Save the corpus to disk in `Matrix Market format <https://math.nist.gov/MatrixMarket/formats.html>`_.
  
      Parameters
      ----------
      fname : str
          Filename of the resulting file.
      corpus : iterable of list of (int, number)
          Corpus in streamed bag-of-words format.
      progress_cnt : int, optional
          Print progress for every `progress_cnt` number of documents.
      index : bool, optional
          Return offsets?
      num_terms : int, optional
          Number of terms in the corpus. If provided, the `corpus.num_terms` attribute (if any) will be ignored.
      metadata : bool, optional
          Generate a metadata file?
  
      Returns
      -------
      offsets : {list of int, None}
          List of offsets (if index=True) or nothing.
  
      Notes
      -----
      Documents are processed one at a time, so the whole corpus is allowed to be larger than the available RAM.
  
      See Also
      --------
      :func:`gensim.corpora.mmcorpus.MmCorpus.save_corpus`
          Save corpus to disk.
  
      """
      mw = MmWriter(fname)
  
      # write empty headers to the file (with enough space to be overwritten later)
      mw.write_headers(
          -1, -1, -1
      )  # will print 50 spaces followed by newline on the stats line
  
      # calculate necessary header info (nnz elements, num terms, num docs) while writing out vectors
      _num_terms, num_nnz = 0, 0
      docno, poslast = -1, -1
      offsets = []
      if hasattr(corpus, "metadata"):
          orig_metadata = corpus.metadata
          corpus.metadata = metadata
          if metadata:
              docno2metadata = {}
      else:
          metadata = False
      for docno, doc in enumerate(corpus):
          if metadata:
              bow, data = doc
              docno2metadata[docno] = data
          else:
              bow = doc
          if docno % progress_cnt == 0:
              logger.info("PROGRESS: saving document #%i", docno)
          if index:
              posnow = mw.fout.tell()
              if posnow == poslast:
                  offsets[-1] = -1
              offsets.append(posnow)
              poslast = posnow
          max_id, veclen = mw.write_vector(docno, bow)
          _num_terms = max(_num_terms, 1 + max_id)
          num_nnz += veclen
      if metadata:
          utils.pickle(docno2metadata, fname + ".metadata.cpickle")
          corpus.metadata = orig_metadata
  
      num_docs = docno + 1
      num_terms = num_terms or _num_terms
  
      if num_docs * num_terms != 0:
          logger.info(
              "saved %ix%i matrix, density=%.3f%% (%i/%i)",
              num_docs,
              num_terms,
              100.0 * num_nnz / (num_docs * num_terms),
              num_nnz,
              num_docs * num_terms,
          )
  
      # now write proper headers, by seeking and overwriting the spaces written earlier
      mw.fake_headers(num_docs, num_terms, num_nnz)
  
      mw.close()
      if index:
          return offsets
  
imports: from __future__ import with_statement
  import logging
  import math
  from gensim import utils
  import numpy
  import scipy.sparse
  from scipy.stats import entropy
  from scipy.linalg import get_blas_funcs, triu
  from scipy.linalg.lapack import get_lapack_funcs
  from scipy.special import psi
label_desc: Use image node_id 5 for calling the MmWriter obj's underlying member method code with example usage: obj.write_corpus(fname, corpus, progress_cnt, index, num_terms, metadata) and returns: offsets