# 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()

[((149, 182), 'pandas.read_pickle', 'pd.read_pickle', (['"""articles.pandas"""'], {}), "('articles.pandas')\n", (163, 182), True, 'import pandas as pd\n'), ((1068, 1095), 'sys.stdout.write', 'sys.stdout.write', (['generated'], {}), '(generated)\n', (1084, 1095), False, 'import sys\n'), ((708, 717), 'numpy.log', 'np.log', (['a'], {}), '(a)\n', (714, 717), True, 'import numpy as np\n'), ((740, 749), 'numpy.exp', 'np.exp', (['a'], {}), '(a)\n', (746, 749), True, 'import numpy as np\n'), ((791, 821), 'numpy.random.multinomial', 'np.random.multinomial', (['(1)', 'a', '(1)'], {}), '(1, a, 1)\n', (812, 821), True, 'import numpy as np\n'), ((1481, 1508), 'sys.stdout.write', 'sys.stdout.write', (['next_char'], {}), '(next_char)\n', (1497, 1508), False, 'import sys\n'), ((1517, 1535), 'sys.stdout.flush', 'sys.stdout.flush', ([], {}), '()\n', (1533, 1535), False, 'import sys\n'), ((759, 768), 'numpy.exp', 'np.exp', (['a'], {}), '(a)\n', (765, 768), True, 'import numpy as np\n')]