Class DoubleMetaphone
java.lang.Object
org.apache.commons.codec.language.DoubleMetaphone
- All Implemented Interfaces:
Encoder,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 invoke setMaxCodeLen(int) after initial setup.
- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionclassInner class for storing results, since there is the optional alternate encoding. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final String[]private static final String[]private static final String[]private intMaximum length of an encoding, default is 4private static final String[]Prefixes when present which are not pronouncedprivate static final String"Vowels" to test for -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected charprivate StringcleanInput(String input) Cleans the input.private booleanconditionC0(String value, int index) Complex condition 0 for 'C'.private booleanconditionCH0(String value, int index) Complex condition 0 for 'CH'.private booleanconditionCH1(String value, int index) Complex condition 1 for 'CH'.private booleanconditionL0(String value, int index) Complex condition 0 for 'L'.private booleanconditionM0(String value, int index) Complex condition 0 for 'M'.protected static booleandoubleMetaphone(String value) Encode a value with Double Metaphone.doubleMetaphone(String value, boolean alternate) Encode a value with Double Metaphone, optionally using the alternate encoding.Encode the value using DoubleMetaphone.Encode the value using DoubleMetaphone.intReturns the maxCodeLen.private inthandleAEIOUY(DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'A', 'E', 'I', 'O', 'U', and 'Y' cases.private inthandleC(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'C' cases.private inthandleCC(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'CC' cases.private inthandleCH(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'CH' cases.private inthandleD(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'D' cases.private inthandleG(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'G' cases.private inthandleGH(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'GH' cases.private inthandleH(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'H' cases.private inthandleJ(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'J' cases.private inthandleL(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'L' cases.private inthandleP(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'P' cases.private inthandleR(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'R' cases.private inthandleS(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'S' cases.private inthandleSC(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'SC' cases.private inthandleT(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'T' cases.private inthandleW(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'W' cases.private inthandleX(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index) Handles 'X' cases.private inthandleZ(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'Z' cases.booleanisDoubleMetaphoneEqual(String value1, String value2) Check if the Double Metaphone values of twoStringvalues are equal.booleanisDoubleMetaphoneEqual(String value1, String value2, boolean alternate) Check if the Double Metaphone values of twoStringvalues are equal, optionally using the alternate value.private booleanisSilentStart(String value) Determines whether or not the value starts with a silent letter.private booleanisSlavoGermanic(String value) Determines whether or not a value is of slavo-germanic origin.private booleanisVowel(char ch) Determines whether or not a character is a vowel or notvoidsetMaxCodeLen(int maxCodeLen) Sets the maxCodeLen.
-
Field Details
-
VOWELS
"Vowels" to test for- See Also:
-
SILENT_START
Prefixes when present which are not pronounced -
L_R_N_M_B_H_F_V_W_SPACE
-
ES_EP_EB_EL_EY_IB_IL_IN_IE_EI_ER
-
L_T_K_S_N_M_B_Z
-
maxCodeLen
private int maxCodeLenMaximum length of an encoding, default is 4
-
-
Constructor Details
-
DoubleMetaphone
public DoubleMetaphone()
-
-
Method Details
-
contains
-
charAt
-
cleanInput
Cleans the input. -
conditionC0
Complex condition 0 for 'C'. -
conditionCH0
Complex condition 0 for 'CH'. -
conditionCH1
Complex condition 1 for 'CH'. -
conditionL0
Complex condition 0 for 'L'. -
conditionM0
Complex condition 0 for 'M'. -
doubleMetaphone
Encode a value with Double Metaphone.- Parameters:
value- String to encode- Returns:
- an encoded string
-
doubleMetaphone
Encode a value with Double Metaphone, optionally using the alternate encoding.- Parameters:
value- String to encodealternate- use alternate encode- Returns:
- an encoded string
-
encode
Encode the value using DoubleMetaphone. It will only work ifobjis aString(likeMetaphone).- Specified by:
encodein 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
Encode the value using DoubleMetaphone.- Specified by:
encodein interfaceStringEncoder- Parameters:
value- String to encode- Returns:
- An encoded String
-
getMaxCodeLen
public int getMaxCodeLen()Returns the maxCodeLen.- Returns:
- int
-
handleAEIOUY
Handles 'A', 'E', 'I', 'O', 'U', and 'Y' cases. -
handleC
Handles 'C' cases. -
handleCC
Handles 'CC' cases. -
handleCH
Handles 'CH' cases. -
handleD
Handles 'D' cases. -
handleG
private int handleG(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'G' cases. -
handleGH
Handles 'GH' cases. -
handleH
Handles 'H' cases. -
handleJ
private int handleJ(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'J' cases. -
handleL
Handles 'L' cases. -
handleP
Handles 'P' cases. -
handleR
private int handleR(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'R' cases. -
handleS
private int handleS(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'S' cases. -
handleSC
Handles 'SC' cases. -
handleT
Handles 'T' cases. -
handleW
Handles 'W' cases. -
handleX
Handles 'X' cases. -
handleZ
private int handleZ(String value, DoubleMetaphone.DoubleMetaphoneResult result, int index, boolean slavoGermanic) Handles 'Z' cases. -
isDoubleMetaphoneEqual
Check if the Double Metaphone values of twoStringvalues are equal.- Parameters:
value1- The left-hand side of the encodedString.equals(Object).value2- The right-hand side of the encodedString.equals(Object).- Returns:
trueif the encodedStrings are equal;falseotherwise.- See Also:
-
isDoubleMetaphoneEqual
Check if the Double Metaphone values of twoStringvalues 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:
trueif the encodedStrings are equal;falseotherwise.
-
isSilentStart
Determines whether or not the value starts with a silent letter. It will returntrueif the value starts with any of 'GN', 'KN', 'PN', 'WR' or 'PS'. -
isSlavoGermanic
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 -
setMaxCodeLen
public void setMaxCodeLen(int maxCodeLen) Sets the maxCodeLen.- Parameters:
maxCodeLen- The maxCodeLen to set
-