Skip to content

Instantly share code, notes, and snippets.

@MattOates
Created October 1, 2013 08:48
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 MattOates/4a451d9f9c31ea055a45 to your computer and use it in GitHub Desktop.
Save MattOates/4a451d9f9c31ea055a45 to your computer and use it in GitHub Desktop.
A markovian bag model of some input test.txt
use v6;
my $previous;
my $current;
my %model := Hash.new;
sub add-to-model (%model,$word,$trailing) {
if not %model.exists($word) {
%model{$word} = KeyBag.new;
}
%model{$word}{$trailing}++;
}
for slurp("test.txt").comb(/\w+/) -> $word {
if not $previous {
$previous = $word;
next;
}
$current = $word;
add-to-model(%model,$previous,$current);
$previous = $current;
}
say %model.perl;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment