Soundex语音匹配算法

package cn.sounderx.text01;
import sun.security.ssl.Debug;
/*
 * 数法的实现
 * */
public class SoundexSimple {
	public static void main(String[] args) {
		String [] name = {	"Darwin,Ian",
				"Davidson, Greg",
				"Darweent,William",
				"Derwin,Daemon"};
		for(int i = 0;i < name.length; i++){
			//System.out.println("未改变之前:"+name[i]);
			System.out.println(Soundex.soundex(name[i]) +' '+ name[i]);
		
		}
	}
}
class Soundex{ 	 							//A   B  C  D  E   F  G  H  I   J   K  L  M N  O  P  W R  S  T   U  V  W X Y Z   实现映射
	public static final char[] MAP = {'0','1','2','3','0','1','2','0','0','2','2','4','5','5','0','1','2','6','2','3','0','1','0','2','0','2'	};
	public static String soundex(String s){		//将给定的字符串转换成对应的字符编码
		String t = s.toUpperCase();	//转换成对应的大写字母
		StringBuffer res = new StringBuffer();
		char c, prev= '?';//主循环,找出映射的字符(最多有4个)检查给定的字符是否为字母
		for(int i = 0; i <t.length() && res.length() < 4 && (c = t.charAt(i)) != ','; i++){
			if(c >='A' && c <='Z' && c !=prev){
				prev =c;	
				if(i == 0){res.append(c);}
				else{
					char m = MAP[c-'A'];
					Debug.println("inner", c +"-->"+m);
					if(m !='0') res.append(m);
				}
			}
		}
		if(res.length() ==0)return null;
		for(int i = res.length(); i < 4; i++)res.append('0');
		return res.toString();
	}
	
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值