# elsewhere...
import pandas as pd
from keras.models import model_from_json
import random
import sys
import numpy as np


maxlen = 15
step = 3

df = pd.read_pickle('articles.pandas')
text = str.join(' ', df.text.tolist())

chars = set(text)
print('total chars:', len(chars))
char_indices = dict((c, i) for i, c in enumerate(chars))
indices_char = dict((i, c) for i, c in enumerate(chars))

start_index = random.randint(0, len(text) - maxlen - 1)

model = model_from_json(open('model.json').read())
model.compile(loss='categorical_crossentropy', optimizer='rmsprop')

model.load_weights('weights.h5')


def sample(a, temperature=1.0):
    # helper function to sample an index from a probability array
    a = np.log(a) / temperature
    a = np.exp(a) / np.sum(np.exp(a))
    return np.argmax(np.random.multinomial(1, a, 1))


for diversity in [0.25]:
    print()
    print('----- diversity:', diversity)

    generated = ''
    sentence = text[start_index: start_index + maxlen]
    generated += sentence
    print('----- Generating with seed: "' + sentence + '"')
    sys.stdout.write(generated)

    for i in range(200):
        x = np.zeros((1, maxlen, len(chars)))
        for t, char in enumerate(sentence):
            x[0, t, char_indices[char]] = 1.

        preds = model.predict(x, verbose=0)[0]
        next_index = sample(preds, diversity)
        next_char = indices_char[next_index]

        generated += next_char
        sentence = sentence[1:] + next_char

        sys.stdout.write(next_char)
        sys.stdout.flush()
    print()

[(12, 'pandas.read_pickle', 'pd.read_pickle', True, 'import pandas as pd\n'), (43, 'sys.stdout.write', 'sys.stdout.write', False, 'import sys\n'), (30, 'numpy.log', 'np.log', True, 'import numpy as np\n'), (31, 'numpy.exp', 'np.exp', True, 'import numpy as np\n'), (32, 'numpy.random.multinomial', 'np.random.multinomial', True, 'import numpy as np\n'), (57, 'sys.stdout.write', 'sys.stdout.write', False, 'import sys\n'), (58, 'sys.stdout.flush', 'sys.stdout.flush', False, 'import sys\n'), (31, 'numpy.exp', 'np.exp', True, 'import numpy as np\n')]