Следующий код:
from keras.preprocessing.sequence import skipgrams
from keras.preprocessing import text
with open('engl_bible.txt', 'r') as file:
norm_bible = file
tokenizer = text.Tokenizer()
tokenizer.fit_on_texts(norm_bible)
word2id = tokenizer.word_counts
id2word = {v:k for k, v in word2id.items()}
vocab_size = len(word2id) + 1
embed_size = 100
wids = [[word2id[w] for w in text.text_to_word_sequence(doc)] for doc in norm_bible]
#print('Vocabulary Size:', vocab_size)
#print('Vocabulary Sample:', list(word2id.items())[:10])
# generate skip-grams
#skip_grams = keras.preprocessing.sequence.skipgrams(sequence, vocabulary_size, window_size=4, negative_samples=1.0, shuffle=True,
# categorical=False, sampling_table=None, seed=None)
skip_grams = [skipgrams(wid, vocabulary_size=vocab_size, window_size=10) for wid in wids]
# view sample skip-grams
pairs = skip_grams[0]
labels = skip_grams[1]
for i in range(10):
print("({:s} ({:d}), {:s} ({:d})) -> {:d}".format(id2word[pairs[i][0]], pairs[i][0],id2word[pairs[i][1]], pairs[i][1],labels[i]))
это должно привести к выводу, подобному этому:
(james (1154), king (13)) -> 1
(king (13), james (1154)) -> 1
(james (1154), perform (1249)) -> 0
(bible (5766), dismissed (6274)) -> 0
, но вывод, который я получаю, следующий:
pairs = skip_grams[0]
IndexError: list index out of range