levenshtein.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /**********
  2. This file is part of levenshpp.
  3. levenshpp is free library: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. levenshpp is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with levenshpp. If not, see <http://www.gnu.org/licenses/>.
  13. **********/
  14. #pragma once
  15. #include <utility>
  16. #include <queue>
  17. static unsigned int levenshtein_get(unsigned int * const *map, int px, int py)
  18. {
  19. if (px == -1 && py == -1)
  20. return 0;
  21. if (px == -1)
  22. return py +1;
  23. if (py == -1)
  24. return px +1;
  25. return map[px][py];
  26. }
  27. template <class T, typename SIZE=unsigned int>
  28. unsigned int levenshtein(const T *a, const T *b, const SIZE aSize, const SIZE bSize)
  29. {
  30. unsigned int **items = new unsigned int*[aSize]();
  31. for (SIZE i =0; i < aSize; i++)
  32. {
  33. items[i] = new unsigned int[bSize]();
  34. for (SIZE j =0; j < bSize; j++)
  35. {
  36. unsigned int add = levenshtein_get(items, i, j -1) +1;
  37. unsigned int del = levenshtein_get(items, i -1, j) +1;
  38. unsigned int mod = levenshtein_get(items, i -1, j -1) +(a[i] == b[j] ? 0 : 1);
  39. items[i][j] = std::min(std::min(add, del), mod);
  40. }
  41. }
  42. const unsigned int levenshtein = items[aSize -1][bSize -1];
  43. for (SIZE i =0; i < aSize; i++)
  44. delete[] items[i];
  45. delete[] items;
  46. return levenshtein;
  47. }
  48. template <class T, typename SIZE=unsigned int>
  49. unsigned int levenshtein(const T &a, const T &b, const SIZE aSize, const SIZE bSize)
  50. {
  51. unsigned int **items = new unsigned int*[aSize]();
  52. for (SIZE i =0; i < aSize; i++)
  53. {
  54. items[i] = new unsigned int[bSize]();
  55. for (SIZE j =0; j < bSize; j++)
  56. {
  57. unsigned int add = levenshtein_get(items, i, j -1) +1;
  58. unsigned int del = levenshtein_get(items, i -1, j) +1;
  59. unsigned int mod = levenshtein_get(items, i -1, j -1) +(a[i] == b[j] ? 0 : 1);
  60. items[i][j] = std::min(std::min(add, del), mod);
  61. }
  62. }
  63. const unsigned int levenshtein = items[aSize -1][bSize -1];
  64. for (SIZE i =0; i < aSize; i++)
  65. delete[] items[i];
  66. delete[] items;
  67. return levenshtein;
  68. }