home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_mlb.zip / Text / Soundex.pm < prev    next >
Text File  |  1997-11-25  |  4KB  |  149 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. sub soundex
  43. {
  44.   local (@s, $f, $fc, $_) = @_;
  45.  
  46.   push @s, '' unless @s;    # handle no args as a single empty string
  47.  
  48.   foreach (@s)
  49.   {
  50.     $_ = uc $_;
  51.     tr/A-Z//cd;
  52.  
  53.     if ($_ eq '')
  54.     {
  55.       $_ = $soundex_nocode;
  56.     }
  57.     else
  58.     {
  59.       ($f) = /^(.)/;
  60.       tr/AEHIOUWYBFPVCGJKQSXZDTLMNR/00000000111122222222334556/;
  61.       ($fc) = /^(.)/;
  62.       s/^$fc+//;
  63.       tr///cs;
  64.       tr/0//d;
  65.       $_ = $f . $_ . '000';
  66.       s/^(.{4}).*/$1/;
  67.     }
  68.   }
  69.  
  70.   wantarray ? @s : shift @s;
  71. }
  72.  
  73. 1;
  74.  
  75. __END__
  76.  
  77. =head1 NAME
  78.  
  79. Text::Soundex - Implementation of the Soundex Algorithm as Described by Knuth
  80.  
  81. =head1 SYNOPSIS
  82.  
  83.   use Text::Soundex;
  84.  
  85.   $code = soundex $string;            # get soundex code for a string
  86.   @codes = soundex @list;             # get list of codes for list of strings
  87.  
  88.   # set value to be returned for strings without soundex code
  89.  
  90.   $soundex_nocode = 'Z000';
  91.  
  92. =head1 DESCRIPTION
  93.  
  94. This module implements the soundex algorithm as described by Donald Knuth
  95. in Volume 3 of B<The Art of Computer Programming>.  The algorithm is
  96. intended to hash words (in particular surnames) into a small space using a
  97. simple model which approximates the sound of the word when spoken by an English
  98. speaker.  Each word is reduced to a four character string, the first
  99. character being an upper case letter and the remaining three being digits.
  100.  
  101. If there is no soundex code representation for a string then the value of
  102. C<$soundex_nocode> is returned.  This is initially set to C<undef>, but
  103. many people seem to prefer an I<unlikely> value like C<Z000>
  104. (how unlikely this is depends on the data set being dealt with.)  Any value
  105. can be assigned to C<$soundex_nocode>.
  106.  
  107. In scalar context C<soundex> returns the soundex code of its first
  108. argument, and in array context a list is returned in which each element is the 
  109. soundex code for the corresponding argument passed to C<soundex> e.g.
  110.  
  111.   @codes = soundex qw(Mike Stok);
  112.  
  113. leaves C<@codes> containing C<('M200', 'S320')>.
  114.  
  115. =head1 EXAMPLES
  116.  
  117. Knuth's examples of various names and the soundex codes they map to
  118. are listed below:
  119.  
  120.   Euler, Ellery -> E460
  121.   Gauss, Ghosh -> G200
  122.   Hilbert, Heilbronn -> H416
  123.   Knuth, Kant -> K530
  124.   Lloyd, Ladd -> L300
  125.   Lukasiewicz, Lissajous -> L222
  126.  
  127. so:
  128.  
  129.   $code = soundex 'Knuth';              # $code contains 'K530'
  130.   @list = soundex qw(Lloyd Gauss);    # @list contains 'L300', 'G200'
  131.  
  132. =head1 LIMITATIONS
  133.  
  134. As the soundex algorithm was originally used a B<long> time ago in the US
  135. it considers only the English alphabet and pronunciation.
  136.  
  137. As it is mapping a large space (arbitrary length strings) onto a small
  138. space (single letter plus 3 digits) no inference can be made about the
  139. similarity of two strings which end up with the same soundex code.  For 
  140. example, both C<Hilbert> and C<Heilbronn> end up with a soundex code
  141. of C<H416>.
  142.  
  143. =head1 AUTHOR
  144.  
  145. This code was implemented by Mike Stok (C<stok@cybercom.net>) from the 
  146. description given by Knuth.  Ian Phillips (C<ian@pipex.net>) and Rich Pinder 
  147. (C<rpinder@hsc.usc.edu>) supplied ideas and spotted mistakes.
  148.