Execute a Markov algorithm: Difference between revisions
Content added Content deleted
Not a robot (talk | contribs) (Add SETL) |
|||
Line 5,437: | Line 5,437: | ||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|SETL}}== |
|||
<syntaxhighlight lang="setl">program markov_algorithm; |
|||
magic := false; |
|||
if command_line(1) = om then |
|||
print("error: no ruleset file given"); |
|||
stop; |
|||
elseif command_line(2) = om then |
|||
print("error: no input string given"); |
|||
stop; |
|||
end if; |
|||
rules := read_file(command_line(1)); |
|||
input := command_line(2); |
|||
loop do |
|||
loop for [pat, repl, trm] in rules do |
|||
if pat in input then |
|||
input(pat) := repl; |
|||
if trm then |
|||
quit; |
|||
else |
|||
continue loop do; |
|||
end if; |
|||
end if; |
|||
end loop; |
|||
quit; |
|||
end loop; |
|||
print(input); |
|||
proc read_file(file_name); |
|||
if (rulefile := open(file_name, "r")) = om then |
|||
print("error: cannot open ruleset file"); |
|||
stop; |
|||
end if; |
|||
rules := []; |
|||
loop doing |
|||
line := getline(rulefile); |
|||
while line /= om do |
|||
rule := parse_rule(line); |
|||
if rule /= om then rules with:= rule; end if; |
|||
end loop; |
|||
return rules; |
|||
end proc; |
|||
proc parse_rule(rule); |
|||
if rule(1) = "#" then return om; end if; $ comment |
|||
if " -> " notin rule then return om; end if; $ not a rule |
|||
[s, e] := mark(rule, " -> "); |
|||
pattern := rule(..s-1); |
|||
repl := rule(e+1..); |
|||
whitespace := "\t\r\n "; |
|||
span(pattern, whitespace); |
|||
rspan(pattern, whitespace); |
|||
span(repl, whitespace); |
|||
rspan(repl, whitespace); |
|||
trm := match(repl, ".") /= ""; |
|||
return [pattern, repl, trm]; |
|||
end proc; |
|||
end program;</syntaxhighlight> |
|||
{{out}} |
|||
<pre>$ setl markov.setl ruleset1.mkv "I bought a B of As from T S." |
|||
I bought a bag of apples from my brother. |
|||
$ setl markov.setl ruleset2.mkv "I bought a B of As from T S." |
|||
I bought a bag of apples from T shop. |
|||
$ setl markov.setl ruleset3.mkv "I bought a B of As W my Bgage from T S." |
|||
I bought a bag of apples with my money from T shop. |
|||
$ setl markov.setl ruleset4.mkv "_1111*11111_" |
|||
11111111111111111111 |
|||
$ setl markov.setl ruleset5.mkv "000000A000000" |
|||
00011H1111000</pre> |
|||
=={{header|SNOBOL4}}== |
=={{header|SNOBOL4}}== |