Markov chain text generator: Difference between revisions
Content deleted Content added
SqrtNegInf (talk | contribs) Added Perl example |
→{{header|REXX}}: added the REXX computer programming language. |
||
Line 906: | Line 906: | ||
you're a humbug? asked Dorothy. A balloon, said Oz, for I have no right to command them |
you're a humbug? asked Dorothy. A balloon, said Oz, for I have no right to command them |
||
once</pre> |
once</pre> |
||
=={{header|REXX}}== |
|||
<lang rexx>/*REXX program produces a Markov chain text from a training text using a text generator.*/ |
|||
parse arg ord fin iFID seed . /*obtain optional arguments from the CL*/ |
|||
if ord=='' | ord=="," then ord= 3 /*Not specified? Then use the default.*/ |
|||
if fin=='' | fin=="," then fin= 300 /* " " " " " " */ |
|||
if iFID=='' |iFID=="," then iFID='alice_oz.txt' /* " " " " " " */ |
|||
if datatype(seed, 'W') then call random ,,seed /* " " " " " " */ |
|||
sw = linesize() - 1 /*get usable linesize (screen width). */ |
|||
$= space( linein(iFID) ) /*elide any superfluous whitespace in $*/ |
|||
say words($) ' words read from input file: ' iFID |
|||
call gTab /*generate the Markov chain text table.*/ |
|||
call gTxt /*generate the Markov chain text. */ |
|||
call show /*display formatted output and a title.*/ |
|||
exit /*stick a fork in it, we're all done. */ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
rand: parse arg ?; return random(1, ?) /*gen a random number from 1 to arg.*/ |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
gTab: @.=; do j=1 for words($)-ord /*keep processing until words exhausted*/ |
|||
p= subword($, j, ord) /*get the appropriate number of words. */ |
|||
@.p= @.p word($, j + ord) /*get a prefix & 1 (of sev.?) suffixes.*/ |
|||
end /*j*/ |
|||
#= j-1 /*define the number of prefixes. */ |
|||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
gTxt: mc=; do until words(mc)>=fin /*build Markov chain text until enough.*/ |
|||
y= subword($, rand(#), ord) /*obtain appropriate number of words. */ |
|||
s= @.y; w= words(s) /*get a suffix for a word set; # wprds.*/ |
|||
if w>1 then s= word(s, rand(w)) /*pick random word in the set of words.*/ |
|||
mc= mc y s /*add a prefix and suffix to the output*/ |
|||
end /*until*/ |
|||
return |
|||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
|||
show: say center('Markov chain text', sw, "═") /*display the title for the output. */ |
|||
g= word(mc, 1) /*generate lines of Markov chain text. */ |
|||
do k=2 to words(mc) /*build output lines word by word. */ |
|||
_= word(mc, k); g_= g _ /*get a word; add it to a temp variable*/ |
|||
if length(g_)>=sw then do; say g; g= _; end /*line too long ? */ |
|||
else g= g_ /*line OK so far. */ |
|||
end /*k*/ |
|||
if g\=='' then say g /*There any residual? Then display it.*/ |
|||
return /* [↓] limits G to terminal width.*/</lang> |
|||
{{out|output|text= when using the default inputs:}} |
|||
<pre> |
|||
════════════════════════════════════════════════════════Markov chain text════════════════════════════════════════════════════════ |
|||
took Toto in her but her eyes were puzzled her very much to be ashamed of a Woodman, and made idea what you're talking know. So, |
|||
while they And oh, my poor journey. This worried Dorothy wag my tail when cats!' cried the Mouse, like to cry a She did not get |
|||
she found to be I do not want truck was all ready the Lion's back. To she might as well and four nights, hammering his leg, and |
|||
was dance?' 'Thank you, it's Em, and said, We who lived in the the same when I the gate the Lion down with one elbow he had |
|||
seen. They a reasonable pace,' said nearly blunted my claws, next; so she took terrible Beast. It was the Scarecrow, having no |
|||
know,' the Mock Turtle his Palace. But first the Scarecrow, What will From the Land of I know. Well, one Land of Oz, how cloth, |
|||
and put it both sides of it, those needles and pins was, how to get To their great joy the Mouse was bristling an M, such as not |
|||
to make personal we leave her here the King, rubbing his back again, and looking took a great interest she looked back once |
|||
smooth, like the surface silk dress she had curious sensation, which puzzled he gave so loud blinded by the glare except Toto, |
|||
and taking hill. So he said, and shepherds dressed in her nicked elbow close Oz. First they came indeed, said the man. nor do I |
|||
know about it, so she a morsel of the little people so they I am a very the limbs of trees, the Queen, pointing to the Woodman |
|||
both shook had happened; and Toto settled down again, the Cap. Willingly! exclaimed Dorothy; on a pole in houses in this country |
|||
</pre> |
|||
=={{header|Swift}}== |
=={{header|Swift}}== |