Jump to content

Execute a Markov algorithm: Difference between revisions

(→‎{{header|Go}}: Clarified output, showed portion of data file. (Markov algorithm code unchanged.))
Line 944:
}
</lang>
 
 
=={{header|D}}==
{{trans|Perl}}
{{works with|D|2}}
<lang d>import std.stdio, std.array, std.file, std.regex: match;
import std.string: splitlines, newline;
 
void main() {
auto rules = readFile("markov_rules.txt");
auto tests = splitlines(cast(string)read("markov_tests.txt"));
foreach (i, rule; rules) {
string[][] capt;
foreach (line; rule) {
auto m = match(line, r"(.*?)\s+->\s+(\.?)(.*)");
if(!m.empty) capt ~= array(m.captures)[1..$];
}
redo:
foreach (c; capt) {
auto copy = tests[i].idup;
tests[i] = replace(tests[i], c[0], c[2]);
if (c[1] == ".") break;
if (tests[i] != copy) goto redo;
}
writeln(tests[i]);
}
}
 
auto readFile(in string fn) {
string[][] res;
foreach(a; split(cast(string)read(fn), newline~newline))
res ~= splitlines(a);
return res;
}</lang>
 
<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|Go}}==
<lang go>package main
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.