<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;"># -*- perl -*-

# (c) Copyright 1998-2007 by Mark Mielke
#
# Freedom to use these sources for whatever you want, as long as credit
# is given where credit is due, is hereby granted. You may make modifications
# where you see fit but leave this copyright somewhere visible. As well, try
# to initial any changes you make so that if I like the changes I can
# incorporate them into later versions.
#
#      - Mark Mielke &lt;mark@mielke.cc&gt;
#

package Text::Soundex;
require 5.006;

use Exporter ();
use XSLoader ();

use strict;

use if $] &gt; 5.016, 'deprecate';

our $VERSION   = '3.04';
our @EXPORT_OK = qw(soundex soundex_unicode soundex_nara soundex_nara_unicode
                    $soundex_nocode);
our @EXPORT    = qw(soundex soundex_nara $soundex_nocode);
our @ISA       = qw(Exporter);

our $nocode;

# Previous releases of Text::Soundex made $nocode available as $soundex_nocode.
# For now, this part of the interface is exported and maintained.
# In the feature, $soundex_nocode will be deprecated.
*Text::Soundex::soundex_nocode = \$nocode;

sub soundex_noxs
{
    # Original Soundex algorithm

    my @results = map {
        my $code = uc($_);
        $code =~ tr/AaEeHhIiOoUuWwYyBbFfPpVvCcGgJjKkQqSsXxZzDdTtLlMmNnRr//cd;

	if (length($code)) {
            my $firstchar = substr($code, 0, 1);
	    $code =~ tr[AaEeHhIiOoUuWwYyBbFfPpVvCcGgJjKkQqSsXxZzDdTtLlMmNnRr]
                       [0000000000000000111111112222222222222222333344555566]s;
	    ($code = substr($code, 1)) =~ tr/0//d;
	    substr($firstchar . $code . '000', 0, 4);
	} else {
	    $nocode;
	}
    } @_;

    wantarray ? @results : $results[0];
}

sub soundex_nara
{
    # US census (NARA) algorithm.

    my @results = map {
	my $code = uc($_);
        $code =~ tr/AaEeHhIiOoUuWwYyBbFfPpVvCcGgJjKkQqSsXxZzDdTtLlMmNnRr//cd;

	if (length($code)) {
            my $firstchar = substr($code, 0, 1);
	    $code =~ tr[AaEeHhIiOoUuWwYyBbFfPpVvCcGgJjKkQqSsXxZzDdTtLlMmNnRr]
                       [0000990000009900111111112222222222222222333344555566]s;
            $code =~ s/(.)9\1/$1/gs;
	    ($code = substr($code, 1)) =~ tr/09//d;
	    substr($firstchar . $code . '000', 0, 4);
	} else {
	    $nocode
	}
    } @_;

    wantarray ? @results : $results[0];
}

sub soundex_unicode
{
    require Text::Unidecode unless defined &amp;Text::Unidecode::unidecode;
    soundex(Text::Unidecode::unidecode(@_));
}

sub soundex_nara_unicode
{
    require Text::Unidecode unless defined &amp;Text::Unidecode::unidecode;
    soundex_nara(Text::Unidecode::unidecode(@_));
}

eval { XSLoader::load(__PACKAGE__, $VERSION) };

if (defined(&amp;soundex_xs)) {
    *soundex = \&amp;soundex_xs;
} else {
    *soundex = \&amp;soundex_noxs;
    *soundex_xs = sub {
        require Carp;
        Carp::croak("XS implementation of Text::Soundex::soundex_xs() ".
                    "could not be loaded");
    };
}

1;

__END__

# Implementation of the soundex algorithm.
#
# Some of this documention was written by Mike Stok.
#
# Examples:
#
# Euler, Ellery -&gt; E460
# Gauss, Ghosh -&gt; G200
# Hilbert, Heilbronn -&gt; H416
# Knuth, Kant -&gt; K530
# Lloyd, Ladd -&gt; L300
# Lukasiewicz, Lissajous -&gt; L222
#

=head1 NAME

Text::Soundex - Implementation of the soundex algorithm.

=head1 SYNOPSIS

  use Text::Soundex;

  # Original algorithm.
  $code = soundex($name);    # Get the soundex code for a name.
  @codes = soundex(@names);  # Get the list of codes for a list of names.

  # American Soundex variant (NARA) - Used for US census data.
  $code = soundex_nara($name);    # Get the soundex code for a name.
  @codes = soundex_nara(@names);  # Get the list of codes for a list of names.

  # Redefine the value that soundex() will return if the input string
  # contains no identifiable sounds within it.
  $Text::Soundex::nocode = 'Z000';

=head1 DESCRIPTION

