import collections


def build_dataset(sentences, words):
    """
    Returns:
    data: sentences, but with each word is substituted by its ranking in dictionary
    count: list of (word, occurrence)
    dictionary: dict, word -> ranking
    reverse_dictionary: dict, ranking -> word
    """
    count = list()
    #NB We are using keywords, therefore no filters!
    count.extend(collections.Counter(words).most_common())
    dictionary = dict()
    for word, _ in count:
        dictionary[word] = len(dictionary)
    data = [[dictionary[w] for w in sentence] for sentence in sentences if len(sentences) > 0]
    reverse_dictionary = dict(zip(dictionary.values(), dictionary.keys())) 
    return data, count, dictionary, reverse_dictionary