Class DoubleMetaphone
- java.lang.Object
-
- org.apache.commons.codec.language.DoubleMetaphone
-
- All Implemented Interfaces:
Encoder
,StringEncoder
public class DoubleMetaphone extends java.lang.Object implements StringEncoder
Encodes a string into a double metaphone value. This Implementation is based on the algorithm by Lawrence Philips.This class is conditionally thread-safe. The instance field for the 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.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description class
DoubleMetaphone.DoubleMetaphoneResult
Inner class for storing results, since there is the optional alternate encoding.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String[]
ES_EP_EB_EL_EY_IB_IL_IN_IE_EI_ER
private static java.lang.String[]
L_R_N_M_B_H_F_V_W_SPACE
private static java.lang.String[]
L_T_K_S_N_M_B_Z
private int
maxCodeLen
Maximum length of an encoding, default is 4private static java.lang.String[]
SILENT_START
Prefixes when present which are not pronouncedprivate static java.lang.String
VOWELS
"Vowels" to test for
-
Constructor Summary
Constructors Constructor Description DoubleMetaphone()
Creates an instance of this DoubleMetaphone encoder
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected char
charAt(java.lang.String value, int index)
private java.lang.String
cleanInput(java.lang.String input)
Cleans the input.private boolean
conditionC0(java.lang.String value, int index)
Complex condition 0 for 'C'.private boolean
conditionCH0(java.lang.String value, int index)
Complex condition 0 for 'CH'.private boolean
conditionCH1(java.lang.String value, int index)
Complex condition 1 for 'CH'.private boolean
conditionL0(java.lang.String value, int index)
Complex condition 0 for 'L'.private boolean
conditionM0(java.lang.String value, int index)
Complex condition 0 for 'M'.protected static boolean
contains(java.lang.String value, int start, int length, java.lang.String... criteria)
java.lang.String
doubleMetaphone(java.lang.String value)
Encode a value with Double Metaphone.java.lang.String
doubleMetaphone(java.lang.String value, boolean alternate)
Encode a value with Double Metaphone, optionally using the alternate encoding.java.lang.Object
encode(java.lang.Object obj)
Encode the value using DoubleMetaphone.java.lang.String
encode(java.lang.String value)
Encode the value using DoubleMetaphone.int
getMaxCodeLen()
Returns the maxCodeLen.private int
handleAEIOUY(DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'A', 'E', 'I', 'O', 'U', and 'Y' cases.private int
handleC(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'C' cases.private int
handleCC(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'CC' cases.private int
handleCH(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'CH' cases.private int
handleD(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'D' cases.private int
handleG(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'G' cases.private int
handleGH(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'GH' cases.private int
handleH(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'H' cases.private int
handleJ(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'J' cases.private int
handleL(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'L' cases.private int
handleP(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'P' cases.private int
handleR(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'R' cases.private int
handleS(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'S' cases.private int
handleSC(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'SC' cases.private int
handleT(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'T' cases.private int
handleW(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'W' cases.private int
handleX(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'X' cases.private int
handleZ(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'Z' cases.boolean
isDoubleMetaphoneEqual(java.lang.String value1, java.lang.String value2)
Check if the Double Metaphone values of twoString
values are equal.boolean
isDoubleMetaphoneEqual(java.lang.String value1, java.lang.String value2, boolean alternate)
Check if the Double Metaphone values of twoString
values are equal, optionally using the alternate value.private boolean
isSilentStart(java.lang.String value)
Determines whether or not the value starts with a silent letter.private boolean
isSlavoGermanic(java.lang.String value)
Determines whether or not a value is of slavo-germanic origin.private boolean
isVowel(char ch)
Determines whether or not a character is a vowel or notvoid
setMaxCodeLen(int maxCodeLen)
Sets the maxCodeLen.
-
-
-
Field Detail
-
VOWELS
private static final java.lang.String VOWELS
"Vowels" to test for- See Also:
- Constant Field Values
-
SILENT_START
private static final java.lang.String[] SILENT_START
Prefixes when present which are not pronounced
-
L_R_N_M_B_H_F_V_W_SPACE
private static final java.lang.String[] L_R_N_M_B_H_F_V_W_SPACE
-
ES_EP_EB_EL_EY_IB_IL_IN_IE_EI_ER
private static final java.lang.String[] ES_EP_EB_EL_EY_IB_IL_IN_IE_EI_ER
-
L_T_K_S_N_M_B_Z
private static final java.lang.String[] L_T_K_S_N_M_B_Z
-
maxCodeLen
private int maxCodeLen
Maximum length of an encoding, default is 4
-
-
Method Detail
-
doubleMetaphone
public java.lang.String doubleMetaphone(java.lang.String value)
Encode a value with Double Metaphone.- Parameters:
value
- String to encode- Returns:
- an encoded string
-
doubleMetaphone
public java.lang.String doubleMetaphone(java.lang.String value, boolean alternate)
Encode a value with Double Metaphone, optionally using the alternate encoding.- Parameters:
value
- String to encodealternate
- use alternate encode- Returns:
- an encoded string
-
encode
public java.lang.Object encode(java.lang.Object obj) throws EncoderException
Encode the value using DoubleMetaphone. It will only work ifobj
is aString
(likeMetaphone
).- Specified by:
encode
in interfaceEncoder
- Parameters:
obj
- Object to encode (should be of type String)- Returns:
- An encoded Object (will be of type String)
- Throws:
EncoderException
- encode parameter is not of type String
-
encode
public java.lang.String encode(java.lang.String value)
Encode the value using DoubleMetaphone.- Specified by:
encode
in interfaceStringEncoder
- Parameters:
value
- String to encode- Returns:
- An encoded String
-
isDoubleMetaphoneEqual
public boolean isDoubleMetaphoneEqual(java.lang.String value1, java.lang.String value2)
Check if the Double Metaphone values of twoString
values are equal.- Parameters:
value1
- The left-hand side of the encodedString.equals(Object)
.value2
- The right-hand side of the encodedString.equals(Object)
.- Returns:
true
if the encodedString
s are equal;false
otherwise.- See Also:
isDoubleMetaphoneEqual(String,String,boolean)
-
isDoubleMetaphoneEqual
public boolean isDoubleMetaphoneEqual(java.lang.String value1, java.lang.String value2, boolean alternate)
Check if the Double Metaphone values of twoString
values are equal, optionally using the alternate value.- Parameters:
value1
- The left-hand side of the encodedString.equals(Object)
.value2
- The right-hand side of the encodedString.equals(Object)
.alternate
- use the alternate value iftrue
.- Returns:
true
if the encodedString
s are equal;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
-
handleAEIOUY
private int handleAEIOUY(DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'A', 'E', 'I', 'O', 'U', and 'Y' cases.
-
handleC
private int handleC(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'C' cases.
-
handleCC
private int handleCC(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'CC' cases.
-
handleCH
private int handleCH(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'CH' cases.
-
handleD
private int handleD(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'D' cases.
-
handleG
private int handleG(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'G' cases.
-
handleGH
private int handleGH(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'GH' cases.
-
handleH
private int handleH(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'H' cases.
-
handleJ
private int handleJ(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'J' cases.
-
handleL
private int handleL(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'L' cases.
-
handleP
private int handleP(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'P' cases.
-
handleR
private int handleR(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'R' cases.
-
handleS
private int handleS(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'S' cases.
-
handleSC
private int handleSC(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'SC' cases.
-
handleT
private int handleT(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'T' cases.
-
handleW
private int handleW(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'W' cases.
-
handleX
private int handleX(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index)
Handles 'X' cases.
-
handleZ
private int handleZ(java.lang.String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic)
Handles 'Z' cases.
-
conditionC0
private boolean conditionC0(java.lang.String value, int index)
Complex condition 0 for 'C'.
-
conditionCH0
private boolean conditionCH0(java.lang.String value, int index)
Complex condition 0 for 'CH'.
-
conditionCH1
private boolean conditionCH1(java.lang.String value, int index)
Complex condition 1 for 'CH'.
-
conditionL0
private boolean conditionL0(java.lang.String value, int index)
Complex condition 0 for 'L'.
-
conditionM0
private boolean conditionM0(java.lang.String value, int index)
Complex condition 0 for 'M'.
-
isSlavoGermanic
private boolean isSlavoGermanic(java.lang.String value)
Determines whether or not a value is of slavo-germanic origin. A value is of slavo-germanic origin if it contians any of 'W', 'K', 'CZ', or 'WITZ'.
-
isVowel
private boolean isVowel(char ch)
Determines whether or not a character is a vowel or not
-
isSilentStart
private boolean isSilentStart(java.lang.String value)
Determines whether or not the value starts with a silent letter. It will returntrue
if the value starts with any of 'GN', 'KN', 'PN', 'WR' or 'PS'.
-
cleanInput
private java.lang.String cleanInput(java.lang.String input)
Cleans the input.
-
charAt
protected char charAt(java.lang.String value, int index)
-
contains
protected static boolean contains(java.lang.String value, int start, int length, java.lang.String... criteria)
-
-