Random sentence from book: Difference between revisions
Content added Content deleted
Line 15: | Line 15: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
<lang julia>""" weighted random pick of items in a Dict{String, Int} where keys are words, values counts """ |
<lang julia>""" weighted random pick of items in a Dict{String, Int} where keys are words, values counts """ |
||
function weightedrandompick( |
function weightedrandompick(dict, total) |
||
n = rand(1:total) |
n = rand(1:total) |
||
for key in keys( |
for key in keys(dict) |
||
n -= |
n -= dict[key] |
||
if n <= 0 |
if n <= 0 |
||
return key |
return key |
||
Line 76: | Line 76: | ||
end |
end |
||
end |
end |
||
followsums = |
followsums = Dict(key => sum(values(follows[key])) for key in keys(follows)) |
||
follow2sums = |
follow2sums = Dict(key => sum(values(follows2[key])) for key in keys(follows2)) |
||
afterstopsum = sum(values(afterstop)) |
afterstopsum = sum(values(afterstop)) |
||