Random sentence from book: Difference between revisions
Content added Content deleted
No edit summary |
|||
Line 493: | Line 493: | ||
</pre> |
</pre> |
||
=={{header|Phix}}== |
|||
{{libheader|Phix/libcurl}} |
|||
<lang Phix>-- demo/rosetta/RandomSentence.exw |
|||
include builtins\libcurl.e |
|||
constant url = "http://www.gutenberg.org/files/36/36-0.txt", |
|||
filename = "war_of_the_worlds.txt", |
|||
fsent = "No one would have believed", |
|||
lasts = "End of the Project Gutenberg EBook", |
|||
unicodes = {utf32_to_utf8({#2019}), -- rsquo |
|||
utf32_to_utf8({#2014})}, -- hyphen |
|||
asciis = {"'","-"}, |
|||
aleph = tagset('Z','A')&tagset('z','a')&tagset('9','0')&",.?! \n", |
|||
follow = new_dict(), -- {word} -> {words,counts} |
|||
follow2 = new_dict() -- {word,word} -> {words,counts} |
|||
if not file_exists(filename) then |
|||
printf(1,"Downloading %s...\n",{filename}) |
|||
CURLcode res = curl_easy_get_file(url,"",filename) |
|||
if res!=CURLE_OK then crash("cannot download") end if |
|||
end if |
|||
string text = get_text(filename) |
|||
text = text[match(fsent,text)..match(lasts,text)-1] |
|||
text = substitute_all(text,unicodes,asciis) |
|||
text = substitute_all(text,".?!-\n",{" ."," ? "," ! "," "," "}) |
|||
text = filter(text,"in",aleph) |
|||
sequence words = split(text) |
|||
procedure account(sequence words) |
|||
string next = words[$] |
|||
words = words[1..$-1] |
|||
for i=length(words) to 1 by -1 do |
|||
integer d = {follow,follow2}[i] |
|||
sequence t = getdd(words,{{},{}},d) |
|||
integer tk = find(next,t[1]) |
|||
if tk=0 then |
|||
t[1] = append(t[1],next) |
|||
t[2] = append(t[2],1) |
|||
else |
|||
t[2][tk] += 1 |
|||
end if |
|||
setd(words,t,d) |
|||
words = words[2..$] |
|||
if words!={"."} then exit end if -- (may as well quit) |
|||
end for |
|||
end procedure |
|||
for i=2 to length(words)-1 do |
|||
if find(words[i],{".","?","!"}) then |
|||
words[i+1] = lower(words[i+1]) |
|||
end if |
|||
account(words[max(1,i-2)..i]) |
|||
end for |
|||
function weighted_random_pick(sequence words, integer dict) |
|||
sequence t = getd(words,dict) |
|||
integer total = sum(t[2]), |
|||
r = rand(total) |
|||
for i=1 to length(t[2]) do |
|||
r -= t[2][i] |
|||
if r<=0 then |
|||
return t[1][i] |
|||
end if |
|||
end for |
|||
end function |
|||
for i=1 to 5 do |
|||
sequence sentence = {".",weighted_random_pick({"."},follow)} |
|||
while true do |
|||
string next = weighted_random_pick(sentence[-2..-1],follow2) |
|||
sentence = append(sentence,next) |
|||
if find(next,{".","?","!"}) then exit end if |
|||
end while |
|||
sentence[2][1] = upper(sentence[2][1]) |
|||
printf(1,"%s\n",{join(sentence[2..$-1])&sentence[$]}) |
|||
end for |
|||
{} = wait_key()</lang> |
|||
{{out}} |
|||
<pre> |
|||
With one another by means of a speck of blight, and apparently strengthened the walls of the spectators had gathered in one cart stood a blind man in the direction of Chobham. |
|||
I fell and lay about our feet. |
|||
And we were driving down Maybury Hill. |
|||
It was with the arms of an engine. |
|||
Now we see further. |
|||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |