Created
October 25, 2019 04:17
-
-
Save StrikingLoo/eda811ba5b1f0f04062f22db27a5fe6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sample_next_word_after_sequence(word_sequence, alpha = 0): | |
next_word_vector = next_after_k_words_matrix[k_words_idx_dict[word_sequence]] + alpha | |
likelihoods = next_word_vector/next_word_vector.sum() | |
return weighted_choice(distinct_words, likelihoods.toarray()) | |
def stochastic_chain(seed, chain_length=15, seed_length=2): | |
current_words = seed.split(' ') | |
if len(current_words) != seed_length: | |
raise ValueError(f'wrong number of words, expected {seed_length}') | |
sentence = seed | |
for _ in range(chain_length): | |
sentence+=' ' | |
next_word = sample_next_word_after_sequence(' '.join(current_words)) | |
sentence+=next_word | |
current_words = current_words[1:]+[next_word] | |
return sentence | |
# example use | |
stochastic_chain('the world') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment