Execute a Markov algorithm: Difference between revisions

Content added Content deleted
No edit summary
m (→‎{{header|REXX}}: added/changed whitespace and comments.)
Line 3,023: Line 3,023:
Code was added to the REXX example to optionally list the contents of the ruleset and/or the Markov entries.
Code was added to the REXX example to optionally list the contents of the ruleset and/or the Markov entries.
<br>Also, blank lines in the ruleset were treated as comments.
<br>Also, blank lines in the ruleset were treated as comments.
<lang rexx>/*REXX pgm to execute a Markov algorithm(s) against specified entries.*/
<lang rexx>/*REXX program executes a Markov algorithm(s) against specified entries. */
parse arg low high . /*allow which ruleset to process.*/
parse arg low high . /*allows which ruleset to process. */
if low=='' | low==',' then low=1 /*assume a default if none given.*/
if low=='' | low==',' then low=1 /*Not specified? Then use the default.*/
if high=='' | high==',' then high=6 /*assume a default if none given.*/
if high=='' | high==',' then high=6 /* " " " " " " */
tellE = low<0; tellR = high<0 /*flags: display file contents. */
tellE=low<0; tellR=high<0 /*flags: used to display file contents.*/
call readEntry
call readEntry
do j=abs(low) to abs(high) /*process each of these rulesets.*/
do j=abs(low) to abs(high) /*process each of these rulesets. */
call readRules j /*read a particular ruleset. */
call readRules j /*read a particular ruleset. */
call execRules j /*execute " " " */
call execRules j /*execute " " " */
say 'result for ruleset' j"≡"!.j
say 'result for ruleset' j"≡"!.j
end /*j*/
end /*j*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*───────────────────────────────EXECRULES──────────────────────────────*/
execRules: parse arg q .; if tellE | tellR then say
execRules: parse arg q .; if tellE | tellR then say /*a blank line*/
do f=1 /* forever */
do f=1
do k=1 while @.k\==''; if left(@.k,1)=='#' | @.k='' then iterate
do k=1 while @.k\==''; if left(@.k,1)=='#' | @.k='' then iterate
parse var @.k a ' ->' b; a=strip(a); b=strip(b)
parse var @.k a ' ->' b; a=strip(a); b=strip(b)
fullstop= left(b,1)=='.' /*is this a fullstop rule? */
fullstop= left(b,1)=='.' /*is this a "fullstop" rule? */
if fullstop then b=substr(b,2) /*purify the B part of the rule. */
if fullstop then b=substr(b,2) /*purify the B part of the rule. */
old=!.q /*remember value before change. */
old=!.q /*remember the value before the change.*/
!.q=changestr(a, !.q, b) /*implement the ruleset change. */
!.q=changestr(a, !.q, b) /*implement the ruleset change. */
if fullstop then if old\==!.q then return /*should we stop?*/
if fullstop then if old\==!.q then return /*should we stop? */
if old\==!.q then iterate f /*Entry changed? Then start over*/
if old\==!.q then iterate f /*Has Entry changed? Then start over.*/
end /*k*/
end /*k*/
leave
leave
end /*f*/
end /*f*/
return
return
/*────────────────────────────────────────────────────────────────────────────*/
/*───────────────────────────────READRULES──────────────────────────────*/
readRules: parse arg ? .; rFID='MARKOV_R.'?; if tellR then say
readRules: parse arg ? .; rFID='MARKOV_R.'?; if tellR then say
@.= /*placeholder: all Markov rules.*/
@.= /*placeholder for all the Markov rules.*/
do r=1 while lines(rFID)\==0 /*read the input file until E-O-F*/
do r=1 while lines(rFID)\==0 /*read the input file until End-Of-File*/
@.r=linein(rFID); if tellR then say 'ruleSet' ?"."left(r,4)'≡'@.r
@.r=linein(rFID); if tellR then say 'ruleSet' ?"."left(r,4)'≡'@.r
end /*r*/ /*(above) read and maybe echo it.*/
end /*r*/ /*(above) read and maybe echo the rule.*/
return
return
/*────────────────────────────────────────────────────────────────────────────*/
/*───────────────────────────────READENTRY──────────────────────────────*/
readEntry: eFID='MARKOV.ENT'; if tellE then say
readEntry: eFID='MARKOV.ENT'; if tellE then say
!.= /*placeholder: all test entries.*/
!.= /*placeholder for all the test entries.*/
do e=1 while lines(eFID)\==0 /*read the input file until E-O-F*/
do e=1 while lines(eFID)\==0 /*read the input file until End-Of-File*/
!.e=linein(eFID); if tellE then say 'test entry' e"≡"!.e
!.e=linein(eFID); if tellE then say 'test entry' e"≡"!.e
end /*e*/ /*(above) read and maybe echo it.*/
end /*e*/ /*(above) read and maybe echo the entry*/
return</lang>
return</lang>
Some older REXXes don't have a '''changestr''' bif, so one is included here ──► [[CHANGESTR.REX]].
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]].
<br><br>
<br><br>
'''output''' when using the default input and files:
'''output''' &nbsp; when using the default input and files:
<pre>
<pre>
result for ruleset 1≡I bought a bag of apples from my brother.
result for ruleset 1≡I bought a bag of apples from my brother.