Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# code courtesy of https://nlpforhackers.io/language-models/
import random
# starting words
text = ["today", "the"]
sentence_finished = False
while not sentence_finished:
# select a random probability threshold
r = random.random()
accumulator = .0
for word in model[tuple(text[-2:])].keys():
accumulator += model[tuple(text[-2:])][word]
# select words that are above the probability threshold
if accumulator >= r:
text.append(word)
break
if text[-2:] == [None, None]:
sentence_finished = True
print (' '.join([t for t in text if t]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.