Pro-AI-TG / tg.py
ProCreations's picture
Yay
0022347 verified
raw
history blame
1.03 kB
import random
def generate_text(corpus, start_word, max_length=100):
"""
Generates text using a bigram language model.
Args:
corpus: A list of words from the training text.
start_word: The word to start the generation.
max_length: The maximum length of the generated text.
Returns:
A string of generated text.
"""
text = start_word
prev_word = start_word
for _ in range(max_length):
# Get all words that follow the previous word in the corpus
next_word_candidates = [word for word in corpus if word[0] == prev_word[-1]]
# Randomly choose the next word based on their frequency
next_word = random.choices(next_word_candidates, weights=[corpus.count(w) for w in next_word_candidates])[0]
text += " " + next_word
prev_word = next_word
return text
# Example usage
corpus = ["hello", "world", "how", "are", "you", "today", "feeling", "great", "is", "a", "beautiful", "day"]
start_word = "hello"
generated_text = generate_text(corpus, start_word)
print(generated_text)