public class Soundex {
public static String soundexOut(String word) {
String drop = dropedWord(word);
word = word.toLowerCase();
String soundex = "" + drop.charAt(0);
drop = drop.toLowerCase();
int i;
if (soundexCode(drop.charAt(0)) == soundexCode(drop.charAt(1)))
i = 2;
else
i = 1;
for (; i < drop.length() && soundex.length() < 5; i++) {
if (i < drop.length()-1 && soundexCode(drop.charAt(i-1)) == soundexCode(drop.charAt(i+1)) ) {
if (drop.charAt(i) == 'y' || drop.charAt(i) == 'h' || drop.charAt(i) == 'w')
i++;
}
else {
int code = soundexCode(drop.charAt(i));
soundex += code!=0 ? code : "";
}
}
if (soundex.length() < 4)
for (i = soundex.length(); i < 4; i++) {
soundex += "0";
}
return soundex;
}
public static int soundexCode(char c) {
String [] code = {"b, f, p, v" , "c, g, j, k, q, s, x, z" , "d, t" , "l" , "m,n" , "r"} ;
int codeNumber = 0;
for( int i = 0 ; i < code.length ; i++ ){
if( code[i].indexOf(c) >= 0 ) {
codeNumber = i+1;
}
}
return codeNumber;
}
public static String dropedWord(String word) {
String drop = "";
drop += word.charAt(0);
word = word.toLowerCase();
for (int i = 1; i < word.length(); i++) {
if (word.charAt(i) == 'a' || word.charAt(i) == 'e' || word.charAt(i) == 'i' ||
word.charAt(i) == 'o' || word.charAt(i) == 'u' )
continue;
drop += word.charAt(i);
}
return drop;
}
}