import collections


def _build_vocab(seq_list_list, add_unknown_token=None, add_beg_token=None, add_end_token=None):
    """
    build a vocabulary from a given list of string lists

    Args:
        seq_list_list: several corpus
        add_unknown_token: if not None, add unknown_token to the vocabulary
        add_beg_token: str, if not None, then add and process the beg_token
        add_end_token: str, if not None, then add and process the end_token

    Returns:
        tuple (word_to_id, word_count)
    """
    counter = collections.Counter()
    for seq_list in seq_list_list:
        for seq in seq_list:
            counter.update(seq)

    # processing the counts of special tokens
    max_count = max([x[1] for x in counter.items()])
    if add_unknown_token is not None:
        counter[add_unknown_token] = max_count + 1
    if add_end_token is not None:
        counter[add_end_token] = max_count + 2
    if add_beg_token is not None:
        counter[add_beg_token] = max_count + 3

    count_pairs = sorted(counter.items(), key=lambda x: (-x[1], x[0]))

    words, counts = list(zip(*count_pairs))
    word_to_id = dict(zip(words, range(len(words))))
    word_count = dict(count_pairs)

    return word_to_id, word_count