def build_count_matrix(input_ids,
                       input_mask,
                       ngrams=2,
                       hash_size=int(math.pow(2, 24))):
  """Build a sparse matrix of size ngrams to doc ids with counts."""
  rows, cols, data = [], [], []
  for ii in tqdm(range(len(input_ids))):
    my_seq = [
        str(tok)
        for jj, tok in enumerate(input_ids[ii])
        if input_mask[ii][jj] == 1
    ]
    my_counts = get_hashed_counts(my_seq, ngrams, hash_size)
    rows.extend(my_counts.keys())
    cols.extend([ii] * len(my_counts))
    data.extend(my_counts.values())
  return sp.csr_matrix((data, (rows, cols)), shape=(hash_size, len(input_ids)))
Build a sparse matrix of size ngrams to doc ids with counts.
language/labs/drkit/search_utils.py