import re
import numpy as np
import pandas as pd
from keras.layers import Dense, LSTM
from keras.utils import to_categorical
from tensorflow import keras
from keras.preprocessing.text import Tokenizer
from keras.models import Sequential
import tensorflow
print(“冉莹莹设计的双向LSTM模型构建及训练生成歌词应用”)
tensorflow.get_logger().setLevel(‘INFO’)
file_path = ‘wanglihong.xlsx’
songs = pd.read_excel(file_path)
#打印歌曲数据集的形状和前几行
print(songs.shape)
print(songs.head())#正则表达式函数，用于清洗歌词文本
def regex_func(text):
text = re.sub(r"[a-zA-Z()‘’…?., !! ,-]+“, ‘’, text)
text = re.sub(‘\s{2,}’, ‘’, text)
return text
new_songs = pd.DataFrame(columns=songs.columns)
new_songs[‘Title’] = songs[‘Title’]
length = []
#清洗歌词，并记录歌词长度
for i in range(len(new_songs)):
new_songs[‘Lyrics’][i] = regex_func(songs[‘Lyrics’][i])
length.append(len(new_songs[‘Lyrics’][i]))
new_songs[‘Length’] = length
print(new_songs.head())
n = len(new_songs)
print(‘一共有{}首歌’.format(n))

texts = ‘’
for i in range(n):
texts += new_songs[‘Lyrics’][i]
print(‘所有歌词的总长度：’,len(texts))

tokenizer = Tokenizer()
tokenizer.fit_on_texts(texts)
#打印字典以及词频统计结果
print(tokenizer.word_index)
print(tokenizer.word_counts)
print(sequences[:10])

num_words = 400
tokenizer = Tokenizer(num_words=num_words)
tokenizer.fit_on_texts(texts)
print(‘字典的长度：’,len(tokenizer.word_index))

sequences = tokenizer.texts_to_sequences(texts)
print('”{}“{}‘.format(texts[:10],‘分别被映射成了整数：’))
print(sequences[:10])

sequences = keras.preprocessing.sequence.pad_sequences(sequences)
print(sequences[:10])

print(‘最小索引：{}，最大索引：{}’.format(sequences.min(),sequences.max()))

star = 0
end = 0
new_songs[‘Sequences’] = ‘’
for i in range(n):
end += new_songs[‘Length’][i]
new_songs.loc[i, ‘Sequences’] = sequences[star:end]
star = end


print(new_songs.head())
#构建数据矩阵函数
def build_matrix(sequence, max_len=10):
max_len += 1
matrix = []
length = len(sequence)
for i in range(length - max_len):
matrix.append(sequence[i:i + max_len])
matrix = np.array(matrix)
X = matrix[:, :-1]
Y = matrix[:, -1]
return X, Y
X, y = build_matrix(new_songs[‘Sequences’][0])
for i in range(1, n):
sequence = new_songs[‘Sequences’][i]
XX, yy = build_matrix(sequence)
X = np.concatenate([X, XX])
y = np.concatenate([y, yy])
print(y)
print(X.shape, y.shape)
#训练模型
model = Sequential()
model.add(keras.layers.Embedding(num_words, 128, input_length=X.shape[1]))
model.add(keras.layers.Bidirectional(LSTM(128, return_sequences=True)))
model.add(keras.layers.Flatten())
model.add(Dense(128, activation=‘relu’))
model.add(Dense(num_words, activation=‘softmax’))
model.compile(loss=‘categorical_crossentropy’, optimizer=‘adam’)
model.summary()
y = to_categorical(y, num_classes=num_words)

model.fit(X, y, batch_size=256, epochs=500, verbose=1)
model.save(‘model_lyr.h5’)

输入文本序列，返回填充后的序列
def input_sequence(text, max_len=10):
sequence = tokenizer.texts_to_sequences(text)
sequence = keras.preprocessing.sequence.pad_sequences(sequence).reshape(1, -1)
sequence = keras.preprocessing.sequence.pad_sequences(sequence, maxlen=max_len)
return sequence

#根据预测结果获取下一个词
def next_word(y_pred):
idx = np.argmax(y_pred)
if idx == 0:
return ‘’
else:
return tokenizer.index_word[idx]

y=model.predict(input_sequence(’‘))
print(next_word(y))
print(‘字典长度：’,len(tokenizer.word_counts))

sequences = tokenizer.texts_to_sequences(texts)
print(’”{}"{}'.format(texts[:10],‘分别被映射成了整数：’)
写明你是怎么做的，设置了哪些参数，参数值是多少？操作结果见“XXX程序”，程序执行结果“XXX图”，对操作结果的分析等。