-
Notifications
You must be signed in to change notification settings - Fork 0
/
01_web_scrapping_letras_musicas.py
173 lines (131 loc) · 5.5 KB
/
01_web_scrapping_letras_musicas.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import os
import nltk
from bs4 import BeautifulSoup
from gensim import models
from keras.callbacks import ModelCheckpoint, LambdaCallback, TensorBoard
from keras.layers import Dense
from keras.layers import Dropout
from keras.layers import LSTM, Embedding, Bidirectional, CuDNNLSTM
from keras.models import Sequential, load_model
from sklearn import feature_extraction
import numpy as np
import argparse
import random
parser = argparse.ArgumentParser()
parser.add_argument("-epochs", type=int, default=1)
parser.add_argument("-units1", type=int, default=30)
parser.add_argument("-units2", type=int, default=30)
parser.add_argument("-batchsize", type=int, default=64)
parser.add_argument("-gpu", type=bool, default=False)
parser.add_argument("-t", type=int, default=5)
args = vars(parser.parse_args())
print(args)
documentos = []
walklist = os.walk("dados/letras-musicas")
for root, subdirs, arquivos in walklist:
for arquivo in arquivos:
if "html" not in arquivo:
continue
caminho = os.path.join(root, arquivo)
# caminho = "dados/letras-musicas/" + arquivo
if os.path.isfile(caminho):
with open(caminho, "r") as f:
html = f.read()
html = feature_extraction.text.strip_accents_ascii(html.lower())
html = html.replace("<br/>", " NLINHA ")
html = html.replace("<p>", " NLINHA ")
soup = BeautifulSoup(html, "html.parser")
documentos.append(soup.text.strip())
qnt_musicas = len(documentos)
print("{} musicas carregadas".format(qnt_musicas))
docs_tokenizados = []
tokenizador = nltk.TreebankWordTokenizer()
for doc in documentos:
tokens = tokenizador.tokenize(doc)
docs_tokenizados.append(tokens)
caminho_modelo_w2v = "modelos/w2v_{}.model".format(qnt_musicas)
if os.path.isfile(caminho_modelo_w2v):
print("Carregando modelo w2v previo...")
w2v_model = models.Word2Vec.load(caminho_modelo_w2v)
else:
print("Treinando modelo w2v...")
w2v_model = models.Word2Vec(docs_tokenizados, size=350, window=15, min_count=0, workers=os.cpu_count(), iter=300)
w2v_model.save(caminho_modelo_w2v)
vocab = list(w2v_model.wv.vocab)
# Funcoes que retornam o indice da palavra no vocabulario criado pelo word2vec e vice-versa
def word2idx(word):
return w2v_model.wv.vocab[word].index
def idx2word(idx):
return w2v_model.wv.index2word[idx]
maxlen = 15
sentences = []
next_tokens = []
for doc in docs_tokenizados: # Cria as "sentencas" para cada documento do corpus
for i in range(0, len(doc) - maxlen):
sentences.append(doc[i:i + maxlen])
next_tokens.append(doc[i + maxlen])
print('Number of sequences:', len(sentences), "\n")
# Criando os inputs(x) e targets(y)
x = np.zeros((len(sentences), maxlen), dtype=np.int32)
y = np.zeros((len(sentences)), dtype=np.int32)
for i, sentence in enumerate(sentences):
for t, token in enumerate(sentence):
x[i, t] = word2idx(token)
y[i] = word2idx(next_tokens[i])
pretrained_weights = w2v_model.wv.vectors
tamanho_vocab = pretrained_weights.shape[0]
tamanho_vetor_w2v = pretrained_weights.shape[1] # 350
print("Tamanho vocab e w2v vector: ", (tamanho_vocab, tamanho_vetor_w2v))
units1 = args["units1"]
units2 = args["units2"]
nome_modelo = "lstm-w2v-wordlevel-{}len-{}-{}-sertanejo.model".format(maxlen, units1, units2)
caminho_modelo_lstm = "modelos/{}".format(nome_modelo)
if os.path.isfile(caminho_modelo_lstm):
print("Carregando modelo lstm previo...")
model = load_model(caminho_modelo_lstm)
else:
print("Criando novo modelo LSTM")
model = Sequential()
model.add(Embedding(input_dim=tamanho_vocab, output_dim=tamanho_vetor_w2v, weights=[pretrained_weights]))
if args["gpu"]:
model.add(CuDNNLSTM(units1, return_sequences=True))
else:
model.add(LSTM(units1, return_sequences=True))
model.add(Dropout(0.1))
if args["gpu"]:
model.add(CuDNNLSTM(units=units2))
else:
model.add(LSTM(units=units2))
model.add(Dropout(0.1))
model.add(Dense(tamanho_vocab, activation='softmax')) # Quantidade de 'respostas' possiveis. Tokens neste caso.
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=["accuracy"])
def gerar_texto(epoch, logs):
print('----- Generating text after Epoch: %d' % epoch)
start = np.random.randint(0, len(sentences))
seed_tokens = list(sentences[start])
print("Seed:")
print("\"", " ".join(seed_tokens), "\"\n")
temperatura = args["t"]
aux = 1
for i in range(500):
xt = np.zeros((1, maxlen), dtype=np.int32)
for t, token in enumerate(seed_tokens):
xt[0, t] = word2idx(token)
prediction = model.predict(xt, verbose=0)
if temperatura > 0 and aux % temperatura == 0:
ordered = (-prediction[0]).argsort()[:3]
indice = np.random.choice(ordered)
else:
indice = (np.argmax(prediction))
result = idx2word(indice)
print("\n" if result == "NLINHA" else result, end=" ")
seed_tokens.append(result)
seed_tokens = seed_tokens[1:len(seed_tokens)]
aux = aux + 1
print("\n\nFIM\n\n", )
checkpoint = ModelCheckpoint(caminho_modelo_lstm, monitor='loss', verbose=1, save_best_only=True, mode='min')
print_callback = LambdaCallback(on_epoch_end=gerar_texto)
tensorboard = TensorBoard(log_dir="logs/{}".format(nome_modelo))
callbacks_list = [checkpoint, print_callback, tensorboard]
print(model.summary())
model.fit(x, y, epochs=args["epochs"], batch_size=args["batchsize"], callbacks=callbacks_list)