Execute a Markov algorithm: Difference between revisions

Add Refal
(Add APL)
(Add Refal)
(16 intermediate revisions by 8 users not shown)
Line 149:
: <code> 00011H1111000 </code>
<br><br>
 
=={{header|11l}}==
{{trans|Nim}}
 
<syntaxhighlight lang="11l">T Rule
String pattern
String replacement
Bool terminating
F (pattern, replacement, terminating)
.pattern = pattern
.replacement = replacement
.terminating = terminating
 
F parse(rules)
[Rule] result
L(line) rules.split("\n")
I line.starts_with(‘#’)
L.continue
I line.trim(‘ ’).empty
L.continue
 
V (pat, rep) = line.split(‘ -> ’)
 
V terminating = 0B
I rep.starts_with(‘.’)
rep = rep[1..]
terminating = 1B
 
result.append(Rule(pat, rep, terminating))
R result
 
F apply(text, rules)
V result = text
V changed = 1B
 
L changed == 1B
changed = 0B
L(rule) rules
I rule.pattern C result
result = result.replace(rule.pattern, rule.replacement)
I rule.terminating
R result
changed = 1B
L.break
 
R result
 
V SampleTexts = [‘I bought a B of As from T S.’,
‘I bought a B of As from T S.’,
‘I bought a B of As W my Bgage from T S.’,
‘_1111*11111_’,
‘000000A000000’]
 
V RuleSets = [
‘# This rules file is extracted from Wikipedia:
# http://en.wikipedia.org/wiki/Markov_Algorithm
A -> apple
B -> bag
S -> shop
T -> the
the shop -> my brother
a never used -> .terminating rule’,
 
‘# Slightly modified from the rules on Wikipedia
A -> apple
B -> bag
S -> .shop
T -> the
the shop -> my brother
a never used -> .terminating rule’,
 
‘# BNF Syntax testing rules
A -> apple
WWWW -> with
Bgage -> ->.*
B -> bag
->.* -> money
W -> WW
S -> .shop
T -> the
the shop -> my brother
a never used -> .terminating rule’,
 
‘### Unary Multiplication Engine, for testing Markov Algorithm implementations
### By Donal Fellows.
# Unary addition engine
_+1 -> _1+
1+1 -> 11+
# Pass for converting from the splitting of multiplication into ordinary
# addition
1! -> !1
,! -> !+
_! -> _
# Unary multiplication by duplicating left side, right side times
1*1 -> x,@y
1x -> xX
X, -> 1,1
X1 -> 1X
_x -> _X
,x -> ,X
y1 -> 1y
y_ -> _
# Next phase of applying
1@1 -> x,@y
1@_ -> @_
,@_ -> !_
++ -> +
# Termination cleanup for addition
_1 -> 1
1+_ -> 1
_+_ -> ’,
 
‘# Turing machine: three-state busy beaver
#
# state A, symbol 0 => write 1, move right, new state B
A0 -> 1B
# state A, symbol 1 => write 1, move left, new state C
0A1 -> C01
1A1 -> C11
# state B, symbol 0 => write 1, move left, new state A
0B0 -> A01
1B0 -> A11
# state B, symbol 1 => write 1, move right, new state B
B1 -> 1B
# state C, symbol 0 => write 1, move left, new state B
0C0 -> B01
1C0 -> B11
# state C, symbol 1 => write 1, move left, halt
0C1 -> H01
1C1 -> H11’]
 
L(ruleset) RuleSets
V rules = parse(ruleset)
print(apply(SampleTexts[L.index], rules))</syntaxhighlight>
 
{{out}}
<pre>
I bought a bag of apples from my brother.
I bought a bag of apples from T shop.
I bought a bag of apples with my money from T shop.
11111111111111111111
00011H1111000
</pre>
 
=={{header|Ada}}==
markov.ads:
<langsyntaxhighlight Adalang="ada">with Ada.Strings.Unbounded;
 
package Markov is
Line 177 ⟶ 320:
Entries : Entry_Array (1 .. Length);
end record;
end Markov;</langsyntaxhighlight>
 
markov.adb:
<langsyntaxhighlight Adalang="ada">package body Markov is
 
function Parse (S : String_Array) return Ruleset is
Line 260 ⟶ 403:
end Apply;
 
end Markov;</langsyntaxhighlight>
 
test_markov.adb:
<langsyntaxhighlight Adalang="ada">with Ada.Command_Line;
with Ada.Text_IO.Unbounded_IO;
with Ada.Strings.Unbounded;
Line 310 ⟶ 453:
end;
end;
end Test_Markov;</langsyntaxhighlight>
 
Output (rulesX contains the ruleset of above examples and testX the example text):
Line 326 ⟶ 469:
=={{header|APL}}==
{{works with|Dyalog APL}}
<langsyntaxhighlight APLlang="apl">markov←{
trim←{(~(∧\∨⌽∘(∧\)∘⌽)⍵∊⎕UCS 9 32)/⍵}
rules←(~rules∊⎕UCS 10 13)⊆rules←80 ¯1 ⎕MAP ⍺
Line 345 ⟶ 488:
}
rules apply ⍵
}</langsyntaxhighlight>
{{out}}
<pre> 'f:\ruleset1.mkv' markov 'I bought a B of As from T S.'
Line 359 ⟶ 502:
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight lang="autohotkey">;---------------------------------------------------------------------------
; Markov Algorithm.ahk
; by wolf_II
Line 712 ⟶ 855:
 
 
;---------- end of file ----------------------------------------------------</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
<langsyntaxhighlight lang="bbcbasic"> PRINT FNmarkov("ruleset1.txt", "I bought a B of As from T S.")
PRINT FNmarkov("ruleset2.txt", "I bought a B of As from T S.")
PRINT FNmarkov("ruleset3.txt", "I bought a B of As W my Bgage from T S.")
Line 749 ⟶ 892:
UNTIL EOF#rules% OR done%
CLOSE #rules%
= text$</langsyntaxhighlight>
'''Output:'''
<pre>
Line 761 ⟶ 904:
=={{header|Bracmat}}==
Save the following text to a file "markov.bra":
<langsyntaxhighlight lang="bracmat">
markov=
{
Line 976 ⟶ 1,119:
& ok
| failure;
</syntaxhighlight>
</lang>
 
Test:
Line 995 ⟶ 1,138:
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Line 1,197 ⟶ 1,340:
 
return 0;
}</langsyntaxhighlight>output<syntaxhighlight lang="text">Rules from 'rule1' ok
text: I bought a B of As from T S.
markoved: I bought a bag of apples from my brother.
Line 1,216 ⟶ 1,359:
text: 000000A000000
markoved: 00011H1111000
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
Note: Non-use of <code>iswhite</code> is intentional, since depending on the locale, other chars besides space and tab might be detected by that function.
<langsyntaxhighlight lang="cpp">
#include <cstdlib>
#include <iostream>
Line 1,333 ⟶ 1,476:
 
std::cout << output << "\n";
}</langsyntaxhighlight>
 
