Random sentence from book: Difference between revisions

m
minor cleanup
m (simplify Dictionary creation)
m (minor cleanup)
Line 320:
function weightedrandompick(dict, total)
n = rand(1:total)
for (key, value) in keys(dict)
n -= dict[key]value
if n <= 0
return key
Line 347:
end
 
""" Keep account of what words follow words and how many times it is seen. Treat sentence terminators
(Treat sentence terminators?.!) as words too). Keep account of what words follow two words and how many times it is seen.
and how many times it is seen, (again treating sentence terminators as words too).
"""
follows, follows2 = Dict{String, Dict{String, Int}}(), Dict{String, Dict{String, Int}}()
Line 371 ⟶ 370:
afterstopsum = sum(values(afterstop))
 
""" Assume that a sentence starts with a not to be shown full-stop character then use a weighted
"""
Assume that a sentence startsrandom withchoice aof notthe topossible bewords shownthat may follow a full-stop characterto add to the sentence.
then use a weighted random choice of the possible words that may follow a
full-stop to add to the sentence. (Here we use '.', '?', or '!' for the full stop character.)
"""
function makesentence()
Line 390 ⟶ 387:
println(join(sentencewords[1:end-1], " ") * sentencewords[end] * "\n")
end
# Print 3 weighted random pick sentences
 
makesentence(); makesentence(); makesentence()
end
4,103

edits