wer.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. def wer(r, h):
  3. """
  4. Calculation of WER with Levenshtein distance.
  5. Works only for iterables up to 254 elements (uint8).
  6. O(nm) time ans space complexity.
  7. >>> wer("who is there".split(), "is there".split())
  8. 1
  9. >>> wer("who is there".split(), "".split())
  10. 3
  11. >>> wer("".split(), "who is there".split())
  12. 3
  13. """
  14. # initialisation
  15. import numpy
  16. d = numpy.zeros((len(r)+1)*(len(h)+1), dtype=numpy.uint8)
  17. d = d.reshape((len(r)+1, len(h)+1))
  18. for i in range(len(r)+1):
  19. for j in range(len(h)+1):
  20. if i == 0:
  21. d[0][j] = j
  22. elif j == 0:
  23. d[i][0] = i
  24. # computation
  25. for i in range(1, len(r)+1):
  26. for j in range(1, len(h)+1):
  27. if r[i-1] == h[j-1]:
  28. d[i][j] = d[i-1][j-1]
  29. else:
  30. substitution = d[i-1][j-1] + 1
  31. insertion = d[i][j-1] + 1
  32. deletion = d[i-1][j] + 1
  33. d[i][j] = min(substitution, insertion, deletion)
  34. return d[len(r)][len(h)]
  35. if __name__ == "__main__":
  36. import doctest
  37. doctest.testmod()