字符串相似算法Soundex的Java实现

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/asty9000/article/details/82262358

Soundex是一种根据发音来计算相似度的一种算法。

public class Soundex {

	private static char[] chars=new char[]{'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'};
	private static int MAXCHARS=4;
	
	public static String getSoundexString(String word){
		
		char[] wordChars=null;
		char[] result=new char[4];
		if(word==null||(wordChars=word.trim().toCharArray()).length==0){
			return null;
		}
		//下标
		int index=-1;
		//当前位移
		int cur=0;
		//Soundex字符串已填充字符数
		int fill=0;
		while(cur<wordChars.length&&fill<MAXCHARS){
			char c=wordChars[cur++];
			if((c>='A'&&c<='Z')){
				index=c-'A';
				if(fill==0){
					c+='a'-'A';
				}
			}else if((c>='a'&&c<='z')){
				index=c-'a';
			}else{
				index=-1;
			}
			if(index!=-1){
				if(fill==0){
					result[fill++]=c;
				}else{
					char curChar=chars[index];
					if(curChar!='0'&&(fill==1||curChar!=result[fill-1])){
						result[fill++]=curChar;
					}
				}
			}
		}
		if(fill==0){
			return null;
		}
		for(int i=MAXCHARS-fill;i>0;i--){
			result[MAXCHARS-i]='0';
		}
		return new String(result);
	}
}

猜你喜欢

转载自blog.csdn.net/asty9000/article/details/82262358