Пример #1
0
    def test_h_emission(self, part_1_a):
        (b_prior_probs, b_transition_probs, b_emission_paras,
        c_prior_probs, c_transition_probs, c_emission_paras,
        h_prior_probs, h_transition_probs, h_emission_paras) = part_1_a()

        mean, std = h_emission_paras['H1']
        mean_hash = hashlib.sha256(str.encode(str(mean))).hexdigest()
        std_hash = hashlib.sha256(str.encode(str(std))).hexdigest()
        msg = 'incorrect mean for letter HOUSE, state H1'
        self.assertEqual("bbb4004ad949f0be888a1923e336d84485cbbace6641a94e09f6380fbc52b9ae", mean_hash, msg)
        msg = 'incorrect std for letter HOUSE, state H1'
        self.assertEqual("17c40ca95ab8e9107a4157365cb34646c64447a9f39cb4447176a736036495b3", std_hash, msg)

        mean, std = h_emission_paras['H2']
        mean_hash = hashlib.sha256(str.encode(str(mean))).hexdigest()
        std_hash = hashlib.sha256(str.encode(str(std))).hexdigest()
        msg = 'incorrect mean for letter HOUSE, state H2'
        self.assertEqual("9f9ac2449c421664c29f3e534b384d10900fe65fc2726941569a614a801f4b47", mean_hash, msg)
        msg = 'incorrect std for letter HOUSE, state H2'
        self.assertEqual("616a46cf184e50b2ff1debd938a19b3f112c2704f07985a3fe13f849bec48288", std_hash, msg)

        mean, std = h_emission_paras['H3']
        mean_hash = hashlib.sha256(str.encode(str(mean))).hexdigest()
        std_hash = hashlib.sha256(str.encode(str(std))).hexdigest()
        msg = 'incorrect mean for letter HOUSE, state H3'
        self.assertEqual("6d2e4be9b46ce8375256cf2bc5e2eb4c38a0fe2c6ae02f32a1e1955305cf3809", mean_hash, msg)
        msg = 'incorrect std for letter HOUSE, state H3'
        self.assertEqual("966d64084414dc3ce0e395a8ed417665a82b21e6f9858e4168d3578585042cc4", std_hash, msg)

        print_success_message("test_h_emission")
Пример #2
0
    def test_c_emission(self, part_1_a):
        (b_prior_probs, b_transition_probs, b_emission_paras,
        c_prior_probs, c_transition_probs, c_emission_paras,
        h_prior_probs, h_transition_probs, h_emission_paras) = part_1_a()
        
        mean, std = c_emission_paras['C1']
        mean_hash = hashlib.sha256(str.encode(str(mean))).hexdigest()
        std_hash = hashlib.sha256(str.encode(str(std))).hexdigest()
        msg = 'incorrect mean for letter CAR, state C1'
        self.assertEqual("ece665f5d82dd6570657a9b11736924a97e327adc5ff314be890b2e565193f44", mean_hash, msg)
        msg = 'incorrect std for letter CAR, state C1'
        self.assertEqual("224edb71a15e864dff50b2224ca79bb5eb5179e4b287bdf3a54f6abec1f5be3e", std_hash, msg)

        mean, std = c_emission_paras['C2']
        mean_hash = hashlib.sha256(str.encode(str(mean))).hexdigest()
        std_hash = hashlib.sha256(str.encode(str(std))).hexdigest()
        msg = 'incorrect mean for letter CAR, state C2'
        self.assertEqual("e63f3d3e0ce127bb0afcca123bc00babd29820b15f53a7f9b6a31534a4fb0597", mean_hash, msg)
        msg = 'incorrect std for letter CAR, state C2'
        self.assertEqual("8f65223004a75f44404f485a1e84090699acef51f39de9411d6d9b377ae859a5", std_hash, msg)

        mean, std = c_emission_paras['C3']
        mean_hash = hashlib.sha256(str.encode(str(mean))).hexdigest()
        std_hash = hashlib.sha256(str.encode(str(std))).hexdigest()
        msg = 'incorrect mean for letter CAR, state C3'
        self.assertEqual("51489ee602434160b5c1cfc98a781353eb98db3b0fee064b951ba5baa4c9a014", mean_hash, msg)
        msg = 'incorrect std for letter CAR, state C3'
        self.assertEqual("6031bf9944ad15cdfcb096f4432643b7c097da0f179e7d584a016724d9338c98", std_hash, msg)

        print_success_message("test_c_emission")
