Class Metaphone
- java.lang.Object
-
- org.apache.commons.codec.language.Metaphone
-
- All Implemented Interfaces:
Encoder
,StringEncoder
public class Metaphone extends java.lang.Object implements StringEncoder
Encodes a string into a Metaphone value.Initial Java implementation by William B. Brogden. December, 1997. Permission given by wbrogden for code to be used anywhere.
Hanging on the Metaphone by Lawrence Philips in Computer Language of Dec. 1990, p 39.
Note, that this does not match the algorithm that ships with PHP, or the algorithm found in the Perl implementations:
- Text:Metaphone-1.96 (broken link 4/30/2013)
- Text:Metaphone-1.96 (link checked 4/30/2013)
They have had undocumented changes from the originally published algorithm. For more information, see CODEC-57.
This class is conditionally thread-safe. The instance field for maximum code length is mutable
setMaxCodeLen(int)
but is not volatile, and accesses are not synchronized. If an instance of the class is shared between threads, the caller needs to ensure that suitable synchronization is used to ensure safe publication of the value between threads, and must not invokesetMaxCodeLen(int)
after initial setup.
-
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
FRONTV
Variable used in Metaphone algorithmprivate int
maxCodeLen
The max code length for metaphone is 4private static java.lang.String
VARSON
Variable used in Metaphone algorithmprivate static java.lang.String
VOWELS
Five values in the English language
-
Constructor Summary
Constructors Constructor Description Metaphone()
Creates an instance of the Metaphone encoder
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description java.lang.Object
encode(java.lang.Object obj)
Encodes an Object using the metaphone algorithm.java.lang.String
encode(java.lang.String str)
Encodes a String using the Metaphone algorithm.int
getMaxCodeLen()
Returns the maxCodeLen.private boolean
isLastChar(int wdsz, int n)
boolean
isMetaphoneEqual(java.lang.String str1, java.lang.String str2)
Tests is the metaphones of two strings are identical.private boolean
isNextChar(java.lang.StringBuilder string, int index, char c)
private boolean
isPreviousChar(java.lang.StringBuilder string, int index, char c)
private boolean
isVowel(java.lang.StringBuilder string, int index)
java.lang.String
metaphone(java.lang.String txt)
Find the metaphone value of a String.private boolean
regionMatch(java.lang.StringBuilder string, int index, java.lang.String test)
void
setMaxCodeLen(int maxCodeLen)
Sets the maxCodeLen.
-
-
-
Field Detail
-
VOWELS
private static final java.lang.String VOWELS
Five values in the English language- See Also:
- Constant Field Values
-
FRONTV
private static final java.lang.String FRONTV
Variable used in Metaphone algorithm- See Also:
- Constant Field Values
-
VARSON
private static final java.lang.String VARSON
Variable used in Metaphone algorithm- See Also:
- Constant Field Values
-
maxCodeLen
private int maxCodeLen
The max code length for metaphone is 4
-
-
Method Detail
-
metaphone
public java.lang.String metaphone(java.lang.String txt)
Find the metaphone value of a String. This is similar to the soundex algorithm, but better at finding similar sounding words. All input is converted to upper case. Limitations: Input format is expected to be a single ASCII word with only characters in the A - Z range, no punctuation or numbers.- Parameters:
txt
- String to find the metaphone code for- Returns:
- A metaphone code corresponding to the String supplied
-
isVowel
private boolean isVowel(java.lang.StringBuilder string, int index)
-
isPreviousChar
private boolean isPreviousChar(java.lang.StringBuilder string, int index, char c)
-
isNextChar
private boolean isNextChar(java.lang.StringBuilder string, int index, char c)
-
regionMatch
private boolean regionMatch(java.lang.StringBuilder string, int index, java.lang.String test)
-
isLastChar
private boolean isLastChar(int wdsz, int n)
-
encode
public java.lang.Object encode(java.lang.Object obj) throws EncoderException
Encodes an Object using the metaphone 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 interfaceEncoder
- Parameters:
obj
- Object to encode- Returns:
- An object (or type java.lang.String) containing the metaphone code which corresponds to the String supplied.
- Throws:
EncoderException
- if the parameter supplied is not of type java.lang.String
-
encode
public java.lang.String encode(java.lang.String str)
Encodes a String using the Metaphone algorithm.- Specified by:
encode
in interfaceStringEncoder
- Parameters:
str
- String object to encode- Returns:
- The metaphone code corresponding to the String supplied
-
isMetaphoneEqual
public boolean isMetaphoneEqual(java.lang.String str1, java.lang.String str2)
Tests is the metaphones of two strings are identical.- Parameters:
str1
- First of two strings to comparestr2
- Second of two strings to compare- Returns:
true
if the metaphones of these strings are identical,false
otherwise.
-
getMaxCodeLen
public int getMaxCodeLen()
Returns the maxCodeLen.- Returns:
- int
-
setMaxCodeLen
public void setMaxCodeLen(int maxCodeLen)
Sets the maxCodeLen.- Parameters:
maxCodeLen
- The maxCodeLen to set
-
-