public class Metaphone extends Object implements StringEncoder
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:
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 maxCodeLen
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 invoke setMaxCodeLen(int)
after initial setup.
Modifier and Type | Field and Description |
---|---|
private static String |
FRONTV
Variable used in Metaphone algorithm
|
private int |
maxCodeLen
The max code length for metaphone is 4
|
private static String |
VARSON
Variable used in Metaphone algorithm
|
private static String |
VOWELS
Five values in the English language
|
Constructor and Description |
---|
Metaphone()
Creates an instance of the Metaphone encoder
|
Modifier and Type | Method and Description |
---|---|
Object |
encode(Object obj)
Encodes an Object using the metaphone algorithm.
|
String |
encode(String str)
Encodes a String using the Metaphone algorithm.
|
int |
getMaxCodeLen()
Returns the maxCodeLen.
|
private boolean |
isLastChar(int wdsz,
int n) |
boolean |
isMetaphoneEqual(String str1,
String str2)
Tests is the metaphones of two strings are identical.
|
private boolean |
isNextChar(StringBuilder string,
int index,
char c) |
private boolean |
isPreviousChar(StringBuilder string,
int index,
char c) |
private boolean |
isVowel(StringBuilder string,
int index) |
String |
metaphone(String txt)
Find the metaphone value of a String.
|
private boolean |
regionMatch(StringBuilder string,
int index,
String test) |
void |
setMaxCodeLen(int maxCodeLen)
Sets the maxCodeLen.
|
private static final String VOWELS
private static final String FRONTV
private static final String VARSON
private int maxCodeLen
public Metaphone()
public String metaphone(String txt)
txt
- String to find the metaphone code forprivate boolean isVowel(StringBuilder string, int index)
private boolean isPreviousChar(StringBuilder string, int index, char c)
private boolean isNextChar(StringBuilder string, int index, char c)
private boolean regionMatch(StringBuilder string, int index, String test)
private boolean isLastChar(int wdsz, int n)
public Object encode(Object obj) throws EncoderException
encode
in interface Encoder
obj
- Object to encodeEncoderException
- if the parameter supplied is not
of type java.lang.Stringpublic String encode(String str)
encode
in interface StringEncoder
str
- String object to encodepublic boolean isMetaphoneEqual(String str1, String str2)
str1
- First of two strings to comparestr2
- Second of two strings to comparetrue
if the metaphones of these strings are identical,
false
otherwise.public int getMaxCodeLen()
public void setMaxCodeLen(int maxCodeLen)
maxCodeLen
- The maxCodeLen to setWebARTS Library Licensed Under the GNU - General Public License. Other Libraries licensed under their respective Open Source Licenses