Пример #3
0
 def test_b_emission(self, part_1_a):
     (b_prior_probs, b_transition_probs, b_emission_paras,
     c_prior_probs, c_transition_probs, c_emission_paras,
     h_prior_probs, h_transition_probs, h_emission_paras) = part_1_a()
     
     mean, std = b_emission_paras['B1']
     mean_hash = hashlib.sha256(str.encode(str(mean))).hexdigest()
     std_hash = hashlib.sha256(str.encode(str(std))).hexdigest()
     msg = 'incorrect mean for letter BUY, state B1'
     self.assertEqual("fc12f049f5c759702b7bcd27d461fb57a4c9176bebfab3e4d15426a74c911d03", mean_hash, msg)
     msg = 'incorrect std for letter BUY, state B1'
     self.assertEqual("9b62d9c6eac8cbacdc2ccdfed1d60feb0716e2b39f5b94eac4bc69f803697ede", std_hash, msg)
     
     mean, std = b_emission_paras['B2']
     mean_hash = hashlib.sha256(str.encode(str(mean))).hexdigest()
     std_hash = hashlib.sha256(str.encode(str(std))).hexdigest()
     msg = 'incorrect mean for letter BUY, state B2'
     self.assertEqual("7a696b9ae0bc3323ce647c690106a78287ec2d5ce24ee5d11f48168bdb1a5dbb", mean_hash, msg)
     msg = 'incorrect std for letter BUY, state B2'
     self.assertEqual("8f3ff2d53dd528ebf1cccbb60667e2a1c0906da993de765634c01e6b5c85b34a", std_hash, msg)
     mean, std = b_emission_paras['B3']
     mean_hash = hashlib.sha256(str.encode(str(mean))).hexdigest()
     std_hash = hashlib.sha256(str.encode(str(std))).hexdigest()
     msg = 'incorrect mean for letter BUY, state B3'
     self.assertEqual("nc308fb57f5aa803bbcddeecac1e547d2b7010018758e72252cb7ceca298e4dbf", mean_hash, msg)
     msg = 'incorrect std for letter BUY, state B3'
     self.assertEqual("69f775cb8dc0f5d96d0c78826f813fc17b99018aee95f8d34e30f7e3f46743ba", std_hash, msg)
     
     print_success_message("test_b_emission")
Пример #4
0
    def test_h_transition(self, part_1_a):
        (b_prior_probs, b_transition_probs, b_emission_paras,
        c_prior_probs, c_transition_probs, c_emission_paras,
        h_prior_probs, h_transition_probs, h_emission_paras) = part_1_a()

        for state, probs in h_transition_probs.items():
            checksum = sum(probs.values())
            msg = ('HOUSE transition prob in state {} should sum to 1 '
                   '(get {})').format(state, checksum)
            self.assertAlmostEqual(1.0, checksum, places=3, msg=msg)
        print_success_message("test_h_transition")
Пример #5
0
    def test_prior(self, part_1_a):
        (b_prior_probs, b_transition_probs, b_emission_paras,
        c_prior_probs, c_transition_probs, c_emission_paras,
        h_prior_probs, h_transition_probs, h_emission_paras) = part_1_a()

        b_prior = sum(b_prior_probs.values())
        c_prior = sum(c_prior_probs.values())
        h_prior = sum(h_prior_probs.values())
        total_prob = b_prior + c_prior + h_prior
        msg = ('incorrect prior probs. each word should be selected with '
               'equal probability. counted {}, should be 1').format(total_prob)
        self.assertAlmostEqual(1.0, total_prob, places=2, msg=msg)
        print_success_message("test_prior")
Пример #6
0
    def setup(self, part_1_a):
        b_states = ['B1', 'B2', 'B3', 'Bend']
        c_states = ['C1', 'C2', 'C3', 'Cend']
        h_states = ['H1', 'H2', 'H3', 'Hend']

        (b_prior_probs, b_transition_probs, b_emission_paras,
        c_prior_probs, c_transition_probs, c_emission_paras,
        h_prior_probs, h_transition_probs, h_emission_paras) = part_1_a()

        states = b_states + c_states + h_states
        prior = b_prior_probs
        prior.update(c_prior_probs)
        prior.update(h_prior_probs)

        trans = b_transition_probs
        trans.update(c_transition_probs)
        trans.update(h_transition_probs)

        emiss = b_emission_paras
        emiss.update(c_emission_paras)
        emiss.update(h_emission_paras)
        return states, prior, trans, emiss