<s> from __future__ import absolute_import , unicode_literals <EOL> from nltk . tag import hmm <EOL> def _wikipedia_example_hmm ( ) : <EOL> states = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> symbols = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> A = [ [ <NUM_LIT> , <NUM_LIT> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> B = [ [ <NUM_LIT> , <NUM_LIT:0.1> ] , [ <NUM_LIT> , <NUM_LIT> ] ] <EOL> pi = [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] <EOL> seq = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> seq = list ( zip ( seq , [ None ] * len ( seq ) ) ) <EOL> model = hmm . _create_hmm_tagger ( states , symbols , A , B , pi ) <EOL> return model , states , symbols , seq <EOL> def test_forward_probability ( ) : <EOL> from numpy . testing import assert_array_almost_equal <EOL> model , states , symbols = hmm . _market_hmm_example ( ) <EOL> seq = [ ( '<STR_LIT>' , None ) , ( '<STR_LIT>' , None ) ] <EOL> expected = [ <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> , <NUM_LIT> ] <EOL> ] <EOL> fp = <NUM_LIT:2> ** model . _forward_probability ( seq ) <EOL> assert_array_almost_equal ( fp , expected ) <EOL> def test_forward_probability2 ( ) : <EOL> from numpy . testing import assert_array_almost_equal <EOL> model , states , symbols , seq = _wikipedia_example_hmm ( ) <EOL> fp = <NUM_LIT:2> ** model . _forward_probability ( seq ) <EOL> fp = ( fp . T / fp . sum ( axis = <NUM_LIT:1> ) ) . T <EOL> wikipedia_results = [ <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> ] <EOL> assert_array_almost_equal ( wikipedia_results , fp , <NUM_LIT:4> ) <EOL> def test_backward_probability ( ) : <EOL> from numpy . testing import assert_array_almost_equal <EOL> model , states , symbols , seq = _wikipedia_example_hmm ( ) <EOL> bp = <NUM_LIT:2> ** model . _backward_probability ( seq ) <EOL> bp = ( bp . T / bp . sum ( axis = <NUM_LIT:1> ) ) . T <EOL> wikipedia_results = [ <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT> , <NUM_LIT> ] , <EOL> [ <NUM_LIT:0.5> , <NUM_LIT:0.5> ] , <EOL> ] <EOL> assert_array_almost_equal ( wikipedia_results , bp , <NUM_LIT:4> ) <EOL> def setup_module ( module ) : <EOL> from nose import SkipTest <EOL> try : <EOL> import numpy <EOL> except ImportError : <EOL> raise SkipTest ( "<STR_LIT>" ) </s>
