Example #1
0
    def test_inner_replacement_rev(self):
        a = ['a', 'x', 'y', 'c']
        b = ['a', 'b', 'c']

        self.assertTrue(
            levenshtein_alignment_path(a, b) in [
                [0, 1, 0, 0],
                [0, 0, 1, 0],
            ])
Example #2
0
def add_hypothese(cn, transcript, score):
    if cn == []:
        cn = []
        for symbol in transcript:
            cn.append({symbol: score})

        return cn

    pivot = get_pivot(cn)
    alignment = levenshtein_alignment_path(list(transcript), pivot)
    cn_total_weight = sum(sum(position.values()) for position in cn) / len(cn)

    cn_pointer = 0
    tr_pointer = 0
    for direction in alignment:
        if direction == -1:  # move in the confusion network
            if None in cn[cn_pointer]:
                cn[cn_pointer][None] += score
            else:
                cn[cn_pointer][None] = score
            cn_pointer += 1
        elif direction == 0:  # move in both
            tr_sym = transcript[tr_pointer]
            if tr_sym in cn[cn_pointer]:
                cn[cn_pointer][tr_sym] += score
            else:
                cn[cn_pointer][tr_sym] = score
            tr_pointer += 1
            cn_pointer += 1
        elif direction == 1:  # move in the confusion network
            tr_sym = transcript[tr_pointer]
            if cn_pointer == len(cn):
                cn.append({None: cn_total_weight, tr_sym: score})
            else:
                cn = cn[:cn_pointer] + [{None: cn_total_weight, tr_sym: score}] + cn[cn_pointer:]
                cn_pointer += 1
            tr_pointer += 1
        else:
            raise RuntimeError("Got unexpected direction {}".format(direction))

    return cn
Example #3
0
 def test_alignment_to_eps_rev(self):
     a = ['a', 'b', 'c']
     b = ['a', None, 'c']
     self.assertEqual(levenshtein_alignment_path(a, b), [0, 0, 0])
Example #4
0
 def test_insertion_only(self):
     a = []
     b = ['a', 'b', 'c']
     self.assertEqual(levenshtein_alignment_path(a, b), [-1, -1, -1])
Example #5
0
 def test_deletion_only(self):
     a = ['a', 'b', 'c']
     b = []
     self.assertEqual(levenshtein_alignment_path(a, b), [1, 1, 1])
Example #6
0
 def test_trivial_deletion(self):
     a = ['a', 'b']
     b = ['a']
     self.assertEqual(levenshtein_alignment_path(a, b), [0, 1])
Example #7
0
 def test_trivial_insertion(self):
     a = ['a']
     b = ['b', 'a']
     self.assertEqual(levenshtein_alignment_path(a, b), [-1, 0])
Example #8
0
 def test_trivial_substitution(self):
     a = ['a']
     b = ['b']
     self.assertEqual(levenshtein_alignment_path(a, b), [0])
Example #9
0
 def test_trivial_match(self):
     a = ['a']
     b = ['a']
     self.assertEqual(levenshtein_alignment_path(a, b), [0])