1 16 17 package org.apache.commons.codec.language; 18 19 import org.apache.commons.codec.EncoderException; 20 import org.apache.commons.codec.StringEncoder; 21 22 30 public class RefinedSoundex implements StringEncoder { 31 32 36 public static final RefinedSoundex US_ENGLISH = new RefinedSoundex(); 37 38 43 public static final char[] US_ENGLISH_MAPPING = "01360240043788015936020505".toCharArray(); 44 45 50 private char[] soundexMapping; 51 52 56 public RefinedSoundex() { 57 this(US_ENGLISH_MAPPING); 58 } 59 60 69 public RefinedSoundex(char[] mapping) { 70 this.soundexMapping = mapping; 71 } 72 73 95 public int difference(String s1, String s2) throws EncoderException { 96 return SoundexUtils.difference(this, s1, s2); 97 } 98 99 112 public Object encode(Object pObject) throws EncoderException { 113 if (!(pObject instanceof java.lang.String )) { 114 throw new EncoderException("Parameter supplied to RefinedSoundex encode is not of type java.lang.String"); 115 } 116 return soundex((String ) pObject); 117 } 118 119 126 public String encode(String pString) { 127 return soundex(pString); 128 } 129 130 139 char getMappingCode(char c) { 140 if (!Character.isLetter(c)) { 141 return 0; 142 } 143 return this.soundexMapping[Character.toUpperCase(c) - 'A']; 144 } 145 146 153 public String soundex(String str) { 154 if (str == null) { 155 return null; 156 } 157 str = SoundexUtils.clean(str); 158 if (str.length() == 0) { 159 return str; 160 } 161 162 StringBuffer sBuf = new StringBuffer (); 163 sBuf.append(str.charAt(0)); 164 165 char last, current; 166 last = '*'; 167 168 for (int i = 0; i < str.length(); i++) { 169 170 current = getMappingCode(str.charAt(i)); 171 if (current == last) { 172 continue; 173 } else if (current != 0) { 174 sBuf.append(current); 175 } 176 177 last = current; 178 179 } 180 181 return sBuf.toString(); 182 } 183 } 184 | Popular Tags |