from typing import List
from typing import Any
import collections


def _create_ngrams(tokens: List[Any], n: int) -> collections.Counter:
  """Creates ngrams from the given list of tokens.

  Args:
    tokens: A list of tokens from which ngrams are created.
    n: Number of tokens to use, e.g. 2 for bigrams.

  Returns:
    A dictionary mapping each ngram to the number of occurrences.
  """

  ngrams = collections.Counter()
  for i in range(len(tokens) - n + 1):
    ngrams[tuple(tokens[i:i + n])] += 1
  return ngrams