Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.96 KB | None | 0 0
  1. def editDistance(r, h):
  2. '''
  3. This function is to calculate the edit distance of reference sentence and the hypothesis sentence.
  4.  
  5. Main algorithm used is dynamic programming.
  6.  
  7. Attributes:
  8. r -> the list of words produced by splitting reference sentence.
  9. h -> the list of words produced by splitting hypothesis sentence.
  10. '''
  11. d = numpy.zeros((len(r)+1)*(len(h)+1), dtype=numpy.uint8).reshape((len(r)+1, len(h)+1))
  12.  
  13. for i in range(len(r)+1):
  14. for j in range(len(h)+1):
  15. if i == 0:
  16. d[0][j] = j
  17. elif j == 0:
  18. d[i][0] = i
  19.  
  20. for i in range(1, len(r)+1):
  21. for j in range(1, len(h)+1):
  22. if r[i-1] == h[j-1]:
  23. d[i][j] = d[i-1][j-1]
  24. else:
  25. substitute = d[i-1][j-1] + 1
  26. insert = d[i][j-1] + 1
  27. delete = d[i-1][j] + 1
  28. d[i][j] = min(substitute, insert, delete)
  29. return d
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement