from datetime import datetime

from glossolalia.loader import load_seeds, load_texts
from glossolalia.lstm import LisSansTaMaman

"""
Jets. atomic answer. peterson said.
"can you come to have to get back to the moon base?"
The old came back and stood at the door.
Uneasily in the heat lay the police
the optus went down at him, its eyes identical and expressionless.
hard to be space commissioner.
i've a strange can't be operate the same way
we're a day of mechanical
he saw his forehead. and he had to be hard to get it.
he was a long way from the big ground
he had to be a few paces
a thin figure with a universe
"""


def train():
    # should_train = True
    nb_words = 50
    nb_epoch = 100
    nb_layers = 100
    dropout = .3
    validation_split = 0.2
    lstm = LisSansTaMaman(nb_layers, dropout, validation_split, debug=True)
    filename_output = "./output/dickish_%i-d%.1f_%s.txt" % (
        nb_layers, dropout, datetime.now().strftime("%y%m%d_%H%M"))

    corpus = load_texts()
    print("Corpus:", corpus[:10])
    lstm.create_model(corpus)

    with open(filename_output, "a+") as f:
        for i in range(0, nb_epoch, 10):
            lstm.fit(epochs=min(i + 10, nb_epoch), initial_epoch=i,
                     validation_split=validation_split)

            for output in lstm.predict_seeds(nb_words):
                print(output)
                f.writelines(output)

        for i, seed in enumerate(load_seeds(corpus, 5)):
            output = lstm.predict(seed, nb_words)
            print("%i %s -> %s" % (i, seed, output))
            f.writelines(output)

        while True:
            input_text = input("> ")
            text = lstm.predict(input_text, nb_words)
            print(text)
            f.writelines("%s\n" % text)


if __name__ == '__main__':
    train()
