Ejemplo n.º 1
0
 def test_trigram_viterbi(self):
     dt = DynamicTable()
     viterbi = ViterbiMath(self._obsT, self._transmBi, self._transmTri, self._tags)
     col = 0
     dt.update(viterbi.get_next_column(dt, 3, col, self._wordSeq[col]))
     col = col + 1
     dt.update(viterbi.get_next_column(dt, 3, col, self._wordSeq[col]))
     expected_table = [{'VB': 0.1, 'NN': 0.1}, {'VB': 0.004000000000000001, 'NN': 0.027999999999999997}] 
     actual_table = dt.probs
     self.assertListEqual(expected_table, actual_table, "do_trigram() probability computation test")
Ejemplo n.º 2
0
    def predict(self, word_seq, n):
        """
        Predicts the tag sequence of a word sequence 'word_seq' using 'n'-gram model.
        ----
        input
            word_seq: a list of word strings to predict the tag sequence
            n: either 2 (bigram) or 3 (trigram)

        returns a list of tag strings of the tag sequence predicted
        """
        seq_size = len(word_seq)
        if seq_size <= 0:
            return []
        dynamic_table = DynamicTable()
        for c in xrange(0,seq_size):
            next_col = self.get_next_column(dynamic_table, n, c, word_seq[c])
            dynamic_table.update(next_col)
        
        last_c = seq_size - 1
        last_col = dynamic_table.probs[last_c]
        last_best_tag = max(last_col, key=last_col.get)
        return dynamic_table.full_path(last_c, last_best_tag)