def generate_codex(lexicon, inverse_codex=False):
    """
    Given a list of words, creates a codex of unique numeric keys assigned to the words.

    Example:
        input: ['boy', 'apple', 'dog', 'cat']
        output: {'apple': 0, 'boy': 1, 'cat': 2, 'dog': 3}

    Input:
        lexicon: (list) List of strings, corresponding to all possible labels (words) in training set
        inverse_codex: (Bool) Set to true to flip the order of keys and values in codex

    Output:
        codex: (dict) Dictionary of unique numeric keys for every word in lexicon (see above).

    """

    lexicon.sort()

    codex = {}
    for i in range(len(lexicon)):
        word = lexicon[i]
        if inverse_codex:
            codex[i] = word
        else:
            codex[word] = i

    return codex