def get_word_decoder(train_data, word_index):
    """ Returns a function that can decode a sentence. """
    reverse_word_index = dict(
        [(value, key) for (key, value) in word_index.items()])
    def reverse(idx):
        return " ".join([reverse_word_index.get(i, '?') for i in train_data[idx]])
    return reverse