Write a function called get_sentence_one_hot that, given a s…
Write a function called get_sentence_one_hot that, given a sentence (represented as a list of word tokens) and a vocabulary (a unique list of tokens/words), returns a list of one-hot encoded vectors, where each word in the sentence is represented by a one-hot encoding based on the vocabulary. In this representation: Each word in the sentence is encoded as a one-hot vector, where the index corresponding to that word in the vocabulary is set to 1, and all other indices are set to 0. If a word from the sentence does not appear in the vocabulary, it should be encoded as a vector of all zeros. Example: words = [“cat”, “dog”, “cat”, “zebra”]vocabulary = [“cat”, “dog”, “parrot”]get_sentence_one_hot(words, vocabulary) = [ np.array([1, 0, 0]), # “cat” is the first word in the vocabulary np.array([0, 1, 0]), # “dog” is the second word in the vocabulary np.array([1, 0, 0]), # “cat” again np.array([0, 0, 0]) # “zebra” is not in the vocabulary]
Read Details