# Sample text text = "the cat sat on the mat the dog sat on the floor" # Tokenize the text words = text.split() # Build bigram model bigrams = defaultdict(list) for i in range(len(words) - 1): bigrams[words[i]].append(words[i + 1]) # Function to generate text def generate_text(start_word, num_words): current_word = start_word result = [current_word] for _ in range(num_words - 1): if current_word in bigrams: next_word = random.choice(bigrams[current_word]) result.append(next_word) current_word = next_word else: break return ' '.join(result) # Generate text print(generate_text("the", 5))