=={{header|CLU}}==
<syntaxhighlight lang="clu">markov = cluster is make, run
rule = struct[from, to: string, term: bool]
rep = array[rule]
% Remove leading and trailing whitespace from a string
trim = proc (s: string) returns (string)
ac = array[char]
sc = sequence[char]
own ws: string := "\n\t "
a: ac := string$s2ac(s)
while ~ac$empty(a) cand string$indexc(ac$bottom(a), ws) ~= 0 do
ac$reml(a)
end
while ~ac$empty(a) cand string$indexc(ac$top(a), ws) ~= 0 do
ac$remh(a)
end
return(string$sc2s(sc$a2s(a)))
end trim
% Parse a single Markov rule
parse = proc (s: string) returns (rule) signals (comment, invalid(string))
if string$empty(s) cor s[1]='#' then signal comment end
arrow: int := string$indexs(" -> ", s)
if arrow=0 then signal invalid(s) end
left: string := trim(string$substr(s, 1, arrow-1))
right: string := trim(string$rest(s, arrow+4))
if ~string$empty(right) cand right[1] = '.' then
right := string$rest(right, 2)
return(rule${from: left, to: right, term: true})
else
return(rule${from: left, to: right, term: false})
end
end parse
% Add a rule to the list
add_rule = proc (m: cvt, s: string) signals (invalid(string))
rep$addh(m, parse(s)) resignal invalid
except when comment: end
end add_rule
% Read rules in sequence from a stream
add_rules = proc (m: cvt, s: stream) signals (invalid(string))
while true do
add_rule(up(m), stream$getl(s)) resignal invalid
except when end_of_file: break end
end
end add_rules
make = proc (s: stream) returns (cvt) signals (invalid(string))
a: rep := rep$new()
add_rules(up(a), s)
return(a)
end make
% Apply a rule to a string
apply_rule = proc (r: rule, s: string) returns (string) signals (no_match)
match: int := string$indexs(r.from, s)
if match = 0 then signal no_match end
new: string := string$substr(s, 1, match-1)
|| r.to
|| string$rest(s, match+string$size(r.from))
return(new)
end apply_rule
% Apply all rules to a string repeatedly
run = proc (c: cvt, s: string) returns (string)
i: int := 1
while i <= rep$high(c) do
r: rule := c[i]
begin
s := apply_rule(r, s)
i := 1
if r.term then break end
end except when no_match:
i := i+1
end
end
return(s)
end run
end markov
 
start_up = proc ()
po: stream := stream$primary_output()
eo: stream := stream$error_output()
begin
args: sequence[string] := get_argv()
file: string := args[1]
input: string := args[2]
fs: stream := stream$open(file_name$parse(file), "read")
mkv: markov := markov$make(fs)
stream$close(fs)
stream$putl(po, markov$run(mkv, input))
end except
when bounds: stream$putl(eo, "Arguments: markov [filename] [string]")
when not_possible(s: string): stream$putl(eo, "File error: " || s)
when invalid(s: string): stream$putl(eo, "Parse error: " || s)
end
end start_up</syntaxhighlight>
{{out}}
<pre>$ ./markov ruleset1.mkv "I bought a B of As from T S."
I bought a bag of apples from my brother.
 
$ ./markov ruleset2.mkv "I bought a B of As from T S."
I bought a bag of apples from T shop.
 
$ ./markov 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.
 
$ ./markov ruleset4.mkv "_1111*11111_"
11111111111111111111
 
$ ./markov ruleset5.mkv "000000A000000"
00011H1111000</pre>
 
=={{header|Common Lisp}}==
I should mention that this uses the regular expression machinery present in Allegro Lisp but not Common Lisp generally (though there are public domain Lisp libraries).
<langsyntaxhighlight lang="lisp">;;; Keeps track of all our rules
(defclass markov ()
((rules :initarg :rules :initform nil :accessor rules)))
Line 1,404 ⟶ 1,664:
(setf ret (adjust rule-info ret))
(if (terminal (car rule-info)) (return ret))
(setf rule-info (get-rule markov ret)))))</langsyntaxhighlight>
Testing:
<syntaxhighlight lang="text">(defparameter
*rules1*
"# This rules file is extracted from Wikipedia:
Line 1,433 ⟶ 1,693:
00011H1111000
NIL
</syntaxhighlight>
</lang>
 
=={{header|Cowgol}}==
<langsyntaxhighlight lang="cowgol">include "cowgol.coh";
include "strings.coh";
include "malloc.coh";
Line 1,632 ⟶ 1,892:
 
print(ApplyRules(ReadFile(fname), &patbuf[0]));
print_nl();</langsyntaxhighlight>
{{out}}
<pre>$ ./markov.386 ruleset1.mkv "I bought a B of As from T S."
Line 1,647 ⟶ 1,907:
=={{header|D}}==
{{trans|Perl}}
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.file, std.regex, std.string, std.range,
std.functional;
Line 1,679 ⟶ 1,939:
writefln("%s\n%s\n", origTest, test);
}
}</langsyntaxhighlight>
{{out}}
<pre>I bought a B of As from T S.
Line 1,698 ⟶ 1,958:
=={{header|Déjà Vu}}==
This implementation expect the initial text on the command line and the ruleset on STDIN.
<langsyntaxhighlight lang="dejavu">(remove-comments) text:
]
for line in text:
Line 1,740 ⟶ 2,000:
not (markov-tick) rules
 
