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: Create :class:`~gensim.corpora.dictionary.Dictionary` from an existing corpus.
  
  Parameters
  ----------
  corpus : iterable of iterable of (int, number)
      Corpus in BoW format.
  id2word : dict of (int, object)
      Mapping id -> word. If None, the mapping `id2word[word_id] = str(word_id)` will be used.
  
  Notes
  -----
  This can be useful if you only have a term-document BOW matrix (represented by `corpus`), but not the original
  text corpus. This method will scan the term-document count matrix for all word ids that appear in it,
  then construct :class:`~gensim.corpora.dictionary.Dictionary` which maps each `word_id -> id2word[word_id]`.
  `id2word` is an optional dictionary that maps the `word_id` to a token.
  In case `id2word` isn't specified the mapping `id2word[word_id] = str(word_id)` will be used.
  
  Returns
  -------
  :class:`~gensim.corpora.dictionary.Dictionary`
      Inferred dictionary from corpus.
  
  Examples
  --------
  .. sourcecode:: pycon
  
      >>> from gensim.corpora import Dictionary
      >>>
      >>> corpus = [[(1, 1.0)], [], [(0, 5.0), (2, 1.0)], []]
      >>> dct = Dictionary.from_corpus(corpus)
      >>> len(dct)
      3
docstr_tok: ["Create",":","class",":","`","~gensim.corpora.dictionary.Dictionary","`","from","an","existing","corpus",".","Parameters","--","--","--","--","--","corpus",":","iterable","of","iterable","of","(","int",",","number",")","Corpus","in","BoW","format",".","id2word",":","dict","of","(","int",",","object",")","Mapping","id","-",">","word",".","If","None",",","the","mapping","`","id2word","[","word_id","]","=","str","(","word_id",")","`","will","be","used",".","Notes","--","--","-","This","can","be","useful","if","you","only","have","a","term-document","BOW","matrix","(","represented","by","`","corpus","`",")",",","but","not","the","original","text","corpus",".","This","method","will","scan","the","term-document","count","matrix","for","all","word","ids","that","appear","in","it",",","then","construct",":","class",":","`","~gensim.corpora.dictionary.Dictionary","`","which","maps","each","`","word_id","-",">","id2word","[","word_id","]","`",".","`","id2word","`","is","an","optional","dictionary","that","maps","the","`","word_id","`","to","a","token",".","In","case","`","id2word","`","is","n't","specified","the","mapping","`","id2word","[","word_id","]","=","str","(","word_id",")","`","will","be","used",".","Returns","--","--","--","-",":","class",":","`","~gensim.corpora.dictionary.Dictionary","`","Inferred","dictionary","from","corpus",".","Examples","--","--","--","--","..","sourcecode",":",":","pycon",">",">",">","from","gensim.corpora","import","Dictionary",">",">",">",">",">",">","corpus","=","[","[","(","1",",","1.0",")","]",",","[","]",",","[","(","0",",","5.0",")",",","(","2",",","1.0",")","]",",","[","]","]",">",">",">","dct","=","Dictionary.from_corpus","(","corpus",")",">",">",">","len","(","dct",")","3"]
code: def from_corpus(corpus, id2word=None):
      """Create :class:`~gensim.corpora.dictionary.Dictionary` from an existing corpus.
  
      Parameters
      ----------
      corpus : iterable of iterable of (int, number)
          Corpus in BoW format.
      id2word : dict of (int, object)
          Mapping id -> word. If None, the mapping `id2word[word_id] = str(word_id)` will be used.
  
      Notes
      -----
      This can be useful if you only have a term-document BOW matrix (represented by `corpus`), but not the original
      text corpus. This method will scan the term-document count matrix for all word ids that appear in it,
      then construct :class:`~gensim.corpora.dictionary.Dictionary` which maps each `word_id -> id2word[word_id]`.
      `id2word` is an optional dictionary that maps the `word_id` to a token.
      In case `id2word` isn't specified the mapping `id2word[word_id] = str(word_id)` will be used.
  
      Returns
      -------
      :class:`~gensim.corpora.dictionary.Dictionary`
          Inferred dictionary from corpus.
  
      Examples
      --------
      .. sourcecode:: pycon
  
          >>> from gensim.corpora import Dictionary
          >>>
          >>> corpus = [[(1, 1.0)], [], [(0, 5.0), (2, 1.0)], []]
          >>> dct = Dictionary.from_corpus(corpus)
          >>> len(dct)
          3
  
      """
      result = Dictionary()
      max_id = -1
      for docno, document in enumerate(corpus):
          if docno % 10000 == 0:
              logger.info("adding document #%i to %s", docno, result)
          result.num_docs += 1
          result.num_nnz += len(document)
          for wordid, word_freq in document:
              max_id = max(wordid, max_id)
              result.num_pos += word_freq
              result.dfs[wordid] = result.dfs.get(wordid, 0) + 1
  
      if id2word is None:
          # make sure length(result) == get_max_id(corpus) + 1
          result.token2id = {str(i): i for i in range(max_id + 1)}
      else:
          # id=>word mapping given: simply copy it
          result.token2id = {
              utils.to_unicode(token): idx
              for idx, token in id2word.items()
          }
      for idx in result.token2id.values():
          # make sure all token ids have a valid `dfs` entry
          result.dfs[idx] = result.dfs.get(idx, 0)
  
      logger.info(
          "built %s from %i documents (total %i corpus positions)",
          result,
          result.num_docs,
          result.num_pos,
      )
      return result
  
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 22 for calling the Dictionary obj's underlying member method code with example usage: obj.from_corpus(corpus, id2word) and returns: result