IIS_2023_1/tepechin_kirill_lab_7/lab7.py

68 lines
2.5 KiB
Python

import numpy as np
from keras.models import Sequential
from keras.layers import Embedding, LSTM, Dense
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
from keras.src.layers import Dropout
#filename = "rutext.txt"
filename = "engtext.txt"
with open(filename, "r", encoding="utf-8") as f:
text = f.read()
# Создаем токенизатор и преобразуем текст в последовательности
tokenizer = Tokenizer()
tokenizer.fit_on_texts([text])
total_words = len(tokenizer.word_index) + 1
input_sequences = []
for line in text.split('\n'):
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i + 1]
input_sequences.append(n_gram_sequence)
# Подготавливаем данные для обучения
max_sequence_length = max([len(x) for x in input_sequences])
input_sequences = pad_sequences(input_sequences, maxlen=max_sequence_length, padding='pre')
X, y = input_sequences[:, :-1], input_sequences[:, -1]
# Определяем архитектуру нейронной сети
model = Sequential()
model.add(Embedding(total_words, 50, input_length=max_sequence_length - 1))
model.add(LSTM(512))
model.add(Dropout(0.2))
model.add(Dense(total_words, activation='softmax'))
# Компилируем модель
model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])
# Обучаем модель
model.fit(X, y, epochs=50, verbose=1)
# Функция для генерации текста
def generate_text(seed_text, next_words, max_sequence_len, model, tokenizer):
for _ in range(next_words):
token_list = tokenizer.texts_to_sequences([seed_text])[0]
token_list = pad_sequences([token_list], maxlen=max_sequence_len - 1, padding='pre')
predicted_probs = model.predict(token_list)[0]
predicted = np.random.choice(len(predicted_probs), p=predicted_probs)
output_word = ""
for word, index in tokenizer.word_index.items():
if index == predicted:
output_word = word
break
seed_text += " " + output_word
return seed_text
# Пример использования
#generated_text = generate_text("Война и", 25, max_sequence_length, model, tokenizer)
generated_text = generate_text("Shakespeare was", 25, max_sequence_length, model, tokenizer)
print(generated_text)