Soundex is a phonetic algorithm for indexing names by sound, as
pronounced in English. The goal is for names with the same
pronunciation to be encoded to the same representation so that they
can be matched despite minor differences in spelling. Soundex is the
most widely known of all phonetic algorithms and is often used
(incorrectly) as a synonym for "phonetic algorithm". Improvements to
Soundex are the basis for many modern phonetic algorithms. (Wikipedia,
2007)

This module implements the original soundex algorithm developed by
Robert Russell and Margaret Odell, patented in 1918 and 1922, as well
as a variation called "American Soundex" used for US census data, and
current maintained by the National Archives and Records Administration
(NARA).

The soundex algorithm may be recognized from Donald Knuth's
B&lt;The Art of Computer Programming&gt;. The algorithm described by
Knuth is the NARA algorithm.

The value returned for strings which have no soundex encoding is
defined using C&lt;$Text::Soundex::nocode&gt;. The default value is C&lt;undef&gt;,
however values such as C&lt;'Z000'&gt; are commonly used alternatives.

For backward compatibility with older versions of this module the
C&lt;$Text::Soundex::nocode&gt; is exported into the caller's namespace as
C&lt;$soundex_nocode&gt;.

In scalar context, C&lt;soundex()&gt; returns the soundex code of its first
argument. In list context, a list is returned in which each element is the
soundex code for the corresponding argument passed to C&lt;soundex()&gt;. For
example, the following code assigns @codes the value C&lt;('M200', 'S320')&gt;:

   @codes = soundex qw(Mike Stok);

To use C&lt;Text::Soundex&gt; to generate codes that can be used to search one
of the publically available US Censuses, a variant of the soundex
algorithm must be used:

    use Text::Soundex;
    $code = soundex_nara($name);

An example of where these algorithm differ follows:

    use Text::Soundex;
    print soundex("Ashcraft"), "\n";       # prints: A226
    print soundex_nara("Ashcraft"), "\n";  # prints: A261

=head1 EXAMPLES

Donald Knuth's examples of names and the soundex codes they map to
are listed below:

  Euler, Ellery -&gt; E460
  Gauss, Ghosh -&gt; G200
  Hilbert, Heilbronn -&gt; H416
  Knuth, Kant -&gt; K530
  Lloyd, Ladd -&gt; L300
  Lukasiewicz, Lissajous -&gt; L222

so:

  $code = soundex 'Knuth';         # $code contains 'K530'
  @list = soundex qw(Lloyd Gauss); # @list contains 'L300', 'G200'

=head1 LIMITATIONS

As the soundex algorithm was originally used a B&lt;long&gt; time ago in the US
it considers only the English alphabet and pronunciation. In particular,
non-ASCII characters will be ignored. The recommended method of dealing
with characters that have accents, or other unicode characters, is to use
the Text::Unidecode module available from CPAN. Either use the module
explicitly:

    use Text::Soundex;
    use Text::Unidecode;

    print soundex(unidecode("Fran\xE7ais")), "\n"; # Prints "F652\n"

Or use the convenient wrapper routine:

    use Text::Soundex 'soundex_unicode';

    print soundex_unicode("Fran\xE7ais"), "\n";    # Prints "F652\n"

Since the soundex algorithm maps a large space (strings of arbitrary
length) onto a small space (single letter plus 3 digits) no inference
can be made about the similarity of two strings which end up with the
same soundex code.  For example, both C&lt;Hilbert&gt; and C&lt;Heilbronn&gt; end
up with a soundex code of C&lt;H416&gt;.

=head1 MAINTAINER

This module is currently maintain by Mark Mielke (C&lt;mark@mielke.cc&gt;).

=head1 HISTORY

Version 3 is a significant update to provide support for versions of
Perl later than Perl 5.004. Specifically, the XS version of the
soundex() subroutine understands strings that are encoded using UTF-8
(unicode strings).

Version 2 of this module was a re-write by Mark Mielke (C&lt;mark@mielke.cc&gt;)
to improve the speed of the subroutines. The XS version of the soundex()
subroutine was introduced in 2.00.

Version 1 of this module was written by Mike Stok (C&lt;mike@stok.co.uk&gt;)
and was included into the Perl core library set.

Dave Carlsen (C&lt;dcarlsen@csranet.com&gt;) made the request for the NARA
algorithm to be included. The NARA soundex page can be viewed at:
C&lt;http://www.nara.gov/genealogy/soundex/soundex.html&gt;

Ian Phillips (C&lt;ian@pipex.net&gt;) and Rich Pinder (C&lt;rpinder@hsc.usc.edu&gt;)
supplied ideas and spotted mistakes for v1.x.

=cut
</pre></body></html>