import gzip


def save_vocabs_to_file(vocabs, file_):
    """ Write vocabs.word2id map to a file

        Parameters
        ----------
        vocabs : VocabDataset
            The dataset to save
        file_ : str or file
            A file to write `word2id` to. If a path that ends with ``.gz``, it will be gzipped.
    """
    if isinstance(file_, str):
        if file_.endswith(".gz"):
            with gzip.open(file_, mode="wt", encoding="utf8") as file_:
                return save_vocabs_to_file(vocabs, file_)
        else:
            with open(file_, "w", encoding="utf8") as file_:
                return save_vocabs_to_file(vocabs, file_)

    id2word = vocabs.get_id2word()
    for i in range(len(id2word)):
        line = "{} {}\n".format(id2word[i], i)
        file_.write(line)