MCPcopy Create free account
hub / github.com/rushter/MLAlgorithms / load_nietzsche

Function load_nietzsche

mla/datasets/base.py:58–78  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

56
57
58def load_nietzsche():
59 text = open(get_filename("data/nietzsche.txt"), "rt").read().lower()
60 chars = set(list(text))
61 char_indices = {ch: i for i, ch in enumerate(chars)}
62 indices_char = {i: ch for i, ch in enumerate(chars)}
63
64 maxlen = 40
65 step = 3
66 sentences = []
67 next_chars = []
68 for i in range(0, len(text) - maxlen, step):
69 sentences.append(text[i : i + maxlen])
70 next_chars.append(text[i + maxlen])
71
72 X = np.zeros((len(sentences), maxlen, len(chars)), dtype=np.bool)
73 y = np.zeros((len(sentences), len(chars)), dtype=np.bool)
74 for i, sentence in enumerate(sentences):
75 for t, char in enumerate(sentence):
76 X[i, t, char_indices[char]] = 1
77 y[i, char_indices[next_chars[i]]] = 1
78 return X, y, text, chars, char_indices, indices_char

Callers 1

Calls 1

get_filenameFunction · 0.85

Tested by

no test coverage detected