importrandom# library for generating random numbers
importnltk# library for working with human language
fromnltk.tokenizeimportword_tokenize# A function from NLTK for breaking words down
fromnltkimportConditionalFreqDist# A class from NLTK for representing the conditional frequency distribution of a set.
nltk.download('punkt')# models used by word_tokenize to tokenize words.
defprobs_model(text):# takes text as input
words=word_tokenize(text)# split words
bigrams=list(nltk.bigrams(words))# pairs of consecutive words
cond_freq_dist=ConditionalFreqDist(bigrams)# conditional frequency of the 2 words
returncond_freq_dist
# probs_model is the representation of the conditional probs.
defgenerate_next_word(model,initial_word):# takes in the representation of the conditional probs and the initial word
next_words=model[initial_word]# randomly selects a next word based on the conditional probs
ifnotnext_words:
returnNone# in case there are no next words
next_word=random.choices(list(next_words))[0]
returnnext_word
if__name__=='__main__':
input_text="In the sweet town of Candyland, there lived a marshmallow named Mallow. Mallow had a unique passion ? a love for Alan Turing's work on computers and artificial intelligence. Instead of bouncing with other candies, Mallow spent its days reading Turing's papers and dreaming of marshmallow-powered machines. Mallow's friends couldn't quite understand its fascination, but they embraced Mallow's uniqueness. One day, Mallow surprised everyone by creating a tiny marshmallow computer that could solve candy puzzles. The town marveled at Mallow's ingenuity, and Mallow's love for Turing's work became a source of inspiration for Candyland. And so, Mallow, the marshmallow with a Turing twist, continued to blend sweetness with technology, making Candyland a tastier and smarter place."
# Reads the content of republic.txt and stores it in the variable input_text.
model=probs_model(input_text)
# Selects any word and generates the next word based on conditional probabilities