I'm trying to convert a word to an array of one-hot encoded arrays using a simple vocabulary. The dictionary I've constructed is keyed off of characters.
vocab = "abc"
char_id = Dict([ (index, char) for (char, index) in enumerate(vocab) ])
# Dict{Char,Int64} with 3 entries:
# 'a' => 1
# 'c' => 3
# 'b' => 2
function char_to_one_hot(char, char_id, max_length)
one_hot = zeros(max_length)
setindex!(one_hot, 1.0, char_id[char])
end
function word_to_one_hot(word, char_id, max_length)
map((char) -> char_to_one_hot(char, char_id, max_length), split(word, ""))
end
word_to_one_hot(word, char_id, max_length)
Unfortunately, this returns an error because the char_id Dict is uses char keys instead of strings. How can I convert either the dictionary to use string values as keys, or chars to strings so the comparison matches?
ERROR: KeyError: key "a" not found
Stacktrace:
[1] getindex at ./dict.jl:467 [inlined]
[2] char_to_one_hot(::SubString{String}, ::Dict{Char,Int64}, ::Int64) at ./REPL[456]:3
[3] #78 at ./REPL[457]:2 [inlined]
[4] iterate at ./generator.jl:47 [inlined]
[5] _collect(::Array{SubString{String},1}, ::Base.Generator{Array{SubString{String},1},var"#78#79"{Dict{Char,Int64},Int64}}, ::Base.EltypeUnknown, ::Base.HasShape{1}) at ./array.jl:699
[6] collect_similar at ./array.jl:628 [inlined]
[7] map at ./abstractarray.jl:2162 [inlined]
[8] word_to_one_hot(::String, ::Dict{Char,Int64}, ::Int64) at ./REPL[457]:2
[9] top-level scope at REPL[458]:1