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(sdict, total)
function weightedrandompick(dict, total)
n = rand(1:total)
n = rand(1:total)
for key in keys(sdict)
for key in keys(dict)
n -= sdict[key]
n -= dict[key]
if n <= 0
if n <= 0
return key
return key
Line 76: Line 76:
end
end
end
end
followsums = SortedDict(key => sum(values(follows[key])) for key in keys(follows))
followsums = Dict(key => sum(values(follows[key])) for key in keys(follows))
follow2sums = SortedDict(key => sum(values(follows2[key])) for key in keys(follows2))
follow2sums = Dict(key => sum(values(follows2[key])) for key in keys(follows2))
afterstopsum = sum(values(afterstop))
afterstopsum = sum(values(afterstop))