Skip to content

Instantly share code, notes, and snippets.

@drusepth
Created November 18, 2018 04:03
Show Gist options
  • Save drusepth/4d7cd5d526edf3b8a042fdec734293a2 to your computer and use it in GitHub Desktop.
Save drusepth/4d7cd5d526edf3b8a042fdec734293a2 to your computer and use it in GitHub Desktop.
import functools
def markov_chain(initial_word=None, heuristic_function=lambda x, y: 0):
chain_words = [initial_word]
while chain_words[-1] != None:
current_word = chain_words[-1]
# Generate a pool of potential antecedents to choose from
potential_followup_words = followup_words_for(current_word)
# Rank that pool of antecedents by a heuristic comparing options
ranked_followup_words = sorted(potential_followup_words, key=functools.cmp_to_key(heuristic_function))
# Choose a word and advance forward through the chain
selected_word = ranked_followup_words[0]
chain_words.append(selected_word)
return " ".join(chain_words[0:-1])
def frequency_heuristic(option_one, option_two):
print("in heuristic")
print(option_one)
print(option_two)
print(markov_chain(initial_word="There", heuristic_function=frequency_heuristic))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment