I start out with a list of word counts:
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
Then I want to compute the no. of ngrams per word and store it in a nested dictionary. The outer key would be the ngram and the inner key the word and inner-most value is the count of the ngram given the word.
I've tried:
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
And that gives me the data structure I need:
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)
But notice that the values are wrong:
('t','h') => Dict("the"=>1)
('h','e') => Dict("the"=>1)
should have been:
('t','h') => Dict("the"=>2)
('h','e') => Dict("the"=>2)
Since the word the appeared twice.
After a closer look, it seems like the haskey(ngram_word_counter, ng)
is always false =(
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
Why is this haskey()
condition always false?
Aucun commentaire:
Enregistrer un commentaire