listing deep learning for text and sequences word-level one-hot encoding ( oy examplebuilds an index of all tokens in the data initial dataone entry per sample (in this examplea sample is sentencebut it could be an entire documenttokenizes the samples via the split method in real lifeyou' also strip punctuation and special characters from the samples import numpy as np samples ['the cat sat on the mat ''the dog ate my homework 'token_index {for sample in samplesfor word in sample split()if word not in token_indextoken_index[wordlen(token_index max_length results np zeros(shape=(len(samples)max_lengthmax(token_index values() )for isample in enumerate(samples)for jword in list(enumerate(sample split()))[:max_length]index token_index get(wordresults[ijindex this is where you store the results assigns unique index to each unique word note that you don' attribute index to anything listing vectorizes the samples you'll only consider the first max_length words in each sample charact er-level one-hot encoding (toy exampleimport string samples ['the cat sat on the mat ''the dog ate my homework 'characters string printable token_index dict(zip(range( len(characters )characters)max_length results np zeros((len(samples)max_lengthmax(token_index keys() )for isample in enumerate(samples)for jcharacter in enumerate(sample)index token_index get(characterall printable ascii results[ijindex characters note that keras has built-in utilities for doing one-hot encoding of text at the word level or character levelstarting from raw text data you should use these utilitiesbecause they take care of number of important features such as stripping special characters from strings and only taking into account the most common words in your dataset ( common restrictionto avoid dealing with very large input vector spaceslicensed to