Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import random # library for generating random numbers
import nltk # library for working with human language
from nltk.tokenize import word_tokenize # A function from NLTK for breaking words down
from nltk import ConditionalFreqDist # A class from NLTK for representing the conditional frequency distribution of a set.
nltk.download('punkt') # models used by word_tokenize to tokenize words.
def probs_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
return cond_freq_dist
# probs_model is the representation of the conditional probs.
def generate_sentence(model, initial_word, length): # takes in the representation of the conditional probs, seed word, and sentence length
sentence = [initial_word]
#Iterates to generate the next word based on the conditional probabilities until the sentence length is reached.
for _ in range(length - 1):
next_words = model[sentence[-1]]
if not next_words:
break # If there are no next words, end the sentence
next_word = random.choice(list(next_words))
sentence.append(next_word)
return ' '.join(sentence) #combine the generated words into a single string
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."
model = probs_model(input_text)
seed_word = "Alan" # Select any word as the starting point
sentence_length = 15 # desired sentence length
generated_sentence = generate_sentence(model, seed_word, sentence_length)
# Print the input text and the generated sentence.
print("Input text:", input_text)
print(f"Given seed word: '{seed_word}', Generated sentence: {generated_sentence}")