How to one-hot-encode sentences at the character level?

前端 未结 5 938
-上瘾入骨i
-上瘾入骨i 2021-01-15 15:16

I would like to convert a sentence to an array of one-hot vector. These vector would be the one-hot representation of the alphabet. It would look like the following:

5条回答
  •  长情又很酷
    2021-01-15 15:37

    Just compare the letters in your passed string to a given alphabet:

    def string_vectorizer(strng, alphabet=string.ascii_lowercase):
        vector = [[0 if char != letter else 1 for char in alphabet] 
                      for letter in strng]
        return vector
    

    Note that, with a custom alphabet (e.g. "defbcazk", the columns will be ordered as each element appears in the original list).

    The output of string_vectorizer('hello'):

    [[0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
    

提交回复
热议问题