Execute a Markov algorithm: Difference between revisions

no edit summary
(add Ada)
No edit summary
Line 1,077:
in if terminating then new else f rules new
else g (a : before) as</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
 
<lang unicon>procedure main(A)
rules := loadRules(open(A[1],"r"))
every write(line := !&input, " -> ",apply(rules, line))
end
 
record rule(pat, term, rep)
 
procedure loadRules(f)
static WS
initial WS := ' \t'
rules := []
every !f ? if not ="#" then put(rules,
rule(1(trim(tab(find("->"))),move(2),tab(many(WS))),
(="."|&null), trim(tab(0))))
return rules
end
 
procedure apply(rules, line)
s := line
repeat {
s ?:= tab(find((r := !rules).pat)) || r.rep || (move(*r.pat),tab(0))
if (s == line) | \r.term then return s else line := s
}
end</lang>
 
Sample runs using above rule sets and test strings:
<pre>
->ma mars.1
I bought a B of As from T S.
I bought a B of As from T S. -> I bought a bag of apples from my brother.
->ma mars.2
I bought a B of As from T S.
I bought a B of As from T S. -> I bought a bag of apples from T shop.
->ma mars.3
I bought a B of As W my Bgage from T S.
I bought a B of As W my Bgage from T S. -> I bought a bag of apples with my money from T shop.
->ma mars.4
_1111*11111_
_1111*11111_ -> 11111111111111111111
->ma mars.5
000000A000000
000000A000000 -> 00011H1111000
</pre>
 
=={{header|J}}==