我从一个单词计数列表开始:
julia> import Iterators: partition
julia> import StatsBase: countmap
julia> s = split("the lazy fox jumps over the brown dog");
julia> vocab_counter = countmap(s)
Dict{SubString{String},Int64} with 7 entries:
"brown" => 1
"lazy" => 1
"jumps" => 1
"the" => 2
"fox" => 1
"over" => 1
"dog" => 1
然后我想计算一下。每个单词的ngrams并将其存储在嵌套字典中。外键是ngram,内键是单词,而最内层的值是给出单词的ngram的计数。
我试过了:
ngram_word_counter = Dict{Tuple,Dict}()
for (word, count) in vocab_counter
for ng in ngram(word, 2) # bigrams.
if ! haskey(ngram_word_counter, ng)
ngram_word_counter[ng] = Dict{String,Int64}()
ngram_word_counter[ng][word] = 0
end
ngram_word_counter[ng][word] += 1
end
end
这给了我需要的数据结构:
julia> ngram_word_counter
Dict{Tuple,Dict} with 20 entries:
('b','r') => Dict("brown"=>1)
('t','h') => Dict("the"=>1)
('o','w') => Dict("brown"=>1)
('z','y') => Dict("lazy"=>1)
('o','g') => Dict("dog"=>1)
('u','m') => Dict("jumps"=>1)
('o','x') => Dict("fox"=>1)
('e','r') => Dict("over"=>1)
('a','z') => Dict("lazy"=>1)
('p','s') => Dict("jumps"=>1)
('h','e') => Dict("the"=>1)
('d','o') => Dict("dog"=>1)
('w','n') => Dict("brown"=>1)
('m','p') => Dict("jumps"=>1)
('l','a') => Dict("lazy"=>1)
('o','v') => Dict("over"=>1)
('v','e') => Dict("over"=>1)
('r','o') => Dict("brown"=>1)
('f','o') => Dict("fox"=>1)
('j','u') => Dict("jumps"=>1)
但请注意,这些值是错误的:
('t','h') => Dict("the"=>1)
('h','e') => Dict("the"=>1)
应该是:
('t','h') => Dict("the"=>2)
('h','e') => Dict("the"=>2)
因为这个词出现了两次。
仔细观察后,似乎haskey(ngram_word_counter, ng)
总是假=(
julia> ngram_word_counter = Dict{Tuple,Dict}()
for (word, count) in vocab_counter
for ng in ngram(word, 2) # bigrams.
println(haskey(ngram_word_counter, ng))
end
end
[OUT]:
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
false
为什么这个haskey()
条件总是错误的?
答案 0 :(得分:5)
TL; DR:它应该是ngram_word_counter[ng][word] += count
而不是ngram_word_counter[ng][word] += 1
。
仅添加1
会忽略多次出现的多个贡献。单词出现的次数以vocab_counter
值编码,这些值进入count
循环中的变量for
。因此,增量应为count
。
后面的调试检查无效,并且通常情况下,调试代码的错误会混淆问题。预定的检查可能是:
julia> ngram_word_counter = Dict{Tuple,Dict}()
for (word, count) in vocab_counter
for ng in ngram(word, 2) # bigrams.
println(haskey(ngram_word_counter, ng))
ngram_word_counter[ng] = 1
end
end