2017-04-03 61 views
2

鉴于计数在文本单词的countmap对象:同时总结在字典{元组,字典{字符串,Int64类型}}内的字典中单个环路

vocab_counter = countmap(split("the lazy fox jumps over the brown dog")) 

[OUT]:

Dict{SubString{String},Int64} with 7 entries: 
    "brown" => 1 
    "lazy" => 1 
    "jumps" => 1 
    "the" => 2 
    "fox" => 1 
    "over" => 1 
    "dog" => 1 

而进入一个人物二元计数器,每字:

ngram_word_counter = Dict{Tuple,Dict}() 
for (word, count) in vocab_counter 
    for ng in ngrams(word, n) # bigrams. 
     if ! haskey(ngram_word_counter, ng) || ! haskey(ngram_word_counter[ng], word) 
      ngram_word_counter[ng] = Dict{String,Int64}() 
      ngram_word_counter[ng][word] = 0 
     end 
     ngram_word_counter[ng][word] += count 
    end 
end 

[ngram_word_counter]:

Dict{Tuple,Dict} with 20 entries: 
    ('b','r') => Dict("brown"=>1) 
    ('t','h') => Dict("the"=>2) 
    ('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"=>2) 
    ('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) 

随着Dict{Tuple, Dict{String,Int64}}对象,我需要重新循环的ngram_word_counter得到ngram_counter无字,即Dict{Tuple,Int64}

ngram_counter = Dict{Tuple,Int64}() 
for ng in keys(ngram_word_counter) 
    ngram_counter[ng] = sum(values(ngram_word_counter[ng])) 
end 

[ngram_counter]:

Dict{Tuple,Int64} with 20 entries: 
    ('b','r') => 1 
    ('t','h') => 2 
    ('o','w') => 1 
    ('z','y') => 1 
    ('o','g') => 1 
    ('u','m') => 1 
    ('o','x') => 1 
    ('e','r') => 1 
    ('a','z') => 1 
    ('p','s') => 1 
    ('h','e') => 2 
    ('d','o') => 1 
    ('w','n') => 1 
    ('m','p') => 1 
    ('l','a') => 1 
    ('o','v') => 1 
    ('v','e') => 1 
    ('r','o') => 1 
    ('f','o') => 1 
    ('j','u') => 1 

目前,为了得到这两个对象,我可以做一个特设第二计数:

function compute_statistics(vocab_counter, n) 
    ngram_word_counter = Dict{Tuple,Dict}() 
    for (word, count) in vocab_counter 
     for ng in ngrams(word, n) # bigrams. 
      if ! haskey(ngram_word_counter, ng) || ! haskey(ngram_word_counter[ng], word) 
       ngram_word_counter[ng] = Dict{String,Int64}() 
       ngram_word_counter[ng][word] = 0 
      end 
      ngram_word_counter[ng][word] += count 
     end 
    end 
    ngram_counter = Dict{Tuple,Int64}() 
    for ng in keys(ngram_word_counter) 
     ngram_counter[ng] = sum(values(ngram_word_counter[ng])) 
    end 
    return ngram_word_counter, ngram_counter 
end 

或同时更新两个ngram_word_counterngram_counter在第一循环:

function compute_statistics(vocab_counter, n) 
    ngram_word_counter = Dict{Tuple,Dict}() 
    ngram_counter = Dict{Tuple,Int64}() 
    for (word, count) in vocab_counter 
     for ng in ngrams(word, n) # bigrams. 
      if ! haskey(ngram_word_counter, ng) || ! haskey(ngram_word_counter[ng], word) 
       ngram_word_counter[ng] = Dict{String,Int64}() 
       ngram_word_counter[ng][word] = 0 
      end 
      ngram_word_counter[ng][word] += count 
      ngram_counter[ng] += 1 
     end 
    end 
    return ngram_word_counter, ngram_counter 
end 

ngram_word_counter, ngram_counter 

但我发现了一个KeyError,更新ngram_counter时:

KeyError: key ('b','r') not found 

我已经添加了额外的检查和它的工作原理:

function compute_statistics(vocab_counter, n) 
    ngram_word_counter = Dict{Tuple,Dict}() 
    ngram_counter = Dict{Tuple,Int64}() 
    for (word, count) in vocab_counter 
     for ng in ngrams(word, n) # bigrams. 
      if ! haskey(ngram_word_counter, ng) || ! haskey(ngram_word_counter[ng], word) 
       ngram_word_counter[ng] = Dict{String,Int64}() 
       ngram_word_counter[ng][word] = 0 
      end 
      if !haskey(ngram_counter, ng) 
       ngram_counter[ng] = 0 
      end 
      ngram_word_counter[ng][word] += count 
      ngram_counter[ng] += 1 
     end 
    end 
    return ngram_word_counter, ngram_counter 
end 

ngram_word_counter, ngram_counter 

[o UT]:

(Dict{Tuple,Dict}(Pair{Tuple,Dict}(('b','r'),Dict("brown"=>1)),Pair{Tuple,Dict}(('t','h'),Dict("the"=>2)),Pair{Tuple,Dict}(('o','w'),Dict("brown"=>1)),Pair{Tuple,Dict}(('z','y'),Dict("lazy"=>1)),Pair{Tuple,Dict}(('o','g'),Dict("dog"=>1)),Pair{Tuple,Dict}(('u','m'),Dict("jumps"=>1)),Pair{Tuple,Dict}(('o','x'),Dict("fox"=>1)),Pair{Tuple,Dict}(('e','r'),Dict("over"=>1)),Pair{Tuple,Dict}(('a','z'),Dict("lazy"=>1)),Pair{Tuple,Dict}(('p','s'),Dict("jumps"=>1))…),Dict{Tuple,Int64}(Pair{Tuple,Int64}(('b','r'),1),Pair{Tuple,Int64}(('t','h'),1),Pair{Tuple,Int64}(('o','w'),1),Pair{Tuple,Int64}(('z','y'),1),Pair{Tuple,Int64}(('o','g'),1),Pair{Tuple,Int64}(('u','m'),1),Pair{Tuple,Int64}(('o','x'),1),Pair{Tuple,Int64}(('e','r'),1),Pair{Tuple,Int64}(('a','z'),1),Pair{Tuple,Int64}(('p','s'),1)…)) 

有没有办法同时总结在快译通{元组,快译通{字符串,Int64的}}内的字典在一个循环?

回答

4

不知道这个答案,但你可以让compute_statistics清洁如下:

function compute_statistics(vocab_counter, n) 
    ngram_word_counter = Dict{Tuple,Dict{String,Int}}() 
    ngram_counter = Dict{Tuple,Int}() 
    for (word, count) in vocab_counter, ng in ngrams(word,n) 
     ngram_word_counter[ng] = get(ngram_word_counter,ng,Dict{String,Int}()) 
     ngram_word_counter[ng][word] = get(ngram_word_counter[ng],word,0)+count 
     ngram_counter[ng] = get(ngram_counter,ng,0)+count 
    end 
    return ngram_word_counter, ngram_counter 
end 

(这使用get避免haskey和更短的for语法)

另一种方式来获得ngram_counterngram_word_counter计算得出:

ngram_counter = map(x->x[1]=>sum(values(x[2])),ngram_word_counter) 

ngram_counter = Dict(k=>sum(values(d)) for (k,d) in ngram_word_counter) 
+0

糟糕。迷惑'getkey'与'get',但它现在已经修复 –