org.apache.commons.codec.language

Class Soundex

Implemented Interfaces:
Encoder, StringEncoder

public class Soundex
extends Object
implements StringEncoder

Encodes a string into a Soundex value. Soundex is an encoding used to relate similar names, but can also be used as a general purpose scheme to find word with similar phonemes.

Version:
$Id: Soundex.java,v 1.26 2004/07/07 23:15:24 ggregory Exp $

Author:
Apache Software Foundation

Nested Class Summary

Field Summary

static Soundex
US_ENGLISH
An instance of Soundex using the US_ENGLISH_MAPPING mapping.
static char[]
US_ENGLISH_MAPPING
This is a default mapping of the 26 letters used in US English.
static String
US_ENGLISH_MAPPING_STRING
This is a default mapping of the 26 letters used in US English.
private int
maxLength
Deprecated. This feature is not needed since the encoding size must be constant.
private char[]
soundexMapping
Every letter of the alphabet is "mapped" to a numerical value.

Constructor Summary

Soundex()
Creates an instance using US_ENGLISH_MAPPING
Soundex(char[] mapping)
Creates a soundex instance using the given mapping.

Method Summary

int
difference(java.lang.String s1, java.lang.String s2)
Encodes the Strings and returns the number of characters in the two encoded Strings that are the same.
Object
encode(Object pObject)
Encodes an Object using the soundex algorithm.
String
encode(java.lang.String pString)
Encodes a String using the soundex algorithm.
private char
getMappingCode(java.lang.String str, int index)
Used internally by the SoundEx algorithm.
int
getMaxLength()
Deprecated. This feature is not needed since the encoding size must be constant.
private char[]
getSoundexMapping()
Returns the soundex mapping.
private char
map(char ch)
Maps the given upper-case character to it's Soudex code.
void
setMaxLength(int maxLength)
Deprecated. This feature is not needed since the encoding size must be constant.
private void
setSoundexMapping(char[] soundexMapping)
Sets the soundexMapping.
String
soundex(java.lang.String str)
Retreives the Soundex code for a given String object.

Field Details

US_ENGLISH

public static final Soundex US_ENGLISH
An instance of Soundex using the US_ENGLISH_MAPPING mapping.

See Also:
US_ENGLISH_MAPPING


US_ENGLISH_MAPPING

public static final char[] US_ENGLISH_MAPPING
This is a default mapping of the 26 letters used in US English. A value of 0 for a letter position means do not encode.

See Also:
Soundex(char[])


US_ENGLISH_MAPPING_STRING

public static final String US_ENGLISH_MAPPING_STRING
This is a default mapping of the 26 letters used in US English. A value of 0 for a letter position means do not encode.

(This constant is provided as both an implementation convenience and to allow Javadoc to pick up the value for the constant values page.)

Field Value:
"01230120022455012623010202"

See Also:
US_ENGLISH_MAPPING


maxLength

private int maxLength

Deprecated. This feature is not needed since the encoding size must be constant. Will be removed in 2.0.

The maximum length of a Soundex code - Soundex codes are only four characters by definition.


soundexMapping

private char[] soundexMapping
Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each letter is mapped. This implementation contains a default map for US_ENGLISH

Constructor Details

Soundex

public Soundex()
Creates an instance using US_ENGLISH_MAPPING

See Also:
Soundex(char[]), US_ENGLISH_MAPPING


Soundex

public Soundex(char[] mapping)
Creates a soundex instance using the given mapping. This constructor can be used to provide an internationalized mapping for a non-Western character set. Every letter of the alphabet is "mapped" to a numerical value. This char array holds the values to which each letter is mapped. This implementation contains a default map for US_ENGLISH

Parameters:
mapping - Mapping array to use when finding the corresponding code for a given character

Method Details

difference

public int difference(java.lang.String s1,
                      java.lang.String s2)
            throws EncoderException
Encodes the Strings and returns the number of characters in the two encoded Strings that are the same. This return value ranges from 0 through 4: 0 indicates little or no similarity, and 4 indicates strong similarity or identical values.

Parameters:
s1 - A String that will be encoded and compared.
s2 - A String that will be encoded and compared.

Returns:
The number of characters in the two encoded Strings that are the same from 0 to 4.

Throws:
EncoderException - if an error occurs encoding one of the strings

Since:
1.3

See Also:
SoundexUtils.difference(StringEncoder,String,String), MS T-SQL DIFFERENCE


encode

public Object encode(Object pObject)
            throws EncoderException
Encodes an Object using the soundex algorithm. This method is provided in order to satisfy the requirements of the Encoder interface, and will throw an EncoderException if the supplied object is not of type java.lang.String.
Specified by:
encode in interface Encoder

Parameters:
pObject - Object to encode

Returns:
An object (or type java.lang.String) containing the soundex code which corresponds to the String supplied.

Throws:
EncoderException - if the parameter supplied is not of type java.lang.String


encode

public String encode(java.lang.String pString)
Encodes a String using the soundex algorithm.
Specified by:
encode in interface StringEncoder

Parameters:
pString - A String object to encode

Returns:
A Soundex code corresponding to the String supplied


getMappingCode

private char getMappingCode(java.lang.String str,
                            int index)
Used internally by the SoundEx algorithm. Consonants from the same code group separated by W or H are treated as one.

Parameters:
str - the cleaned working string to encode (in upper case).
index - the character position to encode

Returns:
Mapping code for a particular character


getMaxLength

public int getMaxLength()

Deprecated. This feature is not needed since the encoding size must be constant. Will be removed in 2.0.

Returns the maxLength. Standard Soundex

Returns:
int


getSoundexMapping

private char[] getSoundexMapping()
Returns the soundex mapping.

Returns:
soundexMapping.


map

private char map(char ch)
Maps the given upper-case character to it's Soudex code.

Parameters:
ch - An upper-case character.

Returns:
A Soundex code.


setMaxLength

public void setMaxLength(int maxLength)

Deprecated. This feature is not needed since the encoding size must be constant. Will be removed in 2.0.

Sets the maxLength.

Parameters:
maxLength - The maxLength to set


setSoundexMapping

private void setSoundexMapping(char[] soundexMapping)
Sets the soundexMapping.

Parameters:
soundexMapping - The soundexMapping to set.


soundex

public String soundex(java.lang.String str)
Retreives the Soundex code for a given String object.

Parameters:
str - String to encode using the Soundex algorithm

Returns:
A soundex code for the String supplied


commons-codec version 1.3 - Copyright © 2002-2004 - Apache Software Foundation