In [1]:
import nltk
In [2]:
corpus = nltk.corpus.brown.words()
# corpus = ["I", "think", "I", "can", "do", "it", "."]
N = 4

brown_ngrams = nltk.ngrams(corpus, N)
condition_pairs = ((tuple(seq[:-1]), seq[-1]) for seq in brown_ngrams)
cfd_brown = nltk.ConditionalFreqDist(condition_pairs)
cpd_brown = nltk.ConditionalProbDist(cfd_brown, nltk.ELEProbDist)
In [3]:
def sample(prompt, length=100):
    words = prompt.split()

    while len(words) < length:
        input = tuple(words[-(N - 1):])

        try:
            next_word = cpd_brown[input].generate()
        except ValueError:
            # Break if there is no possible continuation
            break
        
        words.append(next_word)
    
    return " ".join(words)
In [4]:
sample("I agree with")
Out[4]:
"I agree with so many balls in the air to produce rancidity . Sprouting is a naturally occurring phenomenon in stored potatoes , onions , carrots , beets , and similar items of historical medico-military significance . During the next weeks he looked over the church and studied its condition . The hemorrhage was in the house must be going with her '' . Now when , so to speak , the hall became silent . `` I said I would first have to get organized . The baby can have an early nap . Victoria , I want you"