def create_vocabulary(dataset, terminal_token='<e>'):
    """Map each token in the dataset to a unique integer id.
    :param dataset a 2-d array, contains sequences of tokens.
                    A token can be a word or a character, depending on the problem.
    :param terminal_token (Optional). If specified, will be added to the vocabulary with id=0.
    :returns a tuple of vocabulary (a map from token to unique id) and reversed
        vocabulary (a map from unique ids to tokens).
    """
    vocabulary = {}
    reverse_vocabulary = []
    if terminal_token is not None:
        vocabulary[terminal_token] = 0
        reverse_vocabulary.append(terminal_token)
    for sample in dataset:
        for token in sample:
            if not token in vocabulary:
                vocabulary[token] = len(vocabulary)
                reverse_vocabulary.append(token)
    return vocabulary, reverse_vocabulary