from typing import Counter


def build_word_vocab(data_path, filenames, mincount=1, whole_dataset=False):
    """Computes word vocabulary of several files and save it to "vocab.words.txt".

    Args:
        data_path (str) : Path of the folder containing the sentence files named "[filename].words.txt"
        filenames (list) : list of filenames (ex : ["train"] if file is named "train.words.txt")
        mincount (int) : minimal number of occurences to consider (default:1)

    Returns:
        set of words kept
    """
    word_counter = Counter()
    for fn in filenames:
        vocab = Counter()
        with open(data_path + fn + ".words.txt", "r", encoding="utf8") as file:
            for line in file:
                word_counter.update(line.strip().split())
                vocab.update(line.strip().split())

            vocab = {w for w, c in vocab.items() if c >= mincount}
            with open(data_path + "vocab.{}.words.txt".format(fn), "w", encoding="utf8") as file:
                for w in sorted(list(vocab)):
                    file.write(w + "\n")

    word_vocab = {w for w, c in word_counter.items() if c >= mincount}

    if whole_dataset:
        with open(data_path + "vocab.words.txt", "w", encoding="utf8") as file:
            for w in sorted(list(word_vocab)):
                file.write(w + "\n")

    print('Word vocabulary built. Kept {} out of {}'.format(len(word_vocab), len(word_counter)))

    return word_vocab