Jump to content

Random sentence from book: Difference between revisions

Line 12:
 
Show examples of random sentences generated.
 
=={{header|Julia}}==
<lang julia>""" weighted random pick of items in a SortedDict{String, Int} where keys are words, values counts """
function weightedrandompick(sdict, total)
n = rand(1:total)
for key in keys(sdict)
n -= sdict[key]
if n <= 0
return key
end
end
return last(keys(dict))
end
 
let
""" Read in the book "The War of the Worlds", by H. G. Wells. """
wotw_uri = "http://www.gutenberg.org/files/36/36-0.txt"
wfile = "war_of_the_worlds.txt"
stat(wfile).size == 0 && download(wotw_uri, wfile) # download if file not here already
text = read(wfile, String)
 
"""skip to start of book and prune end """
startphrase, endphrase = "No one would have believed", "she has counted me, among the dead"
text = text[findfirst(startphrase, text).start:findlast(endphrase, text).stop]
 
""" Remove extraneous punctuation, but keep at least sentence-ending punctuation characters . ! and ? """
text = replace(replace(lowercase(text), r"[^01-9a-zA-Z\.\?\!\']" => " "), r"([.?!])" => s" \1")
words = split(text, r"\s+")
 
""" 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).
"""
follows, follows2 = Dict{String, Dict{String, Int}}(), Dict{String, Dict{String, Int}}()
afterstop, wlen = Dict{String, Int}(), length(words)
for (i, w) in enumerate(@view words[1:end-1])
if !haskey(follows, w)
follows[w] = Dict(words[i + 1] => 1)
else
if haskey(follows[w], words[i + 1])
follows[w][words[i + 1]] += 1
else
follows[w][words[i + 1]] = 1
end
end
(i > wlen - 2) && continue
w2 = w * " " * words[i + 1]
if !haskey(follows2, w2)
follows2[w2] = Dict(words[i + 2] => 1)
else
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 = SortedDict(key => sum(values(follows[key])) for key in keys(follows))
follow2sums = SortedDict(key => sum(values(follows2[key])) for key in keys(follows2))
afterstopsum = sum(values(afterstop))
 
"""
Assume that a sentence starts with a not to be shown full-stop character
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()
firstword = weightedrandompick(afterstop, afterstopsum)
sentencewords = [firstword, weightedrandompick(follows[firstword], followsums[firstword])]
while !(sentencewords[end] in [".", "?", "!"])
w2 = sentencewords[end-1] * " " * sentencewords[end]
if haskey(follows2, w2)
push!(sentencewords, weightedrandompick(follows2[w2], follow2sums[w2]))
else
push!(sentencewords, weightedrandompick(afterstop, afterstopsum))
end
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
 
makesentence(); makesentence(); makesentence()
end
</lang>{{out}}
<pre>
(RUN:)
 
It may be lying dead there!
 
I can imagine them covered with smashed windows and saw the flashes of flame flashed up
and saw through a culvert.
 
I remember how mockingly bright the sky was still doubtful it rapped smartly against the
starlight from the sun blazed dazzling in a flash I was beginning to face these things
but later I perceived a hold on me and rapidly growing hotter.
 
(RUN:)
 
A cheese in the goods yard there ploughed through shrieking people and a blue jersey.
 
They were heads merely heads.
 
I heard a faint movement under my feet.
 
(RUN:)
 
Survivors on castle hill alive but helplessly and speechlessly drunk.
 
Before they were killed.
 
The landlord should leave his.
</pre>
 
=={{header|Python}}==
4,105

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.