soundex.el 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. ;;; soundex.el --- implement Soundex algorithm
  2. ;; Copyright (C) 1993, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Christian Plaunt <chris@bliss.berkeley.edu>
  4. ;; Maintainer: FSF
  5. ;; Keywords: matching
  6. ;; Created: Sat May 15 14:48:18 1993
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; The Soundex algorithm maps English words into representations of
  20. ;; how they sound. Words with vaguely similar sound map to the same string.
  21. ;;; Code:
  22. (defvar soundex-alist
  23. '((?B . "1") (?F . "1") (?P . "1") (?V . "1")
  24. (?C . "2") (?G . "2") (?J . "2") (?K . "2") (?Q . "2") (?S . "2")
  25. (?X . "2") (?Z . "2") (?D . "3") (?T . "3") (?L . "4") (?M . "5")
  26. (?N . "5") (?R . "6"))
  27. "Alist of chars-to-key-code for building Soundex keys.")
  28. (defun soundex (word)
  29. "Return a Soundex key for WORD.
  30. Implemented as described in:
  31. Knuth, Donald E. \"The Art of Computer Programming, Vol. 3: Sorting
  32. and Searching\", Addison-Wesley (1973), pp. 391-392."
  33. (let* ((word (upcase word)) (length (length word))
  34. (code (cdr (assq (aref word 0) soundex-alist)))
  35. (key (substring word 0 1)) (index 1) (prev-code code))
  36. ;; once we have a four char key, we're done
  37. (while (and (> 4 (length key)) (< index length))
  38. ;; look up the code for each letter in word at index
  39. (setq code (cdr (assq (aref word index) soundex-alist))
  40. index (1+ index)
  41. ;; append code to key unless the same codes belong to
  42. ;; adjacent letters in the original string
  43. key (concat key (if (or (null code) (string= code prev-code))
  44. ()
  45. code))
  46. prev-code code))
  47. ;; return a key that is 4 chars long and padded by "0"s if needed
  48. (if (> 4 (length key))
  49. (substring (concat key "000") 0 4)
  50. key)))
  51. ;(defvar soundex-test
  52. ; '("Euler" "Gauss" "Hilbert" "Knuth" "Lloyd" "Lukasiewicz"
  53. ; "Ellery" "Ghosh" "Heilbronn" "Kant" "Ladd" "Lissajous")
  54. ; "\n Knuth's names to demonstrate the Soundex algorithm.")
  55. ;
  56. ;(mapcar 'soundex soundex-test)
  57. ;("E460" "G200" "H416" "K530" "L300" "L222"
  58. ; "E460" "G200" "H416" "K530" "L300" "L222")
  59. (provide 'soundex)
  60. ;;; soundex.el ends here