# 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, 5, 12, 38), 'pandas.read_pickle', 'pd.read_pickle', ({(12, 20, 12, 37): '"""articles.pandas"""'}, {}), "('articles.pandas')", True, 'import pandas as pd\n'), ((43, 4, 43, 31), 'sys.stdout.write', 'sys.stdout.write', ({(43, 21, 43, 30): 'generated'}, {}), '(generated)', False, 'import sys\n'), ((30, 8, 30, 17), 'numpy.log', 'np.log', ({(30, 15, 30, 16): 'a'}, {}), '(a)', True, 'import numpy as np\n'), ((31, 8, 31, 17), 'numpy.exp', 'np.exp', ({(31, 15, 31, 16): 'a'}, {}), '(a)', True, 'import numpy as np\n'), ((32, 21, 32, 51), 'numpy.random.multinomial', 'np.random.multinomial', ({(32, 43, 32, 44): '(1)', (32, 46, 32, 47): 'a', (32, 49, 32, 50): '(1)'}, {}), '(1, a, 1)', True, 'import numpy as np\n'), ((57, 8, 57, 35), 'sys.stdout.write', 'sys.stdout.write', ({(57, 25, 57, 34): 'next_char'}, {}), '(next_char)', False, 'import sys\n'), ((58, 8, 58, 26), 'sys.stdout.flush', 'sys.stdout.flush', ({}, {}), '()', False, 'import sys\n'), ((31, 27, 31, 36), 'numpy.exp', 'np.exp', ({(31, 34, 31, 35): 'a'}, {}), '(a)', True, 'import numpy as np\n')]