!. markov markov-parse get-from !args 1</langsyntaxhighlight>
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
;; rule := (pattern replacement [#t terminal])
 
Line 1,777 ⟶ 2,037:
(define (task i-string RS)
(markov i-string (parse-rules RS)))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,814 ⟶ 2,074:
=={{header|F_Sharp|F#}}==
<p>Using Partial Active Pattern to simplify pattern matching.</p>
<langsyntaxhighlight lang="fsharp">open System
open System.IO
open System.Text.RegularExpressions
Line 1,866 ⟶ 2,126:
|> run
|> printfn "%s"
0</langsyntaxhighlight>
{{out}}
<pre>H:\RosettaCode\ExecMarkovAlgo>echo I bought a B of As from T S. | Fsharp\RosettaCode\bin\Debug\RosettaCode.exe m1
Line 1,884 ⟶ 2,144:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 2,059 ⟶ 2,319:
`00011H1111000`,
},
}</langsyntaxhighlight>
{{out}}
<pre>
Line 2,067 ⟶ 2,327:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def markovInterpreterFor = { rules ->
def ruleMap = [:]
rules.eachLine { line ->
Line 2,091 ⟶ 2,351:
text
}]
}</langsyntaxhighlight>
The test code is below (with the markov rulesets 2..5 elided):
<langsyntaxhighlight lang="groovy">def verify = { ruleset ->
[withInput: { text ->
[hasOutput: { expected ->
Line 2,125 ⟶ 2,385:
 
def ruleset5 = markovInterpreterFor("""...""")
verify ruleset5 withInput '000000A000000' hasOutput '00011H1111000'</langsyntaxhighlight>
{{out}}
<pre>
Line 2,139 ⟶ 2,399:
This program expects a source file as an argument and uses the standard input and output devices for the algorithm's I/O.
 
<langsyntaxhighlight lang="haskell">import Data.List (isPrefixOf)
import Data.Maybe (catMaybes)
import Control.Monad
Line 2,181 ⟶ 2,441:
then let new = reverse before ++ to ++ drop (length from) ahead
in if terminating then new else f rules new
else g (a : before) as</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
 
<langsyntaxhighlight lang="unicon">procedure main(A)
rules := loadRules(open(A[1],"r"))
every write(line := !&input, " -> ",apply(rules, line))
Line 2,206 ⟶ 2,466:
if (s == line) | \r.term then return s else line := s
}
end</langsyntaxhighlight>
 
Sample runs using above rule sets and test strings:
Line 2,228 ⟶ 2,488:
 
=={{header|J}}==
'''Solution''':<langsyntaxhighlight lang="j">require'strings regex'
 
markovLexer =: verb define
Line 2,257 ⟶ 2,517:
end.
y
)</langsyntaxhighlight>
 
'''Example''':<langsyntaxhighlight lang="j"> m1 =. noun define
# This rules file is extracted from Wikipedia:
# http://en.wikipedia.org/wiki/Markov_Algorithm
Line 2,272 ⟶ 2,532:
m1 markov 'I bought a B of As from T S.'
I bought a bag of apples from my brother.
</syntaxhighlight>
</lang>
'''Discussion''': The J implementation correctly processes all the rulesets. More details are available on the [[Talk:Markov Algorithm#explicit_vs_tacit|the talk page]].
 
Line 2,278 ⟶ 2,538:
{{trans|D}}
{{works with|Java|7}}
<langsyntaxhighlight lang="java">import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Line 2,340 ⟶ 2,600:
return rules;
}
}</langsyntaxhighlight>
 
Output:
Line 2,360 ⟶ 2,620:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">/**
* Take a ruleset and return a function which takes a string to which the rules
* should be applied.
Line 2,519 ⟶ 2,779:
console.log(markov(ruleset3)('I bought a B of As W my Bgage from T S.'));
console.log(markov(ruleset4)('_1111*11111_'));
console.log(markov(ruleset5)('000000A000000'));</langsyntaxhighlight>
Output:
<pre>I bought a bag of apples from my brother.
Line 2,526 ⟶ 2,786:
11111111111111111111
00011H1111000</pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
This entry assumes that the rule sets are in a single file (markov_rules.txt);
that the rule sets are separated by a blank line;
and that the corresponding test cases are in a separate file (markov_tests.txt), with one test case per line.
In addition, for simplicity, only blanks are counted as <whitespace>.
 
With the following program, jq could then be invoked as follows:
<pre>
jq -nrR --rawfile markov_rules markov_rules.txt -f program.jq markov_tests.txt
</pre>
 
'''Preliminaries'''
<syntaxhighlight lang="jq"># Output: the input string with all its regex-special characters suitably escaped
def deregex:
reduce ("\\\\", "\\*", "\\^", "\\?", "\\+", "\\.", "\\!", "\\{", "\\}", "\\[", "\\]", "\\$", "\\|",
"\\(", "\\)" ) as $c
(.; gsub( $c; $c));
 
# line-break
def lb: "\n";
 
def split2($s):
index($s) as $ix
| if $ix then [ .[:$ix], .[$ix + ($s|length):]] else null end;
 
def trim: sub("^ *";"") | sub(" *$";"");
 
# rulesets are assumed to be separated by a blank line
# input: a string
def readRules:
trim | split("\(lb)\(lb)") | map(split(lb)) ;
 
# tests are assumed to be on consecutive lines via `inputs`
# Output: an array
def readTests: [inputs | trim | select(length>0) ];
 
def rules: $markov_rules | readRules;
 
def tests: readTests;</syntaxhighlight>
 
<syntaxhighlight lang="jq">def parseRules($rules):
"^ *(?<period>[.]?) *(?<rule>.*)" as $pattern
| reduce $rules[] as $rule ([];
if $rule | (startswith("#") or (test(" -> ")|not)) then .
else ($rule|split2(" -> ")) as $splits
| ($splits[1] | capture($pattern)) as $re
| . + [[($splits[0]|trim|deregex), $re.period, ($re.rule | trim)]]
end );
 
# applyRules applies $rules to . recursively,
# where $rules is the set of applicable rules in the form of an array-of-triples.
# Input and output: a string
def applyRules($rules):
# The inner function has arity-0 for efficiency
# input and output: {stop, string}
def apply:
if .stop then .
else .string as $copy
| first( foreach $rules[] as $c (.;
.string |= sub($c[0]; $c[2])
| if $c[1] == "."
then .stop=true
elif .string != $copy
then (apply | .stop = true)
else .
end;
if .stop then . else empty end))
// .
end;
{stop: false, string: .} | apply | .string;
 
def proceed:
rules as $rules
| tests as $tests
| range(0; $tests|length) as $ix
| $tests[$ix]
| " \(.)\n=>\(applyRules( parseRules( $rules[$ix] ) ))\n" ;
 
proceed</syntaxhighlight>
 
{{out}}
<pre>
I bought a B of As from T S.
=>I bought a bag of apples from my brother.
 
I bought a B of As from T S.
=>I bought a bag of apples from T shop.
 
I bought a B of As W my Bgage from T S.
=>I bought a bag of apples with my money from T shop.
 
_1111*11111_
=>11111111111111111111
 
000000A000000
=>00011H1111000
</pre>
 
=={{header|Julia}}==
Line 2,531 ⟶ 2,892:
 
'''Module''':
<langsyntaxhighlight lang="julia">module MarkovAlgos
 
struct MarkovRule{F,T}
Line 2,578 ⟶ 2,939:
end
 
end # module MarkovAlgos</langsyntaxhighlight>
 
'''Main''':
<langsyntaxhighlight lang="julia">include("module.jl")
 
let rulesets = @.("data/markovrules0" * string(1:5) * ".txt"),
Line 2,591 ⟶ 2,952:
println("Transformed:\n", MarkovAlgos.apply(ruletest[i], rules))
end
end</langsyntaxhighlight>
 
{{out}}
Line 2,631 ⟶ 2,992:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">// version 1.1.51
 
import java.io.File
Line 2,674 ⟶ 3,035:
println("$origTest\n$test\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,695 ⟶ 3,056:
 
=={{header|Lua}}==
<langsyntaxhighlight lang="lua">-- utility method to escape punctuation
function normalize(str)
local result = str:gsub("(%p)", "%%%1")
Line 2,879 ⟶ 3,240:
do_markov(grammar3, text2, 'I bought a bag of apples with my money from T shop.')
do_markov(grammar4, text3, '11111111111111111111')
do_markov(grammar5, text4, '00011H1111000')</langsyntaxhighlight>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">markov[ruleset_, text_] :=
Module[{terminating = False, output = text,
rules = StringCases[
Line 2,893 ⟶ 3,254:
output = StringReplace[output, rule[[1]] -> rule[[2]]];
If[! rule[[3]], terminating = False]; Break[]], {rule, rules}]];
output];</langsyntaxhighlight>
Example:
<langsyntaxhighlight lang="mathematica">markov["# Turing machine: three-state busy beaver
#
# state A, symbol 0 => write 1, move right, new state B
Line 2,912 ⟶ 3,273:
# state C, symbol 1 => write 1, move left, halt
0C1 -> H01
1C1 -> H11", "000000A000000"]</langsyntaxhighlight>
Output:
<pre>"00011H1111000"</pre>
 
=={{header|МК-61/52}}==
<syntaxhighlight lang="text"> 9 П4
КИП4 [x] П7 Вx {x} П8
ИП8 ИПE * П8 {x} x=0 08
Line 2,937 ⟶ 3,298:
ИП3 ИПE / [x] П3
x=0 22
ИП4 ИП0 - 9 - x=0 02 С/П</langsyntaxhighlight>
 
Under the rules of left 4 registers, under the word has 8 character cells, the alphabet of the digits from 1 to 8. Rules are placed in "123,456", where "123" is a fragment, and "456" is to be replaced, in the registers of the РA to РD. The number of rules is stored in Р0, the initial word is in Р9. Number triggered rule is the last digit registration Р4 (0 to 3), if no rule did not work, the indicator 0, otherwise the current word to be processed. In РE is stored 10.
 
=={{header|Nim}}==
<langsyntaxhighlight Nimlang="nim">import strutils, strscans
 
type Rule = object
Line 3,094 ⟶ 3,455:
for n, ruleset in RuleSets:
let rules = ruleset.parse()
echo SampleTexts[n].apply(rules)</langsyntaxhighlight>
 
{{out}}
Line 3,107 ⟶ 3,468:
I'm not familiar with string processing, or parsing, so there are probably better ways to express this in OCaml. One might be with the mikmatch library which allows pattern-matching with regexps. Here I've only used the OCaml stdlib...
 
<langsyntaxhighlight OCamllang="ocaml">(* Useful for resource cleanup (such as filehandles) *)
let try_finally x f g =
try let res = f x in g x; res
Line 3,154 ⟶ 3,515:
print_endline (run rules (input_line stdin));
translate ()
in try translate () with End_of_file -> ()</langsyntaxhighlight>
 
With the above compiled to an executable 'markov', and the five rule-sets in files, strings are accepted on stdin for translation:
Line 3,177 ⟶ 3,538:
000000A000000
00011H1111000
</pre>
 
=={{header|Pascal}}==
{{works with|FPC}}
<syntaxhighlight lang="pascal">
program InterpretMA;
{$mode objfpc}{$h+}{$j-}{$b-}
uses
SysUtils;
 
type
TRule = record
Pattern, Replacement: string;
Terminating: Boolean;
end;
 
function ParseMA(const aScheme: string; out aRules: specialize TArray<TRule>): Boolean;
function ParseLine(const s: string; out r: TRule): Boolean;
var
Terms: TStringArray;
begin
Terms := s.Split([' -> ']);
if Length(Terms) <> 2 then exit(False);
r.Pattern := Terms[0].Trim;
r.Replacement := Terms[1].Trim;
r.Terminating := False;
if (r.Replacement <> '') and (r.Replacement[1] = '.') then begin
r.Terminating := True;
Delete(r.Replacement, 1, 1);
end;
Result := True;
end;
var
Lines: TStringArray;
s: string;
I: Integer;
begin
aRules := nil;
if aScheme = '' then exit(False);
Lines := aScheme.Split([LineEnding], TStringSplitOptions.ExcludeEmpty);
if Lines = nil then exit(False);
SetLength(aRules, Length(Lines));
I := 0;
for s in Lines do begin
if s[1] = '#' then continue;
if not ParseLine(s, aRules[I]) then exit(False);
Inc(I);
end;
SetLength(aRules, I);
Result := True;
end;
 
function ExecuteMA(const aScheme, aInput: string): string;
var
Rules: array of TRule;
r: TRule;
Applied: Boolean;
begin
if not ParseMA(aScheme.Replace(#9, ' ', [rfReplaceAll]), Rules) then
exit('Error while parsing MA scheme');
Result := aInput;
repeat
Applied := False;
for r in Rules do begin
if r.Pattern = '' then begin
Result := r.Replacement + Result;
Applied := True;
end else begin
Applied := Result.IndexOf(r.Pattern) >= 0;
if Applied then
Result := Result.Replace(r.Pattern, r.Replacement);
end;
if Applied then begin
if r.Terminating then exit;
break;
end;
end;
until not Applied;
end;
 
type
TTestEntry = record
Scheme, Input, Output: string;
end;
 
const
LE = LineEnding;
TestSet: array[1..5] of TTestEntry = (
(Scheme:
'# This rules file is extracted from Wikipedia: ' +LE+
'# http://en.wikipedia.org/wiki/Markov_Algorithm' +LE+
'A -> apple' +LE+
'B -> bag' +LE+
'S -> shop' +LE+
'T -> the' +LE+
'the shop -> my brother' +LE+
'a never used -> .terminating rule';
Input: 'I bought a B of As from T S.'; Output: 'I bought a bag of apples from my brother.'),
(Scheme:
'# Slightly modified from the rules on Wikipedia' +LE+
'A -> apple' +LE+
'B -> bag' +LE+
'S -> .shop' +LE+
'T -> the' +LE+
'the shop -> my brother' +LE+
'a never used -> .terminating rule';
Input: 'I bought a B of As from T S.'; Output: 'I bought a bag of apples from T shop.'),
(Scheme:
'# BNF Syntax testing rules' +LE+
'A -> apple' +LE+
'WWWW -> with' +LE+
'Bgage -> ->.*' +LE+
'B -> bag' +LE+
'->.* -> money' +LE+
'W -> WW' +LE+
'S -> .shop' +LE+
'T -> the' +LE+
'the shop -> my brother' +LE+
'a never used -> .terminating rule';
Input: 'I bought a B of As W my Bgage from T S.'; Output: 'I bought a bag of apples with my money from T shop.'),
(Scheme:
'### Unary Multiplication Engine, for testing Markov Algorithm implementations' +LE+
'### By Donal Fellows.' +LE+
'# Unary addition engine' +LE+
'_+1 -> _1+' +LE+
'1+1 -> 11+' +LE+
'# Pass for converting from the splitting of multiplication into ordinary' +LE+
'# addition' +LE+
'1! -> !1' +LE+
',! -> !+' +LE+
'_! -> _' +LE+
'# Unary multiplication by duplicating left side, right side times' +LE+
'1*1 -> x,@y' +LE+
'1x -> xX' +LE+
'X, -> 1,1' +LE+
'X1 -> 1X' +LE+
'_x -> _X' +LE+
',x -> ,X' +LE+
'y1 -> 1y' +LE+
'y_ -> _' +LE+
'# Next phase of applying' +LE+
'1@1 -> x,@y' +LE+
'1@_ -> @_' +LE+
',@_ -> !_' +LE+
'++ -> +' +LE+
'# Termination cleanup for addition' +LE+
'_1 -> 1' +LE+
'1+_ -> 1' +LE+
'_+_ -> ';
Input: '_1111*11111_'; Output: '11111111111111111111'),
(Scheme:
'# Turing machine: three-state busy beaver' +LE+
'#' +LE+
'# state A, symbol 0 => write 1, move right, new state B' +LE+
'A0 -> 1B' +LE+
'# state A, symbol 1 => write 1, move left, new state C' +LE+
'0A1 -> C01' +LE+
'1A1 -> C11' +LE+
'# state B, symbol 0 => write 1, move left, new state A' +LE+
'0B0 -> A01' +LE+
'1B0 -> A11' +LE+
'# state B, symbol 1 => write 1, move right, new state B' +LE+
'B1 -> 1B' +LE+
'# state C, symbol 0 => write 1, move left, new state B' +LE+
'0C0 -> B01' +LE+
'1C0 -> B11' +LE+
'# state C, symbol 1 => write 1, move left, halt' +LE+
'0C1 -> H01' +LE+
'1C1 -> H11';
Input: '000000A000000'; Output: '00011H1111000')
);
E_FMT = 'test #%d: expected "%s", but got "%s"';
var
e: TTestEntry;
Result: string;
I: Integer = 1;
Failed: Integer = 0;
begin
for e in TestSet do begin
Result := ExecuteMA(e.Scheme, e.Input);
if Result <> e.Output then begin
WriteLn(Format(E_FMT, [I, e.Output, Result]));
Inc(Failed);
end;
Inc(I);
end;
WriteLn('tests completed: ', Length(TestSet), ', failed: ', Failed);
end.
</syntaxhighlight>
{{out}}
<pre>
tests completed: 5, failed: 0
</pre>
 
Line 3,182 ⟶ 3,735:
This program expects a source file as an argument and uses the standard input and output devices for the algorithm's I/O.
 
<langsyntaxhighlight lang="perl">@ARGV == 1 or die "Please provide exactly one source file as an argument.\n";
open my $source, '<', $ARGV[0] or die "I couldn't open \"$ARGV[0]\" for reading. ($!.)\n";
my @rules;
Line 3,199 ⟶ 3,752:
and ($terminating ? last OUTER : redo OUTER);}}
 
print $input;</langsyntaxhighlight>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>procedure markov(string rules, input, expected)
<span style="color: #008080;">procedure</span> <span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">rules</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">input</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">expected</span><span style="color: #0000FF;">)</span>
sequence subs = {}, reps = {}
<span style="color: #004080;">sequence</span> <span style="color: #000000;">subs</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{},</span> <span style="color: #000000;">reps</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
sequence lines = split(substitute(rules,"\t"," "),'\n')
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">substitute</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rules</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\t'</span><span style="color: #0000FF;">,</span><span style="color: #008000;">' '</span><span style="color: #0000FF;">),</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span>
for i=1 to length(lines) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string li = lines[i]
<span style="color: #004080;">string</span> <span style="color: #000000;">li</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
if length(li) and li[1]!='#' then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">li</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">li</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]!=</span><span style="color: #008000;">'#'</span> <span style="color: #008080;">then</span>
integer k = match(" -> ",li)
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #008000;">" -&gt; "</span><span style="color: #0000FF;">,</span><span style="color: #000000;">li</span><span style="color: #0000FF;">)</span>
if k then
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
subs = append(subs,trim(li[1..k-1]))
<span style="color: #000000;">subs</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">subs</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">li</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]))</span>
reps = append(reps,trim(li[k+4..$]))
<span style="color: #000000;">reps</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">reps</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">li</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #000000;">4</span><span style="color: #0000FF;">..$]))</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
string res = input
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">input</span>
bool term = false
<span style="color: #004080;">bool</span> <span style="color: #000000;">term</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
while 1 do
<span style="color: #008080;">while</span> <span style="color: #000000;">1</span> <span style="color: #008080;">do</span>
bool found = false
<span style="color: #004080;">bool</span> <span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
for i=1 to length(subs) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">subs</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
string sub = subs[i]
<span style="color: #004080;">string</span> <span style="color: #000000;">sub</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">subs</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
integer k = match(sub,res)
<span style="color: #004080;">integer</span> <span style="color: #000000;">k</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">match</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sub</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
if k then
<span style="color: #008080;">if</span> <span style="color: #000000;">k</span> <span style="color: #008080;">then</span>
found = true
<span style="color: #000000;">found</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
string rep = reps[i]
<span style="color: #004080;">string</span> <span style="color: #000000;">rep</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">reps</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
if length(rep) and rep[1]='.' then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">rep</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">and</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]=</span><span style="color: #008000;">'.'</span> <span style="color: #008080;">then</span>
rep = rep[2..$]
<span style="color: #000000;">rep</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span><span style="color: #0000FF;">[</span><span style="color: #000000;">2</span><span style="color: #0000FF;">..$]</span>
term = true
<span style="color: #000000;">term</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
res[k..k+length(sub)-1] = rep
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">k</span><span style="color: #0000FF;">..</span><span style="color: #000000;">k</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">sub</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">rep</span>
exit
end if <span style="color: #008080;">exit</span>
if term<span then exitstyle="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">term</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
if term or not found then exit end if
<span style="color: #008080;">if</span> <span style="color: #000000;">term</span> <span style="color: #008080;">or</span> <span style="color: #008080;">not</span> <span style="color: #000000;">found</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
?{input,res,iff(res=expected?"ok":"**ERROR**")}
<span style="color: #0000FF;">?{</span><span style="color: #000000;">input</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #000000;">expected</span><span style="color: #0000FF;">?</span><span style="color: #008000;">"ok"</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"**ERROR**"</span><span style="color: #0000FF;">)}</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
 
constant ruleset1 = """
<span style="color: #008080;">constant</span> <span style="color: #000000;">ruleset1</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
# This rules file is extracted from Wikipedia:
# This rules file is extracted from Wikipedia:
# http://en.wikipedia.org/wiki/Markov_Algorithm
# http://en.wikipedia.org/wiki/Markov_Algorithm
A -> apple
B A ->&gt; bagapple
S B ->&gt; shopbag
T S ->&gt; theshop
T -&gt; the
the shop -> my brother
the shop -&gt; my brother
a never used -> .terminating rule"""
a never used -&gt; .terminating rule"""</span>
markov(ruleset1,"I bought a B of As from T S.","I bought a bag of apples from my brother.")
<span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ruleset1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I bought a B of As from T S."</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I bought a bag of apples from my brother."</span><span style="color: #0000FF;">)</span>
 
constant ruleset2 = """
<span style="color: #008080;">constant</span> <span style="color: #000000;">ruleset2</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
# Slightly modified from the rules on Wikipedia
# Slightly modified from the rules on Wikipedia
A -> apple
B A ->&gt; bagapple
S B ->&gt; .shopbag
T S ->&gt; the.shop
T -&gt; the
the shop -> my brother
the shop -&gt; my brother
a never used -> .terminating rule"""
a never used -&gt; .terminating rule"""</span>
markov(ruleset2,"I bought a B of As from T S.","I bought a bag of apples from T shop.")
<span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ruleset2</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I bought a B of As from T S."</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I bought a bag of apples from T shop."</span><span style="color: #0000FF;">)</span>
 
