Skip to content

Instantly share code, notes, and snippets.

@nickwan
Created February 22, 2018 03:52
Show Gist options
  • Select an option

  • Save nickwan/26bc5f9df20667748ae8a2e8804e42fa to your computer and use it in GitHub Desktop.

Select an option

Save nickwan/26bc5f9df20667748ae8a2e8804e42fa to your computer and use it in GitHub Desktop.
def generate_tweets(model, corpus, char_to_idx, idx_to_char, n_tweets=10):
# model.load_weights('weights.hdf5')
tweets = []
spaces_in_corpus = np.array([idx for idx in range(CORPUS_LENGTH) if text[idx] == ' '])
for i in range(1, n_tweets + 1):
begin = np.random.choice(spaces_in_corpus)
tweet = u''
sequence = text[begin:begin + MAX_SEQ_LENGTH]
tweet += sequence
for _ in range(100):
x = np.zeros((1, MAX_SEQ_LENGTH, N_CHARS))
for t, char in enumerate(sequence):
x[0, t, char_to_index[char]] = 1.0
preds = model.predict(x, verbose=0)[0]
next_idx = sample(preds)
next_char = index_to_char[next_idx]
tweet += next_char
sequence = sequence[1:] + next_char
tweets.append(tweet)
return tweets
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment