class_docstr: Corpus handles input in `Mallet format <http://mallet.cs.umass.edu/import.php>`_.
  
  **Format description**
  
  One file, one instance per line, assume the data is in the following format ::
  
      [URL] [language] [text of the page...]
  
  Or, more generally, ::
  
      [document #1 id] [label] [text of the document...]
      [document #2 id] [label] [text of the document...]
      ...
      [document #N id] [label] [text of the document...]
  
  Note that language/label is *not* considered in Gensim, used `__unknown__` as default value.
  
  Examples
  --------
  .. sourcecode:: pycon
  
      >>> from gensim.test.utils import get_tmpfile, common_texts
      >>> from gensim.corpora import MalletCorpus
      >>> from gensim.corpora import Dictionary
      >>>
      >>> # Prepare needed data
      >>> dictionary = Dictionary(common_texts)
      >>> corpus = [dictionary.doc2bow(doc) for doc in common_texts]
      >>>
      >>> # Write corpus in Mallet format to disk
      >>> output_fname = get_tmpfile("corpus.mallet")
      >>> MalletCorpus.serialize(output_fname, corpus, dictionary)
      >>>
      >>> # Read corpus
      >>> loaded_corpus = MalletCorpus(output_fname)
class_docstr_tok: ["Corpus","handles","input","in","`","Mallet","format","<","http",":","\/\/mallet.cs.umass.edu\/import.php",">","`","_",".","*","*","Format","description","*","*","One","file",",","one","instance","per","line",",","assume","the","data","is","in","the","following","format",":",":","[","URL","]","[","language","]","[","text","of","the","page","...","]","Or",",","more","generally",",",":",":","[","document","#","1","id","]","[","label","]","[","text","of","the","document","...","]","[","document","#","2","id","]","[","label","]","[","text","of","the","document","...","]","...","[","document","#","N","id","]","[","label","]","[","text","of","the","document","...","]","Note","that","language\/label","is","*","not","*","considered","in","Gensim",",","used","`","__unknown__","`","as","default","value",".","Examples","--","--","--","--","..","sourcecode",":",":","pycon",">",">",">","from","gensim.test.utils","import","get_tmpfile",",","common_texts",">",">",">","from","gensim.corpora","import","MalletCorpus",">",">",">","from","gensim.corpora","import","Dictionary",">",">",">",">",">",">","#","Prepare","needed","data",">",">",">","dictionary","=","Dictionary","(","common_texts",")",">",">",">","corpus","=","[","dictionary.doc2bow","(","doc",")","for","doc","in","common_texts","]",">",">",">",">",">",">","#","Write","corpus","in","Mallet","format","to","disk",">",">",">","output_fname","=","get_tmpfile","(","``","corpus.mallet","''",")",">",">",">","MalletCorpus.serialize","(","output_fname",",","corpus",",","dictionary",")",">",">",">",">",">",">","#","Read","corpus",">",">",">","loaded_corpus","=","MalletCorpus","(","output_fname",")"]
docstr: Save a corpus in the Mallet format.
  
  Warnings
  --------
  This function is automatically called by :meth:`gensim.corpora.malletcorpus.MalletCorpus.serialize`,
  don't call it directly, call :meth:`gensim.corpora.lowcorpus.malletcorpus.MalletCorpus.serialize` instead.
  
  Parameters
  ----------
  fname : str
      Path to output file.
  corpus : iterable of iterable of (int, int)
      Corpus in BoW format.
  id2word : {dict of (int, str), :class:`~gensim.corpora.dictionary.Dictionary`}, optional
      Mapping between word_ids (integers) and words (strings).
      If not provided, the mapping is constructed directly from `corpus`.
  metadata : bool, optional
      If True - ????
  
  Return
  ------
  list of int
      List of offsets in resulting file for each document (in bytes),
      can be used for :meth:`~gensim.corpora.malletcorpus.Malletcorpus.docbyoffset`.
  
  Notes
  -----
  The document id will be generated by enumerating the corpus.
  That is, it will range between 0 and number of documents in the corpus.
  
  Since Mallet has a language field in the format, this defaults to the string '__unknown__'.
  If the language needs to be saved, post-processing will be required.
docstr_tok: ["Save","a","corpus","in","the","Mallet","format",".","Warnings","--","--","--","--","This","function","is","automatically","called","by",":","meth",":","`","gensim.corpora.malletcorpus.MalletCorpus.serialize","`",",","do","n't","call","it","directly",",","call",":","meth",":","`","gensim.corpora.lowcorpus.malletcorpus.MalletCorpus.serialize","`","instead",".","Parameters","--","--","--","--","--","fname",":","str","Path","to","output","file",".","corpus",":","iterable","of","iterable","of","(","int",",","int",")","Corpus","in","BoW","format",".","id2word",":","{","dict","of","(","int",",","str",")",",",":","class",":","`","~gensim.corpora.dictionary.Dictionary","`","}",",","optional","Mapping","between","word_ids","(","integers",")","and","words","(","strings",")",".","If","not","provided",",","the","mapping","is","constructed","directly","from","`","corpus","`",".","metadata",":","bool",",","optional","If","True","-","?","?","?","?","Return","--","--","--","list","of","int","List","of","offsets","in","resulting","file","for","each","document","(","in","bytes",")",",","can","be","used","for",":","meth",":","`","~gensim.corpora.malletcorpus.Malletcorpus.docbyoffset","`",".","Notes","--","--","-","The","document","id","will","be","generated","by","enumerating","the","corpus",".","That","is",",","it","will","range","between","0","and","number","of","documents","in","the","corpus",".","Since","Mallet","has","a","language","field","in","the","format",",","this","defaults","to","the","string","'__unknown__","'",".","If","the","language","needs","to","be","saved",",","post-processing","will","be","required","."]
code: def save_corpus(fname, corpus, id2word=None, metadata=False):
      """Save a corpus in the Mallet format.
  
      Warnings
      --------
      This function is automatically called by :meth:`gensim.corpora.malletcorpus.MalletCorpus.serialize`,
      don't call it directly, call :meth:`gensim.corpora.lowcorpus.malletcorpus.MalletCorpus.serialize` instead.
  
      Parameters
      ----------
      fname : str
          Path to output file.
      corpus : iterable of iterable of (int, int)
          Corpus in BoW format.
      id2word : {dict of (int, str), :class:`~gensim.corpora.dictionary.Dictionary`}, optional
          Mapping between word_ids (integers) and words (strings).
          If not provided, the mapping is constructed directly from `corpus`.
      metadata : bool, optional
          If True - ????
  
      Return
      ------
      list of int
          List of offsets in resulting file for each document (in bytes),
          can be used for :meth:`~gensim.corpora.malletcorpus.Malletcorpus.docbyoffset`.
  
      Notes
      -----
      The document id will be generated by enumerating the corpus.
      That is, it will range between 0 and number of documents in the corpus.
  
      Since Mallet has a language field in the format, this defaults to the string '__unknown__'.
      If the language needs to be saved, post-processing will be required.
  
      """
      if id2word is None:
          logger.info(
              "no word id mapping provided; initializing from corpus"
          )
          id2word = utils.dict_from_corpus(corpus)
  
      logger.info("storing corpus in Mallet format into %s", fname)
  
      truncated = 0
      offsets = []
      with utils.open(fname, "wb") as fout:
          for doc_id, doc in enumerate(corpus):
              if metadata:
                  doc_id, doc_lang = doc[1]
                  doc = doc[0]
              else:
                  doc_lang = "__unknown__"
  
              words = []
              for wordid, value in doc:
                  if abs(int(value) - value) > 1e-6:
                      truncated += 1
                  words.extend(
                      [utils.to_unicode(id2word[wordid])] * int(value)
                  )
              offsets.append(fout.tell())
              fout.write(
                  utils.to_utf8(
                      "%s %s %s\n" % (doc_id, doc_lang, " ".join(words))
                  )
              )
  
      if truncated:
          logger.warning(
              "Mallet format can only save vectors with integer elements; "
              "%i float entries were truncated to integer value",
              truncated,
          )
  
      return offsets
  
file_path: gensim/gensim/corpora/malletcorpus.py
imports: from __future__ import with_statement
  import logging
  from gensim import utils
  from gensim.corpora import LowCorpus
label_desc: Use image node_id 5 for calling the MalletCorpus obj's underlying member method code with example usage: obj.save_corpus(fname, corpus, id2word, metadata) and returns: offsets