constant ruleset3 = """
<span style="color: #008080;">constant</span> <span style="color: #000000;">ruleset3</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
# BNF Syntax testing rules
# BNF Syntax testing rules
A -> apple
A -&gt; apple
WWWW -> with
WWWW -&gt; with
Bgage -> ->.*
Bgage -&gt; -&gt;.*
B -> bag
B -&gt; bag
->.* -> money
-&gt;.* -&gt; money
W -> WW
S W ->&gt; .shopWW
T S ->&gt; the.shop
T -&gt; the
the shop -> my brother
the shop -&gt; my brother
a never used -> .terminating rule"""
a never used -&gt; .terminating rule"""</span>
markov(ruleset3,"I bought a B of As W my Bgage from T S.","I bought a bag of apples with my money from T shop.")
<span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ruleset3</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I bought a B of As W my Bgage from T S."</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"I bought a bag of apples with my money from T shop."</span><span style="color: #0000FF;">)</span>
 
constant ruleset4 = """
<span style="color: #008080;">constant</span> <span style="color: #000000;">ruleset4</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
### Unary Multiplication Engine, for testing Markov Algorithm implementations
### Unary Multiplication Engine, for testing Markov Algorithm implementations
### By Donal Fellows.
### By Donal Fellows.
# Unary addition engine
# Unary addition engine
_+1 -> _1+
1 _+1 ->&gt; 11_1+
1+1 -&gt; 11+
# Pass for converting from the splitting of multiplication into ordinary
# Pass for converting from the splitting of multiplication into ordinary
# addition
# addition
1! -> !1
, 1! ->&gt; !+1
_ ,! ->&gt; _!+
_! -&gt; _
# Unary multiplication by duplicating left side, right side times
# Unary multiplication by duplicating left side, right side times
1*1 -> x,@y
1*1 -&gt; x,@y
1x -> xX
X, 1x ->&gt; 1,1xX
X1 X, ->&gt; 1X1,1
_x X1 ->&gt; _X1X
,x _x ->&gt; ,X_X
y1 ,x ->&gt; 1y,X
y_ y1 ->&gt; _1y
y_ -&gt; _
# Next phase of applying
# Next phase of applying
1@1 -> x,@y
1@_1 ->&gt; x,@_y
, 1@_ ->&gt; !@_
++ ,@_ ->&gt; +!_
++ -&gt; +
# Termination cleanup for addition
# Termination cleanup for addition
_1 -> 1
1+_ _1 ->&gt; 1
_ 1+_ ->&gt; 1
_+_ -&gt;
"""
"""</span>
markov(ruleset4,"_1111*11111_","11111111111111111111")
<span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ruleset4</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"_1111*11111_"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"11111111111111111111"</span><span style="color: #0000FF;">)</span>
 
