ProCreations
commited on
Yay
Browse files
tg.py
ADDED
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
|
3 |
+
def generate_text(corpus, start_word, max_length=100):
|
4 |
+
"""
|
5 |
+
Generates text using a bigram language model.
|
6 |
+
|
7 |
+
Args:
|
8 |
+
corpus: A list of words from the training text.
|
9 |
+
start_word: The word to start the generation.
|
10 |
+
max_length: The maximum length of the generated text.
|
11 |
+
|
12 |
+
Returns:
|
13 |
+
A string of generated text.
|
14 |
+
"""
|
15 |
+
text = start_word
|
16 |
+
prev_word = start_word
|
17 |
+
for _ in range(max_length):
|
18 |
+
# Get all words that follow the previous word in the corpus
|
19 |
+
next_word_candidates = [word for word in corpus if word[0] == prev_word[-1]]
|
20 |
+
# Randomly choose the next word based on their frequency
|
21 |
+
next_word = random.choices(next_word_candidates, weights=[corpus.count(w) for w in next_word_candidates])[0]
|
22 |
+
text += " " + next_word
|
23 |
+
prev_word = next_word
|
24 |
+
return text
|
25 |
+
|
26 |
+
# Example usage
|
27 |
+
corpus = ["hello", "world", "how", "are", "you", "today", "feeling", "great", "is", "a", "beautiful", "day"]
|
28 |
+
start_word = "hello"
|
29 |
+
generated_text = generate_text(corpus, start_word)
|
30 |
+
print(generated_text)
|