code: def _create_source(
      index,
      dictionary,
      tfidf,
      symmetric,
      dominant,
      nonzero_limit,
      dtype,
  ):
      """Build a sparse term similarity matrix using a term similarity index.
  
      Returns
      -------
      matrix : :class:`scipy.sparse.coo_matrix`
          The sparse term similarity matrix.
  
      """
      assert isinstance(index, TermSimilarityIndex)
      assert dictionary is not None
      matrix_order = len(dictionary)
  
      if matrix_order == 0:
          raise ValueError(
              "Dictionary provided to SparseTermSimilarityMatrix must not be empty"
          )
  
      logger.info(
          "constructing a sparse term similarity matrix using %s", index
      )
  
      if nonzero_limit is None:
          nonzero_limit = matrix_order
  
      def tfidf_sort_key(term_index):
          if isinstance(term_index, tuple):
              term_index, *_ = term_index
          term_idf = tfidf.idfs[term_index]
          return (-term_idf, term_index)
  
      if tfidf is None:
          columns = sorted(dictionary.keys())
          logger.info(
              "iterating over %i columns in dictionary order",
              len(columns),
          )
      else:
          assert max(tfidf.idfs) == matrix_order - 1
          columns = sorted(tfidf.idfs.keys(), key=tfidf_sort_key)
          logger.info(
              "iterating over %i columns in tf-idf order", len(columns)
          )
  
      nonzero_counter_dtype = _shortest_uint_dtype(nonzero_limit)
  
      column_nonzero = np.array(
          [0] * matrix_order, dtype=nonzero_counter_dtype
      )
      if dominant:
          column_sum = np.zeros(matrix_order, dtype=dtype)
      if symmetric:
          assigned_cells = set()
      row_buffer = array("Q")
      column_buffer = array("Q")
      if dtype is np.float16 or dtype is np.float32:
          data_buffer = array("f")
      elif dtype is np.float64:
          data_buffer = array("d")
      else:
          raise ValueError(
              "Dtype %s is unsupported, use numpy.float16, float32, or float64."
              % dtype
          )
  
      def cell_full(t1_index, t2_index, similarity):
          if dominant and column_sum[t1_index] + abs(similarity) >= 1.0:
              return True  # after adding the similarity, the matrix would cease to be strongly diagonally dominant
          assert column_nonzero[t1_index] <= nonzero_limit
          if column_nonzero[t1_index] == nonzero_limit:
              return True  # after adding the similarity, the column would contain more than nonzero_limit elements
          if symmetric and (t1_index, t2_index) in assigned_cells:
              return True  # a similarity has already been assigned to this cell
          return False
  
      def populate_buffers(t1_index, t2_index, similarity):
          column_buffer.append(t1_index)
          row_buffer.append(t2_index)
          data_buffer.append(similarity)
          column_nonzero[t1_index] += 1
          if symmetric:
              assigned_cells.add((t1_index, t2_index))
          if dominant:
              column_sum[t1_index] += abs(similarity)
  
      try:
          from tqdm import tqdm as progress_bar
      except ImportError:
  
          def progress_bar(iterable):
              return iterable
  
      for column_number, t1_index in enumerate(progress_bar(columns)):
          column_buffer.append(column_number)
          row_buffer.append(column_number)
          data_buffer.append(1.0)
  
          if nonzero_limit <= 0:
              continue
  
          t1 = dictionary[t1_index]
          num_nonzero = column_nonzero[t1_index]
          num_rows = nonzero_limit - num_nonzero
          most_similar = (
              [
                  (dictionary.token2id[term], similarity)
                  for term, similarity in index.most_similar(
                      t1, topn=num_rows
                  )
                  if term in dictionary.token2id
              ]
              if num_rows > 0
              else []
          )
  
          if tfidf is None:
              rows = sorted(most_similar)
          else:
              rows = sorted(most_similar, key=tfidf_sort_key)
  
          for t2_index, similarity in rows:
              if cell_full(t1_index, t2_index, similarity):
                  continue
              if not symmetric:
                  populate_buffers(t1_index, t2_index, similarity)
              elif not cell_full(t2_index, t1_index, similarity):
                  populate_buffers(t1_index, t2_index, similarity)
                  populate_buffers(t2_index, t1_index, similarity)
  
      data_buffer = np.frombuffer(data_buffer, dtype=dtype)
      row_buffer = np.frombuffer(row_buffer, dtype=np.uint64)
      column_buffer = np.frombuffer(column_buffer, dtype=np.uint64)
      matrix = sparse.coo_matrix(
          (data_buffer, (row_buffer, column_buffer)),
          shape=(matrix_order, matrix_order),
      )
  
      logger.info(
          "constructed a sparse term similarity matrix with %0.06f%% density",
          100.0 * matrix.getnnz() / matrix_order**2,
      )
  
      return matrix
  
imports: from array import array
  from itertools import chain
  import logging
  from math import sqrt
  import numpy
  from scipy import sparse
  from gensim.matutils import corpus2csc
  from gensim.utils import SaveLoad, is_corpus
label_desc: Use image node_id 2 for calling a global function with example usage: _create_source(index, dictionary, tfidf, symmetric, dominant, nonzero_limit, dtype) and returns: matrix, unknown,  term_index, False, True, True, True, iterable