-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.py
53 lines (43 loc) · 1.77 KB
/
generator.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import re
import random
import pickle
import argparse
class Generator_text:
def __init__(self, filename):
text = open(filename, 'r', encoding='utf-8').read()
formatted_text = re.sub('[^а-яё,.!?]', ' ', text.lower())
# formatted_text = re.sub('[^a-z,.!?]', ' ', text.lower()) # english text
self.splitted_text = re.split('\\s+', formatted_text)
# биграммная модель
def fit(self):
result = {k: [] for k in self.splitted_text[:-1]}
for i in range(len(self.splitted_text) - 1):
if self.splitted_text[i + 1] not in result[self.splitted_text[i]]:
result[self.splitted_text[i]].append(self.splitted_text[i + 1])
with open('model.pkl', 'wb') as data:
pickle.dump(result, data, protocol=pickle.HIGHEST_PROTOCOL)
# генерация текста
def generate(self, length):
with open('model.pkl', 'rb') as data:
bigrams = pickle.load(data)
first_word = random.choice(list(bigrams.keys()))
next_word = first_word
generated = ''
for i in range(length):
generated += ''.join(next_word + " ")
next_word = random.choice(bigrams[next_word])
while next_word not in list(bigrams.keys()): # избежание ошибок
next_word = random.choice(list(bigrams.keys()))
print(generated)
# Запуск программы
def start():
Arguments = argparse.ArgumentParser()
Arguments.add_argument("-f")
Arguments.add_argument("-l")
args = Arguments.parse_args()
filename = args.f
length = int(args.l)
text = Generator_text(filename)
text.fit()
text.generate(length)
start()