import tempfile


def create_temporary_vocab_file(words, counts=None):
  """
    Creates a temporary vocabulary file.

    Args:
      words: List of words in the vocabulary

    Returns:
      A temporary file object with one word per line
    """
  vocab_file = tempfile.NamedTemporaryFile()
  if counts is None:
    for token in words:
      vocab_file.write((token + "\n").encode("utf-8"))
  else:
    for token, count in zip(words, counts):
      vocab_file.write("{}\t{}\n".format(token, count).encode("utf-8"))
  vocab_file.flush()
  return vocab_file