File size: 1,033 Bytes
0022347
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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)