PY
py
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
import random
def generate_text(data, n=100):
words = data.split()
word_dict = {}
for i in range(len(words) - 1):
current_word = words[i]
next_word = words[i + 1]
if current_word not in word_dict:
word_dict[current_word] = []
word_dict[current_word].append(next_word)
current_word = random.choice(list(word_dict.keys()))
result = current_word
for _ in range(n):
if current_word not in word_dict:
break
next_word = random.choice(word_dict[current_word])
result += ' ' + next_word
current_word = next_word
return result
# Example usage
data = "Your text data here..."
generated_text = generate_text(data)
print(generated_text)
Enter to Rename, Shift+Enter to Preview
OUTPUT
Exécuter