RosettaCodeData/Task/Execute-a-Markov-algorithm/D/execute-a-markov-algorithm.d

34 lines
998 B
D
Raw Permalink Normal View History

2013-04-10 23:57:08 -07:00
void main() {
2013-10-27 22:24:23 +00:00
import std.stdio, std.file, std.regex, std.string, std.range,
std.functional;
const rules = "markov_rules.txt".readText.splitLines.split("");
auto tests = "markov_tests.txt".readText.splitLines;
auto re = ctRegex!(r"^([^#]*?)\s+->\s+(\.?)(.*)"); // 160 MB RAM.
2013-04-10 23:57:08 -07:00
2013-10-27 22:24:23 +00:00
alias slZip = curry!(zip, StoppingPolicy.requireSameLength);
foreach (test, const rule; slZip(tests, rules)) {
const origTest = test.dup;
2013-04-10 23:57:08 -07:00
string[][] capt;
2013-10-27 22:24:23 +00:00
foreach (const line; rule) {
2013-04-10 23:57:08 -07:00
auto m = line.match(re);
2013-10-27 22:24:23 +00:00
if (!m.empty) {
//capt.put(m.captures.dropOne);
capt ~= m.captures.dropOne.array;
}
2013-04-10 23:57:08 -07:00
}
REDO:
2013-10-27 22:24:23 +00:00
const copy = test;
foreach (const c; capt) {
2013-04-10 23:57:08 -07:00
test = test.replace(c[0], c[2]);
if (c[1] == ".")
break;
if (test != copy)
goto REDO;
}
writefln("%s\n%s\n", origTest, test);
}
}