class_docstr: Dictionary encapsulates the mapping between normalized words and their integer ids.
  
  Notable instance attributes:
  
  Attributes
  ----------
  token2id : dict of (str, int)
      token -> token_id. I.e. the reverse mapping to `self[token_id]`.
  cfs : dict of (int, int)
      Collection frequencies: token_id -> how many instances of this token are contained in the documents.
  dfs : dict of (int, int)
      Document frequencies: token_id -> how many documents contain this token.
  num_docs : int
      Number of documents processed.
  num_pos : int
      Total number of corpus positions (number of processed words).
  num_nnz : int
      Total number of non-zeroes in the BOW matrix (sum of the number of unique
      words per document over the entire corpus).
class_docstr_tok: ["Dictionary","encapsulates","the","mapping","between","normalized","words","and","their","integer","ids",".","Notable","instance","attributes",":","Attributes","--","--","--","--","--","token2id",":","dict","of","(","str",",","int",")","token","-",">","token_id",".","I.e",".","the","reverse","mapping","to","`","self","[","token_id","]","`",".","cfs",":","dict","of","(","int",",","int",")","Collection","frequencies",":","token_id","-",">","how","many","instances","of","this","token","are","contained","in","the","documents",".","dfs",":","dict","of","(","int",",","int",")","Document","frequencies",":","token_id","-",">","how","many","documents","contain","this","token",".","num_docs",":","int","Number","of","documents","processed",".","num_pos",":","int","Total","number","of","corpus","positions","(","number","of","processed","words",")",".","num_nnz",":","int","Total","number","of","non-zeroes","in","the","BOW","matrix","(","sum","of","the","number","of","unique","words","per","document","over","the","entire","corpus",")","."]
docstr: Save :class:`~gensim.corpora.dictionary.Dictionary` to a text file.
  
  Parameters
  ----------
  fname : str
      Path to output file.
  sort_by_word : bool, optional
      Sort words in lexicographical order before writing them out?
  
  Notes
  -----
  Format::
  
      num_docs
      id_1[TAB]word_1[TAB]document_frequency_1[NEWLINE]
      id_2[TAB]word_2[TAB]document_frequency_2[NEWLINE]
      ....
      id_k[TAB]word_k[TAB]document_frequency_k[NEWLINE]
  
  This text format is great for corpus inspection and debugging. As plaintext, it's also easily portable
  to other tools and frameworks. For better performance and to store the entire object state,
  including collected corpus statistics, use :meth:`~gensim.corpora.dictionary.Dictionary.save` and
  :meth:`~gensim.corpora.dictionary.Dictionary.load` instead.
  
  See Also
  --------
  :meth:`~gensim.corpora.dictionary.Dictionary.load_from_text`
      Load :class:`~gensim.corpora.dictionary.Dictionary` from text file.
  
  Examples
  --------
  .. sourcecode:: pycon
  
      >>> from gensim.corpora import Dictionary
      >>> from gensim.test.utils import get_tmpfile
      >>>
      >>> tmp_fname = get_tmpfile("dictionary")
      >>> corpus = [["máma", "mele", "maso"], ["ema", "má", "máma"]]
      >>>
      >>> dct = Dictionary(corpus)
      >>> dct.save_as_text(tmp_fname)
      >>>
      >>> loaded_dct = Dictionary.load_from_text(tmp_fname)
      >>> assert dct.token2id == loaded_dct.token2id
