1 16 19 package org.apache.taglibs.string.util; 20 21 28 public class Soundex { 29 30 static public final char[] US_ENGLISH_MAPPING = 31 "01230120022455012623010202".toCharArray(); 32 33 static public final Soundex US_ENGLISH = new Soundex(); 34 35 private char[] soundexMapping; 36 37 public Soundex() { 38 this(US_ENGLISH_MAPPING); 39 } 40 41 public Soundex(char[] mapping) { 42 this.soundexMapping = mapping; 43 } 44 45 50 public String soundex(String str) { 51 char out[] = { '0', '0', '0', '0' }; 52 char last, mapped; 53 int incount = 1, count = 1; 54 out[0] = Character.toUpperCase( str.charAt(0) ); 55 last = getMappingCode( str.charAt(0) ); 56 while( (incount < str.length() ) && 57 (mapped = getMappingCode(str.charAt(incount++))) != 0 && 58 (count < 4) ) 59 { 60 if( (mapped != '0') && (mapped != last) ) { 61 out[count++] = mapped; 62 } 63 last = mapped; 64 } 65 return new String (out); 66 } 67 68 71 private char getMappingCode(char c) { 72 if( !Character.isLetter(c) ) { 73 return 0; 74 } else { 75 return soundexMapping[Character.toUpperCase(c) - 'A']; 76 } 77 } 78 79 } 80 | Popular Tags |