Random sentence from book: Difference between revisions
Content added Content deleted
m (→{{header|Phix}}: added personal tag) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 20: | Line 20: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}} |
||
< |
<syntaxhighlight lang="algol68"># generate random sentences using text from a book as a basis # |
||
# use the associative array in the Associate array/iteration task # |
# use the associative array in the Associate array/iteration task # |
||
Line 290: | Line 290: | ||
FI; |
FI; |
||
close( input file ) |
close( input file ) |
||
FI</ |
FI</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Sample output produced with the command-line: |
Sample output produced with the command-line: |
||
Line 322: | Line 322: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">""" weighted random pick of items in a Dict{String, Int} where keys are words, values counts """ |
||
function weightedrandompick(dict, total) |
function weightedrandompick(dict, total) |
||
n = rand(1:total) |
n = rand(1:total) |
||
Line 395: | Line 395: | ||
makesentence(); makesentence(); makesentence() |
makesentence(); makesentence(); makesentence() |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
(RUN:) |
(RUN:) |
||
Line 439: | Line 439: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
Inspired by Julia solution, but not a translation actually. |
Inspired by Julia solution, but not a translation actually. |
||
< |
<syntaxhighlight lang="nim">import random, sequtils, strutils, tables |
||
from unicode import utf8 |
from unicode import utf8 |
||
Line 533: | Line 533: | ||
else: |
else: |
||
weightedChoice(followCount[lastWord], followSum[lastWord]) |
weightedChoice(followCount[lastWord], followSum[lastWord]) |
||
echo sentence.join(" ") & lastWord</ |
echo sentence.join(" ") & lastWord</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 552: | Line 552: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
< |
<syntaxhighlight lang="perl">#!/usr/bin/perl |
||
use strict; # https://rosettacode.org/wiki/Random_sentence_from_book |
use strict; # https://rosettacode.org/wiki/Random_sentence_from_book |
||
Line 584: | Line 584: | ||
} |
} |
||
print sentence() for 1 .. 10;</ |
print sentence() for 1 .. 10;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 614: | Line 614: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
{{libheader|Phix/libcurl}} |
{{libheader|Phix/libcurl}} |
||
<!--< |
<!--<syntaxhighlight lang="phix">(notonline)--> |
||
<span style="color: #000080;font-style:italic;">-- demo/rosetta/RandomSentence.exw</span> |
<span style="color: #000080;font-style:italic;">-- demo/rosetta/RandomSentence.exw</span> |
||
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">libcurl</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">libcurl</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> |
||
Line 690: | Line 690: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span> |
||
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span> |
<span style="color: #0000FF;">{}</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">wait_key</span><span style="color: #0000FF;">()</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 703: | Line 703: | ||
Extended to preserve some extra "sentence pausing" characters and try and tidy-up apostrophes. |
Extended to preserve some extra "sentence pausing" characters and try and tidy-up apostrophes. |
||
< |
<syntaxhighlight lang="python">from urllib.request import urlopen |
||
import re |
import re |
||
from string import punctuation |
from string import punctuation |
||
Line 770: | Line 770: | ||
#%% |
#%% |
||
sentence = gen_sentence(word2next, word2next2) |
sentence = gen_sentence(word2next, word2next2) |
||
print(sentence)</ |
print(sentence)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 790: | Line 790: | ||
Started out as translation of [[Random_sentence_from_book#Perl|Perl]], but diverged. |
Started out as translation of [[Random_sentence_from_book#Perl|Perl]], but diverged. |
||
<lang |
<syntaxhighlight lang="raku" line>my $text = '36-0.txt'.IO.slurp.subst(/.+ '*** START OF THIS' .+? \n (.*?) 'End of the Project Gutenberg EBook' .*/, {$0} ); |
||
$text.=subst(/ <+punct-[.!?\’,]> /, ' ', :g); |
$text.=subst(/ <+punct-[.!?\’,]> /, ' ', :g); |
||
Line 817: | Line 817: | ||
} |
} |
||
say sentence() ~ "\n" for ^10;</ |
say sentence() ~ "\n" for ^10;</syntaxhighlight> |
||
{{out|Sample output}} |
{{out|Sample output}} |
||
<pre>To the inhabitants calling itself the Committee of Public Supply seized the opportunity of slightly shifting my position, which had caused a silent mass of smoke rose slanting and barred the face. |
<pre>To the inhabitants calling itself the Committee of Public Supply seized the opportunity of slightly shifting my position, which had caused a silent mass of smoke rose slanting and barred the face. |
||
Line 841: | Line 841: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{libheader|Wren-seq}} |
{{libheader|Wren-seq}} |
||
< |
<syntaxhighlight lang="ecmascript">import "io" for File |
||
import "random" for Random |
import "random" for Random |
||
import "/seq" for Lst |
import "/seq" for Lst |
||
Line 932: | Line 932: | ||
System.print(sentence) |
System.print(sentence) |
||
System.print() |
System.print() |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |