Execute a Markov algorithm: Difference between revisions
Content added Content deleted
(Added groovy version) |
(D entry: i index => zip, assert lens => requireSameLength, no auto "rules" for understandability) |
||
Line 1,048: | Line 1,048: | ||
=={{header|D}}== |
=={{header|D}}== |
||
{{trans|Perl}} |
{{trans|Perl}} |
||
<lang d>import std.stdio, std.array, std.file, std.regex, std.string |
<lang d>import std.stdio, std.array, std.file, std.regex, std.string, |
||
std.range; |
|||
void main() { |
void main() { |
||
string[][] rules = readText("markov_rules.txt").splitLines() |
|||
.split(""); |
|||
auto tests = readText("markov_tests.txt").splitLines(); |
auto tests = readText("markov_tests.txt").splitLines(); |
||
auto |
auto re = ctRegex!(r"^([^#]*?)\s+->\s+(\.?)(.*)"); // 130 MB RAM. |
||
auto pairs = zip(StoppingPolicy.requireSameLength, tests, rules); |
|||
foreach ( |
foreach (test, rule; pairs) { |
||
auto |
auto origTest = test.dup; |
||
string[][] capt; |
string[][] capt; |
||
foreach (line; |
foreach (line; rule) { |
||
auto m = line.match( |
auto m = line.match(re); |
||
if (!m.empty) |
if (!m.empty) |
||
capt ~= m.captures.array()[1 .. $]; |
|||
} |
} |
||
⚫ | |||
REDO: |
|||
⚫ | |||
foreach (c; capt) { |
foreach (c; capt) { |
||
test = test.replace(c[0], c[2]); |
test = test.replace(c[0], c[2]); |
||
if (c[1] == ".") |
if (c[1] == ".") |
||
break; |
|||
if (test != copy) |
|||
goto REDO; |
|||
} |
} |
||
writefln("%s\n%s\n", |
writefln("%s\n%s\n", origTest, test); |
||
} |
} |
||
}</lang> |
}</lang> |