Markov chain text generator: Difference between revisions

m
→‎{{header|REXX}}: elided the need for the RAND function, changed a comment..
(Edited my Python to conform to standards a bit better.)
m (→‎{{header|REXX}}: elided the need for the RAND function, changed a comment..)
Line 920:
sw = linesize() - 1 /*get usable linesize (screen width). */
$= space( linein(iFID) ) /*elide any superfluous whitespace in $*/
$$= $ /*save a copy after $ is destructed. */
say words($) ' words read from input file: ' iFID
call gTab /*generate the Markov chain text table.*/
Line 926 ⟶ 925:
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($, 1j, ord) /*obtainget the appropriate number of words. */
parse@.p= var@.p $ . word($, j + ord) /*deleteget firsta word,prefix no& longer1 needed(of sev.?) suffixes.*/
@.p= @.p word($, ord) /*build prefix & 1 (of sev.?) suffixes.*/
end /*j*/
#= j-1 /*define the number of prefixes. */
Line 938 ⟶ 934:
/*──────────────────────────────────────────────────────────────────────────────────────*/
gTxt: mc=; do until words(mc)>=fin /*build Markov chain text until enough.*/
y= subword($$, randrandom(1, #), 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, randrandom(1,w)) /*pick random word in the set of words.*/
mc= mc y s /*add a prefix and suffix to the output*/
end /*until*/