import numpy as np import tensorflow as tf from keras.models import Sequential from keras.layers import Embedding, LSTM, Dense from keras.preprocessing.text import Tokenizer from keras.utils import pad_sequences # Путь к файлу file_path = 'vlastelin-kolec.txt' # Замените 'your_text_file.txt' на путь к вашему файлу с художественным текстом with open(file_path, 'r', encoding='utf-8') as file: text = file.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] y = tf.keras.utils.to_categorical(y, num_classes=total_words) model = Sequential() model.add(Embedding(total_words, 100, input_length=max_sequence_length-1)) model.add(LSTM(100)) model.add(Dense(total_words, activation='softmax')) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) model.fit(X, y, epochs=100, verbose=1) next_words = 100 while True: seed_text = input('Введите текст: ') if seed_text == "0": break for _ in range(next_words): token_list = tokenizer.texts_to_sequences([seed_text])[0] token_list = pad_sequences([token_list], maxlen=max_sequence_length-1, padding='pre') predicted = np.argmax(model.predict(token_list), axis=-1) output_word = "" for word, index in tokenizer.word_index.items(): if index == predicted: output_word = word break seed_text += " " + output_word print(seed_text)