Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/drupal/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Julia中从char转换为string?_Julia - Fatal编程技术网

如何在Julia中从char转换为string?

如何在Julia中从char转换为string?,julia,Julia,我正在尝试使用一个简单的词汇表将一个单词转换为一个热编码数组的数组。我编的这本字典是用字母键编成的 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_on

我正在尝试使用一个简单的词汇表将一个单词转换为一个热编码数组的数组。我编的这本字典是用字母键编成的

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)
不幸的是,这会返回一个错误,因为
char\u id
Dict is使用char键而不是字符串。如何将字典转换为使用字符串值作为键,或将字符转换为字符串以使比较匹配

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

字符串已经可以看作是字符的集合,因此您不需要拆分单词

但是,
map
的特殊化方式是,在字符串上只能映射返回字符的函数。广播系统也将字符串视为标量。这给我们留下了一些选择:一个简单的
for
循环,或者一个生成器/理解

我想在这种情况下,我会理解:

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)
    [char_to_one_hot(char, char_id, max_length) for char in word]
end
我想这是你所期待的:

julia> vocab = "abc"
"abc"

julia> char_id = Dict([ (index, char) for (char, index) in enumerate(vocab) ])
Dict{Char,Int64} with 3 entries:
  'a' => 1
  'c' => 3
  'b' => 2

julia> word_to_one_hot("acb", char_id, 5)
3-element Array{Array{Float64,1},1}:
 [1.0, 0.0, 0.0, 0.0, 0.0]
 [0.0, 0.0, 1.0, 0.0, 0.0]
 [0.0, 1.0, 0.0, 0.0, 0.0]


如果仍要在单字符字符串和字符之间转换,可以通过以下方式进行转换:

julia> str="a"; first(str)
'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)

julia> chr='a'; string(chr)
"a"

要将长度为1的字符串转换为字符,请使用[1]引用该字符串的第一个字符。 要将字符转换为字符串,请使用string()

julia> s = "c"
"c"

julia> s[1]
'c': ASCII/Unicode U+0063 (category Ll: Letter, lowercase)

julia> string(s)
"c"