Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

A few more tests, slightly modified innards.

  • Loading branch information...
commit 93b7766c922eba854ccc8c71246135ae92391ca9 1 parent b6cbf3d
@arnsholt authored
Showing with 20 additions and 13 deletions.
  1. +8 −12 lib/Algorithm/Viterbi.pm
  2. +12 −1 t/00-basic.t
View
20 lib/Algorithm/Viterbi.pm
@@ -38,19 +38,11 @@ my class Actions {
}
has @!alphabet; # The HMM's alphabet
-has %!name-to-index;
has %.p-transition;
has %.p-emission;
-method BUILD(:@alphabet) {
- @!alphabet = @alphabet;
-
- for @!alphabet.kv -> $index, $state {
- %!name-to-index{$state} = $index;
- }
-
- %!p-transition = {};
- %!p-emission = {};
+method BUILD(:@alphabet!) {
+ callsame;
}
# TODO: Algorithm::Viterbi on CPAN also computes the Forward probability of
@@ -232,9 +224,13 @@ tag, then by observation.
=over 4
-=item method new(:@alphabet)
+=item method new(:@alphabet!, :%p-transition, :%p-emission)
-The constructor takes a single argument: a list of the tag names used.
+The alphabet parameter is required (an alphabet-less HMM doesn't make too much
+sense). The transition and emission probabilities are also required for
+correct operation of C<decode>, but can be specified either on construction,
+with the C<train> method, or by manual specification via the corresponding
+fields.
=item method decode(Str @input)
View
13 t/00-basic.t
@@ -4,7 +4,7 @@ use Test;
use Algorithm::Viterbi;
-plan 4;
+plan 7;
my Algorithm::Viterbi $hmm .= new(:alphabet<H C>);
pass("creating new decoder");
@@ -16,3 +16,14 @@ 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>);
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>);
+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>");
+
+dies_ok(-> { my Algorithm::Viterbi $hmm .= new; },
+ "impossible to create alphabet-less decoder");
Please sign in to comment.
Something went wrong with that request. Please try again.