| 1 package com.daffodilwoods.daffodildb.server.sql99.common; 2 3 import com.daffodilwoods.database.resource.*; 4 5 public class Soundex { 6 static final OctetMap alpha = new OctetMap('a', 'z'); 7 static final OctetMap Alpha = new OctetMap('A', 'Z'); 8 static { 9 Alpha.include('a', 'z'); 10 } 11 12 13 static final String sMap = "01230120022455012623010202"; 14 static final char scode(int c) { 15 if (alpha.has(c)) { 16 return sMap.charAt(c - 'a'); 17 } else { 18 return sMap.charAt(c - 'A'); 19 } 20 } 21 22 public static final String soundex(String s) throws DException { 23 char[] ret = new char[4]; 24 char last = 'x'; 25 int pos = 0; 26 for (int i = 0; i < s.length() && pos < 4; i++) { 27 int c = s.charAt(i) & 0xff; 28 if (Alpha.has(c)) { 29 if (pos == 0) { 30 ret[pos++] = Character.toUpperCase( (char) c); 31 } else { 32 char code = scode(c); 33 if (code != '0' && code != last) { 34 ret[pos++] = code; 35 last = code; 36 } 37 } 38 } 39 } 40 while (pos < 4) { 41 ret[pos++] = '0'; 42 } 43 return new String (ret); 44 } 45 46 public static final int difference(String a, String b) throws DException { 47 String sa = soundex(a); 48 String sb = soundex(b); 49 int diff = 0; 50 for (int i = 0; i < 4; i++) { 51 if (sa.charAt(i) == sb.charAt(i)) { 52 diff++; 53 } 54 } 55 return diff; 56 } 57 } 58 | Popular Tags |