1 package com.quadcap.util.text; 2 3 40 41 46 public class Soundex { 47 static final OctetMap alpha = new OctetMap('a', 'z'); 48 static final OctetMap Alpha = new OctetMap('A', 'Z'); 49 static { 50 Alpha.include('a', 'z'); 51 } 52 53 54 static final String sMap = "01230120022455012623010202"; 55 static final char scode(int c) { 56 if (alpha.has(c)) { 57 return sMap.charAt(c - 'a'); 58 } else { 59 return sMap.charAt(c - 'A'); 60 } 61 } 62 63 public static final String soundex(String s) { 64 char[] ret = new char[4]; 65 char last = 'x'; 66 int pos = 0; 67 for (int i = 0; i < s.length() && pos < 4; i++) { 68 int c = s.charAt(i) & 0xff; 69 if (Alpha.has(c)) { 70 if (pos == 0) { 71 ret[pos++] = Character.toUpperCase((char)c); 72 } else { 73 char code = scode(c); 74 if (code != '0' && code != last) { 75 ret[pos++] = code; 76 last = code; 77 } 78 } 79 } 80 } 81 if (pos == 0) return ""; 82 while (pos < 4) ret[pos++] = '0'; 83 return new String (ret); 84 } 85 86 public static final int difference(String a, String b) { 87 String sa = soundex(a); 88 String sb = soundex(b); 89 int diff = 0; 90 for (int i = 0; i < 4; i++) { 91 if (sa.charAt(i) == sb.charAt(i)) diff++; 92 } 93 return diff; 94 } 95 96 static String [] data = { 98 "blather", "blabber", "Smith", "Smyth", 99 "abcdefghijklmnopqrstuvwxyz", "a", "b", "bed", "BBD", 100 "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "smithers", "smothers", "brothers" 101 }; 102 public static void main(String [] args) { 103 for (int i = 0; i < data.length; i++) { 104 System.out.println(soundex(data[i]) + ": " + data[i]); 105 } 106 } 107 } 109 | Popular Tags |