Skip to content

Instantly share code, notes, and snippets.

@ivoras
Created May 14, 2015 12:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ivoras/2d7737c214c3dc937c28 to your computer and use it in GitHub Desktop.
Save ivoras/2d7737c214c3dc937c28 to your computer and use it in GitHub Desktop.
import std.algorithm, std.container, std.conv, std.regex, std.stdio, std.string;
Array!string [string] markov;
int main(string[] args)
{
static rx = regex("[ \r\n\t,.!?@#\"'*_()\\[\\]{}+=:;/-]+");
auto prev_token = "";
foreach (c_line; stdin.byLine()) {
auto line = to!string(c_line);
string[] tokens = split(line.replace("„", "").replace("“", ""), rx);
foreach (tok; tokens) {
if (tok.length == 0)
continue;
auto token = toLower(tok);
if (prev_token in markov) {
markov[prev_token].insert(token);
} else {
markov[prev_token] = Array!string([token]);
}
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment