Random sentence from book: Difference between revisions

m
simplify Dictionary creation
(Added Algol 68)
m (simplify Dictionary creation)
Line 348:
 
""" Keep account of what words follow words and how many times it is seen.
(Treat sentence terminators as words too). Keep account of what words follow two words
and how many times it is seen, (again treating sentence terminators as words too).
"""
Line 354:
afterstop, wlen = Dict{String, Int}(), length(words)
for (i, w) in enumerate(@view words[1:end-1])
ifd = get!haskey(follows, w, Dict(words[i + 1] => 0))
get!(d, follows[w] = Dict(words[i + 1], => 10)
elsed[words[i + 1]] += 1
if w in if haskey(follows[w]".", words[i +"?", 1"!"])
d = get!(afterstop, follows[w][words[i + 1]], += 10)
elseafterstop[words[i + 1]] += 1
follows[w][words[i + 1]] = 1
end
end
(i > wlen - 2) && continue
w2 = w * " " * words[i + 1]
ifd = get!haskey(follows2, w2, Dict(words[i + 2] => 0))
get!(d, follows2[w2] = Dict(words[i + 2], => 10)
elsed[words[i + 2]] += 1
if haskey(follows2[w2], words[i + 2])
follows2[w2][words[i + 2]] += 1
else
follows2[w2][words[i + 2]] = 1
end
end
if w in [".", "?", "!"]
if !haskey(afterstop, words[i + 1])
afterstop[words[i + 1]] = 1
else
afterstop[words[i + 1]] += 1
end
end
end
followsums = Dict(key => sum(values(follows[key])) for key in keys(follows))
Line 403 ⟶ 388:
end
sentencewords[1] = uppercase(firstword[1]) * (length(firstword) > 1 ? firstword[2:end] : "")
println(replace(join(sentencewords[1:end-1], " ") * sentencewords[end] * "\n", " i " => " I "))
end
 
Line 422 ⟶ 407:
 
(RUN:)
 
It was this cylinder.
 
4,103

edits