docstr_tok: ["Save",":","class",":","`","~gensim.corpora.dictionary.Dictionary","`","to","a","text","file",".","Parameters","--","--","--","--","--","fname",":","str","Path","to","output","file",".","sort_by_word",":","bool",",","optional","Sort","words","in","lexicographical","order","before","writing","them","out","?","Notes","--","--","-","Format",":",":","num_docs","id_1","[","TAB","]","word_1","[","TAB","]","document_frequency_1","[","NEWLINE","]","id_2","[","TAB","]","word_2","[","TAB","]","document_frequency_2","[","NEWLINE","]","....","id_k","[","TAB","]","word_k","[","TAB","]","document_frequency_k","[","NEWLINE","]","This","text","format","is","great","for","corpus","inspection","and","debugging",".","As","plaintext",",","it","'s","also","easily","portable","to","other","tools","and","frameworks",".","For","better","performance","and","to","store","the","entire","object","state",",","including","collected","corpus","statistics",",","use",":","meth",":","`","~gensim.corpora.dictionary.Dictionary.save","`","and",":","meth",":","`","~gensim.corpora.dictionary.Dictionary.load","`","instead",".","See","Also","--","--","--","--",":","meth",":","`","~gensim.corpora.dictionary.Dictionary.load_from_text","`","Load",":","class",":","`","~gensim.corpora.dictionary.Dictionary","`","from","text","file",".","Examples","--","--","--","--","..","sourcecode",":",":","pycon",">",">",">","from","gensim.corpora","import","Dictionary",">",">",">","from","gensim.test.utils","import","get_tmpfile",">",">",">",">",">",">","tmp_fname","=","get_tmpfile","(","``","dictionary","''",")",">",">",">","corpus","=","[","[","``","m\u00e1ma","''",",","``","mele","''",",","``","maso","''","]",",","[","``","ema","''",",","``","m\u00e1","''",",","``","m\u00e1ma","''","]","]",">",">",">",">",">",">","dct","=","Dictionary","(","corpus",")",">",">",">","dct.save_as_text","(","tmp_fname",")",">",">",">",">",">",">","loaded_dct","=","Dictionary.load_from_text","(","tmp_fname",")",">",">",">","assert","dct.token2id","==","loaded_dct.token2id"]
code: def save_as_text(self, fname, sort_by_word=True):
      """Save :class:`~gensim.corpora.dictionary.Dictionary` to a text file.
  
      Parameters
      ----------
      fname : str
          Path to output file.
      sort_by_word : bool, optional
          Sort words in lexicographical order before writing them out?
  
      Notes
      -----
      Format::
  
          num_docs
          id_1[TAB]word_1[TAB]document_frequency_1[NEWLINE]
          id_2[TAB]word_2[TAB]document_frequency_2[NEWLINE]
          ....
          id_k[TAB]word_k[TAB]document_frequency_k[NEWLINE]
  
      This text format is great for corpus inspection and debugging. As plaintext, it's also easily portable
      to other tools and frameworks. For better performance and to store the entire object state,
      including collected corpus statistics, use :meth:`~gensim.corpora.dictionary.Dictionary.save` and
      :meth:`~gensim.corpora.dictionary.Dictionary.load` instead.
  
      See Also
      --------
      :meth:`~gensim.corpora.dictionary.Dictionary.load_from_text`
          Load :class:`~gensim.corpora.dictionary.Dictionary` from text file.
  
      Examples
      --------
      .. sourcecode:: pycon
  
          >>> from gensim.corpora import Dictionary
          >>> from gensim.test.utils import get_tmpfile
          >>>
          >>> tmp_fname = get_tmpfile("dictionary")
          >>> corpus = [["máma", "mele", "maso"], ["ema", "má", "máma"]]
          >>>
          >>> dct = Dictionary(corpus)
          >>> dct.save_as_text(tmp_fname)
          >>>
          >>> loaded_dct = Dictionary.load_from_text(tmp_fname)
          >>> assert dct.token2id == loaded_dct.token2id
  
      """
      logger.info("saving dictionary mapping to %s", fname)
      with utils.open(fname, "wb") as fout:
          numdocs_line = "%d\n" % self.num_docs
          fout.write(utils.to_utf8(numdocs_line))
          if sort_by_word:
              for token, tokenid in sorted(self.token2id.items()):
                  line = "%i\t%s\t%i\n" % (
                      tokenid,
                      token,
                      self.dfs.get(tokenid, 0),
                  )
                  fout.write(utils.to_utf8(line))
          else:
              for tokenid, freq in sorted(
                  self.dfs.items(), key=lambda item: -item[1]
              ):
                  line = "%i\t%s\t%i\n" % (tokenid, self[tokenid], freq)
                  fout.write(utils.to_utf8(line))
  
imports: from collections import defaultdict
  from collections.abc import Mapping
  import logging
  import itertools
  from typing import Optional, List, Tuple
  from gensim import utils
label_desc: Use image node_id 17 for calling the Dictionary obj's underlying member method code with example usage: obj.save_as_text(fname, sort_by_word) without return types