home *** CD-ROM | disk | FTP | other *** search
/ OpenStep 4.2 / Openstep-4.2-Intel-User.iso / usr / lib / perl5 / Text / Soundex.pm < prev    next >
Text File  |  1997-03-29  |  2KB  |  83 lines

  1. package Text::Soundex;
  2. require 5.000;
  3. require Exporter;
  4.  
  5. @ISA = qw(Exporter);
  6. @EXPORT = qw(&soundex $soundex_nocode);
  7.  
  8. # $Id: soundex.pl,v 1.2 1994/03/24 00:30:27 mike Exp $
  9. #
  10. # Implementation of soundex algorithm as described by Knuth in volume
  11. # 3 of The Art of Computer Programming, with ideas stolen from Ian
  12. # Phillips <ian@pipex.net>.
  13. #
  14. # Mike Stok <Mike.Stok@meiko.concord.ma.us>, 2 March 1994.
  15. #
  16. # Knuth's test cases are:
  17. # Euler, Ellery -> E460
  18. # Gauss, Ghosh -> G200
  19. # Hilbert, Heilbronn -> H416
  20. # Knuth, Kant -> K530
  21. # Lloyd, Ladd -> L300
  22. # Lukasiewicz, Lissajous -> L222
  23. #
  24. # $Log: soundex.pl,v $
  25. # Revision 1.2  1994/03/24  00:30:27  mike
  26. # Subtle bug (any excuse :-) spotted by Rich Pinder <rpinder@hsc.usc.edu>
  27. # in the way I handles leasing characters which were different but had
  28. # the same soundex code.  This showed up comparing it with Oracle's
  29. # soundex output.
  30. #
  31. # Revision 1.1  1994/03/02  13:01:30  mike
  32. # Initial revision
  33. #
  34. #
  35. ##############################################################################
  36.  
  37. # $soundex_nocode is used to indicate a string doesn't have a soundex
  38. # code, I like undef other people may want to set it to 'Z000'.
  39.  
  40. $soundex_nocode = undef;
  41.  
  42. # soundex
  43. #
  44. # usage:
  45. #
  46. # @codes = &soundex (@wordList);
  47. # $code = &soundex ($word);
  48. #
  49. # This strenuously avoids 0
  50.  
  51. sub soundex
  52. {
  53.   local (@s, $f, $fc, $_) = @_;
  54.  
  55.   foreach (@s)
  56.   {
  57.     tr/a-z/A-Z/;
  58.     tr/A-Z//cd;
  59.  
  60.     if ($_ eq '')
  61.     {
  62.       $_ = $soundex_nocode;
  63.     }
  64.     else
  65.     {
  66.       ($f) = /^(.)/;
  67.       tr/AEHIOUWYBFPVCGJKQSXZDTLMNR/00000000111122222222334556/;
  68.       ($fc) = /^(.)/;
  69.       s/^$fc+//;
  70.       tr///cs;
  71.       tr/0//d;
  72.       $_ = $f . $_ . '000';
  73.       s/^(.{4}).*/$1/;
  74.     }
  75.   }
  76.  
  77.   wantarray ? @s : shift @s;
  78. }
  79.  
  80. 1;
  81.  
  82.