Java Reference
In-Depth Information
static
static boolean
boolean debug = false
false ;
/* Implements the mapping
* from: AEHIOUWYBFPVCGJKQSXZDTLMNR
* to: 00000000111122222222334556
*/
public
public static
char [] MAP = {
//A B C D E F G H I J K L M
'0' , '1' , '2' , '3' , '0' , '1' , '2' , '0' , '0' , '2' , '2' , '4' , '5' ,
//N O P W R S T U V W X Y Z
'5' , '0' , '1' , '2' , '6' , '2' , '3' , '0' , '1' , '0' , '2' , '0' , '2'
static final
final char
};
/** Convert the given String to its Soundex code.
* @return null If the given string can't be mapped to Soundex.
*/
public
public static
static String soundex ( String s ) {
// Algorithm works on uppercase (mainframe era).
String t = s . toUpperCase ();
StringBuffer res = new
new StringBuffer ();
char
char c , prev = '?' , prevOutput = '?' ;
// Main loop: find up to 4 chars that map.
for
for ( int
int i = 0 ; i < t . length () && res . length () < 4 &&
( c = t . charAt ( i )) != ',' ; i ++) {
// Check to see if the given character is alphabetic.
// Text is already converted to uppercase. Algorithm
// only handles ASCII letters, do NOT use Character.isLetter()!
// Also, skip double letters.
iif ( c >= 'A' && c <= 'Z' && c != prev ) {
prev = c ;
// First char is installed unchanged, for sorting.
iif ( i == 0 ) {
res . append ( c );
} else
else {
char
char m = MAP [ c - 'A' ];
iif ( debug ) {
System . out . println ( c + " --> " + m );
}
iif ( m != '0' && m != prevOutput ) {
res . append ( m );
prevOutput = m ;
Search WWH ::




Custom Search