constant ruleset5 = """
<span style="color: #008080;">constant</span> <span style="color: #000000;">ruleset5</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
# Turing machine: three-state busy beaver
# Turing machine: three-state busy beaver
#
#
# state A, symbol 0 => write 1, move right, new state B
# state A, symbol 0 =&gt; write 1, move right, new state B
A0 -> 1B
A0 -&gt; 1B
# state A, symbol 1 => write 1, move left, new state C
# state A, symbol 1 =&gt; write 1, move left, new state C
0A1 -> C01
1A1 0A1 ->&gt; C11C01
1A1 -&gt; C11
# state B, symbol 0 => write 1, move left, new state A
# state B, symbol 0 =&gt; write 1, move left, new state A
0B0 -> A01
1B0 0B0 ->&gt; A11A01
1B0 -&gt; A11
# state B, symbol 1 => write 1, move right, new state B
# state B, symbol 1 =&gt; write 1, move right, new state B
B1 -> 1B
B1 -&gt; 1B
# state C, symbol 0 => write 1, move left, new state B
# state C, symbol 0 =&gt; write 1, move left, new state B
0C0 -> B01
1C0 0C0 ->&gt; B11B01
1C0 -&gt; B11
# state C, symbol 1 => write 1, move left, halt
# state C, symbol 1 =&gt; write 1, move left, halt
0C1 -> H01
1C1 0C1 ->&gt; H11H01
1C1 -&gt; H11
"""
"""</span>
markov(ruleset5,"000000A000000","00011H1111000")</lang>
<span style="color: #000000;">markov</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ruleset5</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"000000A000000"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"00011H1111000"</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,338 ⟶ 3,893:
=={{header|PHP}}==
 
<langsyntaxhighlight PHPlang="php"><?php
 
function markov($text, $ruleset) {
Line 3,484 ⟶ 4,039:
foreach ($conf AS $id => $rule) {
echo 'Ruleset ', $id, ' : ', markov($rule['text'], $rule['rule']), PHP_EOL;
}</langsyntaxhighlight>
 
{{out}}
Line 3,495 ⟶ 4,050:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de markov (File Text)
(use (@A @Z R)
(let Rules
Line 3,513 ⟶ 4,068:
(T (= "." (cadr (setq R @)))
(append @A (cddr R) @Z) )
(setq Text (append @A (cdr R) @Z)) ) ) ) ) )</langsyntaxhighlight>
Output:
<pre>: (markov "r1" "I bought a B of As from T S.")
Line 3,534 ⟶ 4,089:
Module lambda can be found there : http://www.complang.tuwien.ac.at/ulrich/Prolog-inedit/lambda.pl
 
<langsyntaxhighlight lang="prolog">:- module('markov.pl', [markov/3, apply_markov/3]).
 
:- use_module(library(lambda)).
Line 3,622 ⟶ 4,177:
replacement([X | R]) --> [X], {X \= '\n'}, replacement(R).
replacement([]) --> [].
</syntaxhighlight>
</lang>
Code to test :
<langsyntaxhighlight Prologlang="prolog">:- use_module('markov.pl').
:- use_module(library(lambda)).
 
Line 3,741 ⟶ 4,296:
writeln(B),
writeln(R).
</syntaxhighlight>
</lang>
Output :
<pre> ?- markov.
Line 3,769 ⟶ 4,324:
=={{header|PureBasic}}==
The GUI used here allows a ruleset to be loaded from a text file or manually added one rule at a time. Symbol input can be tested anytime by selecting 'Interpret'.
<langsyntaxhighlight PureBasiclang="purebasic">Structure mRule
pattern.s
replacement.s
Line 3,882 ⟶ 4,437:
EndSelect
Until isDone
</syntaxhighlight>
</lang>
Sample output from loading Ruleset 1 and interpreting a symbol:
<pre>Comment: "# This rules file is extracted from Wikipedia:"
Line 3,901 ⟶ 4,456:
 
The example gains flexibility by not being tied to specific files. The functions may be imported into other programs which then can provide textual input from their sources without the need to pass 'file handles' around.
<langsyntaxhighlight lang="python">import re
 
def extractreplacements(grammar):
Line 4,034 ⟶ 4,589:
== '11111111111111111111'
assert replace(text4, extractreplacements(grammar5)) \
== '00011H1111000'</langsyntaxhighlight>
 
=={{header|Racket}}==
Line 4,042 ⟶ 4,597:
The <tt>Markov-algorithm</tt> for a set of rules returns a function which maps from a string to string and can be used as a first-class object. Rules are represented by abstract data structures.
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 4,071 ⟶ 4,626:
(let loop ([x x0] [fx (f x0)])
(if (equal? x fx) fx (loop fx (f fx)))))
</syntaxhighlight>
</lang>
 
Example of use:
 
<langsyntaxhighlight lang="racket">
> (define MA
(Markov-algorithm
Line 4,087 ⟶ 4,642:
> (MA "I bought a B of As from T S.")
"I bought a bag of apples from T shop."
</syntaxhighlight>
</lang>
 
===The source reader===
Line 4,093 ⟶ 4,648:
To read from a file just replace <tt>with-input-from-string</tt> ==> <tt>with-input-from-file</tt>.
 
<langsyntaxhighlight lang="racket">
;; the reader
(define (read-rules source)
Line 4,116 ⟶ 4,671:
(define (read-Markov-algorithm source)
(apply Markov-algorithm (read-rules source)))
</syntaxhighlight>
</lang>
 
Examples:
 
<langsyntaxhighlight lang="racket">
(define R3 (read-Markov-algorithm "
# BNF Syntax testing rules
Line 4,185 ⟶ 4,740:
0C1 -> H01
1C1 -> H11"))
</syntaxhighlight>
</lang>
 
<langsyntaxhighlight lang="racket">
> (R3 "I bought a B of As W my Bgage from T S.")
"I bought a bag of apples with my money from T shop."
Line 4,196 ⟶ 4,751:
> (R5 "000000A000000")
"00011H1111000"
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
Line 4,206 ⟶ 4,761:
Add --verbose to see the replacements step-by-step.
 
<syntaxhighlight lang="raku" perl6line>grammar Markov {
token TOP {
^ [^^ [<rule> | <comment>] $$ [\n|$]]* $
Line 4,270 ⟶ 4,825:
say "starting with: $start_value";
say run(:$ruleset, :$start_value, :$verbose);
}</langsyntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, <Arg 1>: e.File
, <Arg 2>: e.Input
, <ReadLines 1 e.File>: e.Lines
, <Each ParseRule e.Lines>: e.Rules
, <Apply (e.Rules) e.Input>: e.Result
= <Prout e.Result>;
};
 
Each {
s.F = ;
s.F (e.I) e.R = <Mu s.F e.I> <Each s.F e.R>;
};
 
ReadLines {
s.Chan e.File = <Open 'r' s.Chan e.File>
<ReadLines (s.Chan)>;
(s.Chan), <Get s.Chan>: {
0 = <Close s.Chan>;
e.Line = (e.Line) <ReadLines (s.Chan)>;
};
};
 
ParseRule {
= (Empty);
'#' e.X = (Empty);
e.Pat ' -> ' e.Rep,
<Trim e.Pat>: e.TrPat,
<Trim e.Rep>: e.TrRep,
e.TrRep: {
'.' e.R = (Term (e.Pat) (e.R));
e.R = (Nonterm (e.Pat) (e.R));
};
};
 
ApplyRule {
(s.Type (e.Pat) (e.Rep)) e.Subj,
e.Subj: e.X e.Pat e.Y = s.Type e.X e.Rep e.Y;
t.Rule e.Subj = NoMatch e.Subj;
};
 
Apply {
(e.Rules) () e.Subj = e.Subj;
(e.Rules) (t.Rule e.Rest) e.Subj,
<ApplyRule t.Rule e.Subj>: {
NoMatch e.Subj = <Apply (e.Rules) (e.Rest) e.Subj>;
Term e.Res = e.Res;
Nonterm e.Res = <Apply (e.Rules) e.Res>;
};
(e.Rules) e.Subj = <Apply (e.Rules) (e.Rules) e.Subj>;
};
 
Trim {
' ' e.X = <Trim e.X>;
e.X ' ' = <Trim e.X>;
e.X = e.X;
};</syntaxhighlight>
{{out}}
<pre>$ refgo markov ruleset1.mkv 'I bought a B of As from T S.'
I bought a bag of apples from my brother.
$ refgo markov ruleset2.mkv 'I bought a B of As from T S.'
I bought a bag of apples from T shop.
$ refgo markov 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.
$ refgo markov ruleset4.mkv '_111*11111_'
111111111111111
$ refgo markov ruleset5.mkv '000000A000000'
00011H1111000</pre>
 
=={{header|REXX}}==
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.
<langsyntaxhighlight lang="rexx">/*REXX program executes a Markov algorithm(s) against specified entries. */
parse arg low high . /*allows which ruleset to process. */
if low=='' | low=="," then low=1 /*Not specified? Then use the default.*/
Line 4,316 ⟶ 4,941:
@.r=linein(rFID); if tellR then say 'ruleSet' ?"."left(r,4) '───►' @.r
end /*r*/ /* [↑] read and maybe echo the rule. */
return</langsyntaxhighlight>
Some older REXXes don't have a &nbsp; '''changestr''' &nbsp; BIF, so one is included here &nbsp; ──► &nbsp; [[CHANGESTR.REX]].
<br><br>
Line 4,339 ⟶ 4,964:
 
=={{header|Ruby}}==
{{works with|Ruby|13.82.70}}
<langsyntaxhighlight Rubylang="ruby">def setup(ruleset)
ruleset.each_line.inject([]) do |rules, line|
if line =~ /^\s*#/
Line 4,352 ⟶ 4,977:
end
 
def morcovmarkov(ruleset, input_data)
rules = setup(ruleset)
while (matched = rules.find { |match, replace, term|
Line 4,359 ⟶ 4,984:
end
input_data
end</langsyntaxhighlight>
 
'''Test:'''
<langsyntaxhighlight Rubylang="ruby">ruleset1 = <<EOS
# This rules file is extracted from Wikipedia:
# http://en.wikipedia.org/wiki/Markov_Algorithm
Line 4,373 ⟶ 4,998:
EOS
 
puts morcovmarkov(ruleset1, "I bought a B of As from T S.")
 
ruleset2 = <<EOS
Line 4,385 ⟶ 5,010:
EOS
 
puts morcovmarkov(ruleset2, "I bought a B of As from T S.")
 
ruleset3 = <<EOS
Line 4,401 ⟶ 5,026:
EOS
 
puts morcovmarkov(ruleset3, "I bought a B of As W my Bgage from T S.")
 
ruleset4 = <<EOS
Line 4,434 ⟶ 5,059:
EOS
 
puts morcovmarkov(ruleset4, "_1111*11111_")
 
ruleset5 = <<EOS
Line 4,457 ⟶ 5,082:
EOS
 
puts morcovmarkov(ruleset5, "000000A000000")</langsyntaxhighlight>
 
{{out}}
Line 4,467 ⟶ 5,092:
00011H1111000
</pre>
 
 
=={{header|Rust}}==
 
<langsyntaxhighlight lang="rust">use std::str::FromStr;
 
#[derive(Clone, Debug)]
Line 4,701 ⟶ 5,325:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
{{works with|Scala|2.8}}
<langsyntaxhighlight lang="scala">import scala.io.Source
 
object MarkovAlgorithm {
Line 4,734 ⟶ 5,358:
println(algorithm(args(1)))
}
}</langsyntaxhighlight>
 
Script-style, and more concise:
 
<langsyntaxhighlight lang="scala">import scala.io.Source
 
if (argv.size != 2 ) error("Syntax: MarkovAlgorithm inputFile inputPattern")
Line 4,753 ⟶ 5,377:
 
println(argv(1))
println(algorithm(argv(1)))</langsyntaxhighlight>
 
Sample outputs:
Line 4,781 ⟶ 5,405:
The following implementation uses several string-related procedures provided by SRFI-13 [http://srfi.schemers.org/srfi-13/srfi-13.html].
 
<langsyntaxhighlight lang="scheme">
(define split-into-lines
(lambda (str)
Line 4,829 ⟶ 5,453:
rules))
(loop (cdr remaining) result)))))))
</syntaxhighlight>
</lang>
 
=={{header|SequenceL}}==
 
<syntaxhighlight lang="sequencel">
<lang sequenceL>
import <Utilities/Sequence.sl>;
 
Line 4,882 ⟶ 5,506:
replaceSubString(str, original, new, n + 1);
 
</syntaxhighlight>
</lang>
 
=={{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}}==
Note that the run-time data is immediately after the "end" label. This works with CSNOBOL4, on a Unix (or Unix-like) platform.
The Markov rules are actually compiled into the program after parsing, and are then directly executed (self-modifying code).
<syntaxhighlight lang="snobol4">
<lang SNOBOL4>
#!/bin/sh
exec "snobol4" "-r" "$0" "$@"
Line 5,016 ⟶ 5,715:
000000A000000
END
</syntaxhighlight>
</lang>
 
=={{header|Swift}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="swift">import Foundation
 
func setup(ruleset: String) -> [(String, String, Bool)] {
Line 5,065 ⟶ 5,764:
}
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 5,077 ⟶ 5,776:
=={{header|Tcl}}==
{{works with|Tcl|8.5}}
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
if {$argc < 3} {error "usage: $argv0 ruleFile inputFile outputFile"}
lassign $argv ruleFile inputFile outputFile
Line 5,117 ⟶ 5,816:
puts $out $line
}
close $out</langsyntaxhighlight>
In the case where there are no terminating rules and no overlapping issues, the following is an alternative:
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
if {$argc < 3} {error "usage: $argv0 ruleFile inputFile outputFile"}
lassign $argv ruleFile inputFile outputFile
Line 5,146 ⟶ 5,845:
}
puts $out $data
close $out</langsyntaxhighlight>
 
=={{header|VBScript}}==
====Implementation====
<syntaxhighlight lang="vb">
<lang vb>
class markovparser
 
Line 5,224 ⟶ 5,923:
end function
end class
</syntaxhighlight>
</lang>
 
=====Invocation=====
<syntaxhighlight lang="vb">
<lang vb>
dim m1
set m1 = new markovparser
Line 5,288 ⟶ 5,987:
m5.ruleset = fso.opentextfile("busybeaver.tur").readall
wscript.echo m5.apply("000000A000000")
</syntaxhighlight>
</lang>
 
=====Output=====
<syntaxhighlight lang="vb">
<lang vb>
I bought a bag of apples from my brother.
I bought a bag of apples from T shop.
Line 5,297 ⟶ 5,996:
11111111111111111111
00011H1111000
</syntaxhighlight>
</lang>
 
=={{header|Wren}}==
{{trans|Kotlin}}
{{libheader|Wren-ioutil}}
{{libheader|wren-pattern}}
<syntaxhighlight lang="wren">import "./ioutil" for FileUtil, File
import "./pattern" for Pattern
 
var lb = FileUtil.lineBreak
 
/* rulesets assumed to be separated by a blank line in file */
var readRules = Fn.new { |path|
return File.read(path).trimEnd().split("%(lb)%(lb)").map { |rs| rs.split(lb) }.toList
}
 
/* tests assumed to be on consecutive lines */
var readTests = Fn.new { |path| File.read(path).trimEnd().split(lb) }
 
var rules = readRules.call("markov_rules.txt")
var tests = readTests.call("markov_tests.txt")
var pattern = Pattern.new("+0/s[~.][+0/z]", Pattern.start)
var ix = 0
for (origTest in tests) {
var captures = []
for (rule in rules[ix]) {
if (rule.startsWith("#")) continue
var splits = rule.split(" -> ")
var m = pattern.find(splits[1])
if (m) captures.add([splits[0].trimEnd()] + m.capsText)
}
var test = origTest
while (true) {
var copy = test
var redo = false
for (c in captures) {
test = test.replace(c[0], c[2])
if (c[1] == ".") break
if (test != copy) {
redo = true
break
}
}
if (!redo) break
}
System.print("%(origTest)\n%(test)\n")
ix = ix + 1
}</syntaxhighlight>
 
{{out}}
<pre>
I bought a B of As from T S.
I bought a bag of apples from my brother.
 
I bought a B of As from T S.
I bought a bag of apples from T shop.
 
I bought a B of As W my Bgage from T S.
I bought a bag of apples with my money from T shop.
 
_1111*11111_
11111111111111111111
 
000000A000000
00011H1111000
</pre>
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">fcn parseRuleSet(lines){
if(vm.numArgs>1) lines=vm.arglist; // lines or object
ks:=L(); vs:=L();
Line 5,325 ⟶ 6,089:
}while(go);
text
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">ruleSet:=parseRuleSet("# This rules file is extracted from Wikipedia:",
"# http://en.wikipedia.org/wiki/Markov_Algorithm",
"A\t->\tapple", "B -> bag", "S -> shop", "T -> the",
"the shop -> my brother", "a never used -> .terminating rule");
ruleSet.println();
markov("I bought a B of As from T S.",ruleSet).println();</langsyntaxhighlight>
{{out}}
<pre>
Line 5,337 ⟶ 6,101:
I bought a bag of apples from my brother.
</pre>
<langsyntaxhighlight lang="zkl">parseRuleSet( // rule set in a list
T("# Slightly modified from the rules on Wikipedia",
"A -> apple", "B -> bag", "S -> .shop", "T -> the",
Line 5,347 ⟶ 6,111:
"W -> WW", "S -> .shop", "T -> the",
"the shop -> my brother", "a never used -> .terminating rule") :
markov("I bought a B of As W my Bgage from T S.",_).println();</langsyntaxhighlight>
{{out}}
<pre>
Line 5,354 ⟶ 6,118:
</pre>
For the next two tasks, read the rule set from a file.
<langsyntaxhighlight lang="zkl">parseRuleSet(File("ruleSet4")) : markov("_1111*11111_",_).println();
parseRuleSet(File("ruleSet5")) : markov("000000A000000",_).println();</langsyntaxhighlight>
{{out}}
<pre>
2,095

edits