-
Notifications
You must be signed in to change notification settings - Fork 0
/
task3.2.py
34 lines (30 loc) · 1.13 KB
/
task3.2.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
27
28
29
30
31
32
33
34
import random
class MarkovChainWordGenerator:
def __init__(self, text, order=1):
self.order = order
self.model = self.build_model(text.split())
def build_model(self, words):
model = {}
for i in range(len(words) - self.order):
state = tuple(words[i:i+self.order])
next_word = words[i+self.order]
if state not in model:
model[state] = []
model[state].append(next_word)
return model
def generate_text(self, length=50, seed=None):
if seed is None:
seed = random.choice(list(self.model.keys()))
result = list(seed)
for _ in range(length - self.order):
state = tuple(result[-self.order:])
if state not in self.model:
break
next_word = random.choice(self.model[state])
result.append(next_word)
return ' '.join(result)
# Example usage
text = "this is an example text for the Markov chain word generator."
generator = MarkovChainWordGenerator(text, order=2)
generated_text = generator.generate_text(length=10)
print(generated_text)