Skip to content
Browse files

Merge pull request #1 from retupmoca/master

Make tests pass
  • Loading branch information...
2 parents 1eabac5 + 70527ab commit 329f269db71b1f21269add626ffc8ab73e1b47f6 @arnsholt committed May 13, 2014
Showing with 13 additions and 15 deletions.
  1. +11 −13 lib/Algorithm/Viterbi.pm
  2. +2 −2 t/00-basic.t
View
24 lib/Algorithm/Viterbi.pm
@@ -2,8 +2,8 @@ use v6;
class Algorithm::Viterbi;
-our class Start {};
-our class End {};
+#our class Start {};
+#our class End {};
# TODO:
our role Observation {};
@@ -43,9 +43,7 @@ has %.p-emission;
# TODO: It might be nice to be able to do the computations both using
# logarithms and the way it works now?
-method BUILD(:@alphabet!) {
- callsame;
-}
+submethod BUILD(:@!alphabet!, :%!p-transition, :%!p-emission) { }
# TODO: Algorithm::Viterbi on CPAN also computes the Forward probability of
# the sequence. Should be doable to compute as well.
@@ -64,9 +62,9 @@ method decode($hmm: @input) {
@trellis[0][0] = 0;
for ^@!alphabet -> $state {
my $tag = @!alphabet[$state];
- @trellis[0][$state] = %!p-transition{Start}{$tag}
+ @trellis[0][$state] = %!p-transition{'Start'}{$tag}
* %!p-emission{$tag}{$first};
- @trace[0][$state] = Start;
+ @trace[0][$state] = 'Start';
}
# Iterate over the input, calculating probabilities as we go.
@@ -102,7 +100,7 @@ method decode($hmm: @input) {
for ^@!alphabet -> $prev-state {
my $prev-tag = @!alphabet[$prev-state];
my $new-p = @trellis[$index][$prev-state]
- * %!p-transition{$prev-tag}{End};
+ * %!p-transition{$prev-tag}{'End'};
if $new-p > $max-p {
$max-p = $new-p;
@@ -136,8 +134,8 @@ multi method train($hmm: Str $file) {
multi method train($hmm: @input) {
# First, count the number of transitions between pairs of tags, and
# emission counts for each tag-observation pair.
- for @input -> @sequence {
- my $prev = Start;
+ for @input.lol -> @sequence {
+ my $prev = 'Start';
for @sequence -> $pair {
my ($observation, $tag) = ($pair.key, $pair.value);
@@ -152,13 +150,13 @@ multi method train($hmm: @input) {
}
%!p-transition{$prev} //= {};
- %!p-transition{$prev}{End}++;
+ %!p-transition{$prev}{'End'}++;
}
# XXX: Development testing code
- #say %!p-transition{Start}<H>; # Should be: 77
+ #say %!p-transition{'Start'}<H>; # Should be: 77
#say %!p-transition<C><H>; # Should be: 26
- #say %!p-transition<C>{End}; # Should be: 44
+ #say %!p-transition<C>{'End'}; # Should be: 44
#say %!p-emission<C><3>; # Should be: 20
# Compute the actual transition probabilities.
View
4 t/00-basic.t
@@ -13,15 +13,15 @@ $hmm.train("t/eisner.tt");
ok($hmm.p-transition<C><H> == 13/68, "C -> H == 13/68?");
ok($hmm.p-emission<C><3> == 5/34, "C -> 3 == 5/34?");
-my $result = $hmm.decode(<1 1 3 3 3 3 1 1 1 1>);
+my $result = $hmm.decode(<1 1 3 3 3 3 1 1 1 1>.list);
is_deeply($result, ["H", "H", "H", "H", "H", "H", "C", "C", "C", "C"],
"correctly decodes <1 1 3 3 3 3 1 1 1 1>");
my Algorithm::Viterbi $other .= new(:alphabet<H C>,
:p-transition($hmm.p-transition), :p-emission($hmm.p-emission));
pass("creating decoder with explicit probabilities");
-$result = $other.decode(<1 1 3 3 3 3 1 1 1 1>);
+$result = $other.decode(<1 1 3 3 3 3 1 1 1 1>.list);
is_deeply($result, ["H", "H", "H", "H", "H", "H", "C", "C", "C", "C"],
"correctly decodes <1 1 3 3 3 3 1 1 1 1>");

0 comments on commit 329f269

Please sign in to comment.
Something went wrong with that request. Please try again.