Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

148 lines
4.1 KiB

  1. package Text::Soundex;
  2. require 5.000;
  3. require Exporter;
  4. @ISA = qw(Exporter);
  5. @EXPORT = qw(&soundex $soundex_nocode);
  6. # $Id: soundex.pl,v 1.2 1994/03/24 00:30:27 mike Exp $
  7. #
  8. # Implementation of soundex algorithm as described by Knuth in volume
  9. # 3 of The Art of Computer Programming, with ideas stolen from Ian
  10. # Phillips <[email protected]>.
  11. #
  12. # Mike Stok <[email protected]>, 2 March 1994.
  13. #
  14. # Knuth's test cases are:
  15. #
  16. # Euler, Ellery -> E460
  17. # Gauss, Ghosh -> G200
  18. # Hilbert, Heilbronn -> H416
  19. # Knuth, Kant -> K530
  20. # Lloyd, Ladd -> L300
  21. # Lukasiewicz, Lissajous -> L222
  22. #
  23. # $Log: soundex.pl,v $
  24. # Revision 1.2 1994/03/24 00:30:27 mike
  25. # Subtle bug (any excuse :-) spotted by Rich Pinder <[email protected]>
  26. # in the way I handles leasing characters which were different but had
  27. # the same soundex code. This showed up comparing it with Oracle's
  28. # soundex output.
  29. #
  30. # Revision 1.1 1994/03/02 13:01:30 mike
  31. # Initial revision
  32. #
  33. #
  34. ##############################################################################
  35. # $soundex_nocode is used to indicate a string doesn't have a soundex
  36. # code, I like undef other people may want to set it to 'Z000'.
  37. $soundex_nocode = undef;
  38. sub soundex
  39. {
  40. local (@s, $f, $fc, $_) = @_;
  41. push @s, '' unless @s; # handle no args as a single empty string
  42. foreach (@s)
  43. {
  44. $_ = uc $_;
  45. tr/A-Z//cd;
  46. if ($_ eq '')
  47. {
  48. $_ = $soundex_nocode;
  49. }
  50. else
  51. {
  52. ($f) = /^(.)/;
  53. tr/AEHIOUWYBFPVCGJKQSXZDTLMNR/00000000111122222222334556/;
  54. ($fc) = /^(.)/;
  55. s/^$fc+//;
  56. tr///cs;
  57. tr/0//d;
  58. $_ = $f . $_ . '000';
  59. s/^(.{4}).*/$1/;
  60. }
  61. }
  62. wantarray ? @s : shift @s;
  63. }
  64. 1;
  65. __END__
  66. =head1 NAME
  67. Text::Soundex - Implementation of the Soundex Algorithm as Described by Knuth
  68. =head1 SYNOPSIS
  69. use Text::Soundex;
  70. $code = soundex $string; # get soundex code for a string
  71. @codes = soundex @list; # get list of codes for list of strings
  72. # set value to be returned for strings without soundex code
  73. $soundex_nocode = 'Z000';
  74. =head1 DESCRIPTION
  75. This module implements the soundex algorithm as described by Donald Knuth
  76. in Volume 3 of B<The Art of Computer Programming>. The algorithm is
  77. intended to hash words (in particular surnames) into a small space using a
  78. simple model which approximates the sound of the word when spoken by an English
  79. speaker. Each word is reduced to a four character string, the first
  80. character being an upper case letter and the remaining three being digits.
  81. If there is no soundex code representation for a string then the value of
  82. C<$soundex_nocode> is returned. This is initially set to C<undef>, but
  83. many people seem to prefer an I<unlikely> value like C<Z000>
  84. (how unlikely this is depends on the data set being dealt with.) Any value
  85. can be assigned to C<$soundex_nocode>.
  86. In scalar context C<soundex> returns the soundex code of its first
  87. argument, and in array context a list is returned in which each element is the
  88. soundex code for the corresponding argument passed to C<soundex> e.g.
  89. @codes = soundex qw(Mike Stok);
  90. leaves C<@codes> containing C<('M200', 'S320')>.
  91. =head1 EXAMPLES
  92. Knuth's examples of various names and the soundex codes they map to
  93. are listed below:
  94. Euler, Ellery -> E460
  95. Gauss, Ghosh -> G200
  96. Hilbert, Heilbronn -> H416
  97. Knuth, Kant -> K530
  98. Lloyd, Ladd -> L300
  99. Lukasiewicz, Lissajous -> L222
  100. so:
  101. $code = soundex 'Knuth'; # $code contains 'K530'
  102. @list = soundex qw(Lloyd Gauss); # @list contains 'L300', 'G200'
  103. =head1 LIMITATIONS
  104. As the soundex algorithm was originally used a B<long> time ago in the US
  105. it considers only the English alphabet and pronunciation.
  106. As it is mapping a large space (arbitrary length strings) onto a small
  107. space (single letter plus 3 digits) no inference can be made about the
  108. similarity of two strings which end up with the same soundex code. For
  109. example, both C<Hilbert> and C<Heilbronn> end up with a soundex code
  110. of C<H416>.
  111. =head1 AUTHOR
  112. This code was implemented by Mike Stok (C<[email protected]>) from the
  113. description given by Knuth. Ian Phillips (C<[email protected]>) and Rich Pinder
  114. (C<[email protected]>) supplied ideas and spotted mistakes.