Execute a Markov algorithm: Difference between revisions

Content deleted Content added
Shorter lines in D entry, removed casts, formatting, some comments and blank lines added
Line 1,006: Line 1,006:


void main() {
void main() {
auto rules = split(splitLines(cast(string)read("res/markov_rules.txt")), "");
string[][] rules = readText("markov_rules.txt").splitLines().
split("");
auto tests = splitLines(cast(string)read("res/markov_tests.txt"));
string[] tests = readText("markov_tests.txt").splitLines();
enum regex = ctRegex!(r"^([^#]*?)\s+->\s+(\.?)(.*)"); // compile time regex creation
assert(tests.length == rules.length);

// Faster to compile run-time regex
//auto regex = regex(r"^([^#]*?)\s+->\s+(\.?)(.*)");

// Compile-time regex creation
// DMD 2.059 uses about 130 MB RAM to compile this
enum regex = ctRegex!(r"^([^#]*?)\s+->\s+(\.?)(.*)");

foreach (i, test; tests) {
foreach (i, test; tests) {
string[][] capt;
string[][] capt;
foreach (line; rules[i]) {
foreach (line; rules[i]) {
auto m = match(line, regex);
auto m = line.match(regex);
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;
}
}

writeln(test);
writeln(test);
}
}