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() {
auto rules = readText("markov_rules.txt").splitLines().split("");
string[][] rules = readText("markov_rules.txt").splitLines()
.split("");
auto tests = readText("markov_tests.txt").splitLines();
auto tests = readText("markov_tests.txt").splitLines();
auto regex = ctRegex!(r"^([^#]*?)\s+->\s+(\.?)(.*)");
auto re = ctRegex!(r"^([^#]*?)\s+->\s+(\.?)(.*)"); // 130 MB RAM.


auto pairs = zip(StoppingPolicy.requireSameLength, tests, rules);
foreach (i, test; tests) {
foreach (test, rule; pairs) {
auto origtest = test.dup;
auto origTest = test.dup;


string[][] capt;
string[][] capt;
foreach (line; rules[i]) {
foreach (line; rule) {
auto m = line.match(regex);
auto m = line.match(re);
if (!m.empty) capt ~= array(m.captures)[1 .. $];
if (!m.empty)
capt ~= m.captures.array()[1 .. $];
}
}

REDO: auto copy = test;
REDO:
auto copy = test;
foreach (c; capt) {
foreach (c; capt) {
test = test.replace(c[0], c[2]);
test = test.replace(c[0], c[2]);
if (c[1] == ".") break;
if (c[1] == ".")
if (test != copy) goto REDO;
break;
if (test != copy)
goto REDO;
}
}
writefln("%s\n%s\n", origtest, test);
writefln("%s\n%s\n", origTest, test);
}
}
}</lang>
}</lang>