code: def get_count_matrix(args, db_opts):
      """
      Form a sparse word to document count matrix (inverted index).
  
      M[i, j] = # times word i appears in document j.
      """
      global MAX_SZ
      with DocDB(**db_opts) as doc_db:
          doc_ids = doc_db.get_doc_ids()
  
      tok_class = tokenizers.get_class(args.tokenizer)
      if args.num_workers is not None and args.num_workers <= 1:
          # single threaded
          init(tok_class, db_opts)
      else:
          workers = ProcessPool(
              args.num_workers,
              initializer=init,
              initargs=(tok_class, db_opts),
          )
  
      # Compute the count matrix in steps (to keep in memory)
      logging.info("Mapping...")
      row, col, data = [], [], []
      step = max(int(len(doc_ids) / 10), 1)
      batches = [
          doc_ids[i : i + step] for i in range(0, len(doc_ids), step)
      ]
      _count = partial(count, args.ngram, args.hash_size)
      for i, batch in enumerate(batches):
          logging.info(
              "-" * 25
              + "Batch %d/%d" % (i + 1, len(batches))
              + "-" * 25
          )
          if args.num_workers is not None and args.num_workers <= 1:
              seq = map(_count, batch)
          else:
              seq = workers.imap_unordered(_count, batch)
          for b_row, b_col, b_data in seq:
              row.extend(b_row)
              col.extend(b_col)
              data.extend(b_data)
              if len(data) > MAX_SZ:
                  break
          if len(data) > MAX_SZ:
              logging.info("Reached max indexable size, breaking.")
              break
  
      logging.info("Creating sparse matrix...")
      data, row, col = truncate(data, row, col)
  
      count_matrix = sp.csr_matrix(
          (data, (row, col)), shape=(args.hash_size, len(doc_ids) + 1)
      )
      count_matrix.sum_duplicates()
      return count_matrix
  
imports: import numpy
  import scipy.sparse
  import math
  from multiprocessing import Pool
  from multiprocessing.util import Finalize
  from functools import partial
  from collections import Counter
  from .None import utils
  from .doc_db import DocDB
  from .None import tokenizers
  import parlai.utils.logging
label_desc: Use image node_id 7 for calling a global function with example usage: get_count_matrix(args, db_opts) and returns: count_matrix