Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def editDistance(r, h):
- '''
- This function is to calculate the edit distance of reference sentence and the hypothesis sentence.
- Main algorithm used is dynamic programming.
- Attributes:
- r -> the list of words produced by splitting reference sentence.
- h -> the list of words produced by splitting hypothesis sentence.
- '''
- d = numpy.zeros((len(r)+1)*(len(h)+1), dtype=numpy.uint8).reshape((len(r)+1, len(h)+1))
- for i in range(len(r)+1):
- for j in range(len(h)+1):
- if i == 0:
- d[0][j] = j
- elif j == 0:
- d[i][0] = i
- for i in range(1, len(r)+1):
- for j in range(1, len(h)+1):
- if r[i-1] == h[j-1]:
- d[i][j] = d[i-1][j-1]
- else:
- substitute = d[i-1][j-1] + 1
- insert = d[i][j-1] + 1
- delete = d[i-1][j] + 1
- d[i][j] = min(substitute, insert, delete)
- return d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement