Example #1
0
    def test_tag_prob2(self):
        tagset = {'D', 'N', 'V'}
        trans = {
            ('<s>', '<s>'): {
                'D': 1.0
            },
            ('<s>', 'D'): {
                'N': 1.0
            },
            ('D', 'N'): {
                'V': 0.8,
                'N': 0.2
            },
            ('N', 'N'): {
                'V': 1.0
            },
            ('N', 'V'): {
                '</s>': 1.0
            },
        }
        out = {
            'D': {
                'the': 1.0
            },
            'N': {
                'dog': 0.4,
                'barks': 0.6
            },
            'V': {
                'dog': 0.1,
                'barks': 0.9
            },
        }
        hmm = HMM(3, tagset, trans, out)

        y = 'D N V'.split()
        p = hmm.tag_prob(y)
        self.assertAlmostEqual(p, 0.8)

        lp = hmm.tag_log_prob(y)
        self.assertAlmostEqual(lp, log2(0.8))
Example #2
0
    def test_tag_prob(self):
        tagset = {'D', 'N', 'V'}
        trans = {
            ('<s>', '<s>'): {'D': 1.0},
            ('<s>', 'D'): {'N': 1.0},
            ('D', 'N'): {'V': 1.0},
            ('N', 'V'): {'</s>': 1.0},
        }
        out = {
            'D': {'the': 1.0},
            'N': {'dog': 0.4, 'barks': 0.6},
            'V': {'dog': 0.1, 'barks': 0.9},
        }
        hmm = HMM(3, tagset, trans, out)

        y = 'D N V'.split()
        p = hmm.tag_prob(y)
        self.assertAlmostEqual(p, 1.0)

        lp = hmm.tag_log_prob(y)
        self.assertAlmostEqual(lp, log2(1.0))