in is21_deep_bias/score.py [0:0]
def align(self, refs, hyps):
if len(refs) == 0 and len(hyps) == 0:
raise ValueError("Doesn't support empty ref AND hyp!")
# NOTE: we're not resetting the values in these matrices because every value
# will be overridden in the loop below. If this assumption doesn't hold,
# be sure to set all entries in self.scores_ and self.backtraces_ to 0.
self.scores_ = [[0.0] * (len(hyps) + 1) for _ in range(len(refs) + 1)]
self.backtraces_ = [[0] * (len(hyps) + 1) for _ in range(len(refs) + 1)]
num_rows, num_cols = len(self.scores_), len(self.scores_[0])
for i in range(num_rows):
for j in range(num_cols):
if i == 0 and j == 0:
self.scores_[i][j] = 0.0
self.backtraces_[i][j] = 0
continue
if i == 0:
self.scores_[i][j] = self.scores_[i][j - 1] + self.cost(
None, hyps[j - 1], Code.insertion
)
self.backtraces_[i][j] = coordinate_to_offset(i, j - 1, num_cols)
continue
if j == 0:
self.scores_[i][j] = self.scores_[i - 1][j] + self.cost(
refs[i - 1], None, Code.deletion
)
self.backtraces_[i][j] = coordinate_to_offset(i - 1, j, num_cols)
continue
# Below here both i and j are greater than 0
ref = refs[i - 1]
hyp = hyps[j - 1]
best_score = self.scores_[i - 1][j - 1] + (
self.cost(ref, hyp, Code.match)
if ref == hyp
else self.cost(ref, hyp, Code.substitution)
)
prev_row = i - 1
prev_col = j - 1
ins = self.scores_[i][j - 1] + self.cost(None, hyp, Code.insertion)
if ins < best_score:
best_score = ins
prev_row = i
prev_col = j - 1
delt = self.scores_[i - 1][j] + self.cost(ref, None, Code.deletion)
if delt < best_score:
best_score = delt
prev_row = i - 1
prev_col = j
self.scores_[i][j] = best_score
self.backtraces_[i][j] = coordinate_to_offset(
prev_row, prev_col, num_cols
)
return self.get_result(refs, hyps)