Soundex算法

Soundex 算法是一种拼音算法,用于按英语发音来索引姓名,它最初由美国人口调查局开发。

Soundex 方法返回一个表示姓名的四字符代码,由一个英文字母后跟三个数字构成。

字母是姓名的首字母,数字对姓名中剩余的辅音字母编码。 发音相近的姓名具有相同的 SoundEx 代码。

辅音字母将映射到一个特定的数字。

算法简要说明

1、将英文字按以下规则替换(除第一个字符外):
 
 a e h i o u w y -> 0
   b f p v -> 1
   c g j k q s x z -> 2
   d t -> 3
   l -> 4
   m n -> 5
   r -> 6
2、去除0,对于重复的字符只保留一个
3、返回前4个字符,不足4位以0补足

import java.util.*;
import java.lang.System.*;

public class Soundex {
	
	private static final long serialVersion = 1L;
	
	/*实现26个英文字母的映射*/
	public static final char[] MAP = {
		//A  B   C   D   E   F   G   H   I   J   K   L   M
		'0','1','2','3','0','1','2','0','0','2','2','4','5',
		//N  O   P   Q   R   S   T   U   V   W   X   Y   Z
		'5','0','1','2','6','2','3','0','1','0','2','0','2'
	};
	
	public static void main(String args[]) {
		
		List<String> names = new ArrayList<>();
		names.add("songzhaoherng");
		names.add("sonzhaoheng");
		names.add("sungzhaoheng");
		names.add("sunzhaoheng");

		for(String name: names) {
			System.out.println(Soundex.soundex(name) + "---->" + name);
			
		}
	}

	/*将传过来的字符串转换为 Soundex 码
	 *不能转换的返回null
	 * */
	public static String soundex(String s) {
		
		String t = s.toUpperCase();//将其转换为大写(实现英文字母的统一)
		
		StringBuffer sbu = new StringBuffer(); //StringBuffer更加安全
		char c;
		char prev = '?', PrevOutput = '?';//符号字符
		
		/*主循环,找出映射的字符(最多4个)
		 * 算法仅能处理 ASCII 码,不要用Character.isLetter()
		 * */
		for(int i = 0; i < t.length() && sbu.length() < 4 && (c = t.charAt(i)) != ','; i++) {
			
			//判断字母是否为大写,且是否为符号字符
			if(c >= 'A' && c <= 'Z' && c!= prev) {
				
				prev = c;
				
				//沿用首字符,依次进行排序,不是首字符的按照 Soundex 码
				if(i == 0) {
					sbu.append(c);
				}else {
					char m = MAP[c - 'A'];
					
					/*
					if(false) {
						System.out.println(c + "---->" + m);
					}*/
					
					//去除重复的英文字母
					if(m != '0' && m != PrevOutput) {
						sbu.append(m);
						PrevOutput = m;
					}
				}
			}
		}
		
		//不能转换的返回null
		if(sbu.length() == 0) {
			return null;
		}
		
		//如果长度不够4就添加0
		for(int i = sbu.length(); i < 4; i++) {
			sbu.append('0');
		}
		
		return sbu.toString();
	}

}

运行结果:

S526---->songzhaoherng
S525---->sonzhaoheng
S525---->sungzhaoheng
S525---->sunzhaoheng

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Soundex算法是一种用于字符串匹配的算法,它可以将一个单词转换成一个固定长度的编码(通常为4个字符),以便于比较单词的相似性。以下是用C语言实现Soundex算法的代码示例: ```c #include <stdio.h> #include <ctype.h> #include <string.h> void soundex(char *s, char *soundex_code) { const char *soundex_table = "01230120022455012623010202"; int i, j, prev_code, curr_code; /* Convert the first letter to uppercase */ soundex_code[0] = toupper(s[0]); /* Convert the remaining letters to their corresponding Soundex codes */ prev_code = soundex_table[s[0] - 'A']; j = 1; for (i = 1; i < strlen(s); i++) { curr_code = soundex_table[s[i] - 'A']; if (curr_code != '0' && curr_code != prev_code) { soundex_code[j++] = curr_code; } prev_code = curr_code; } /* Pad the Soundex code with zeros if it is less than 4 characters */ while (j < 4) { soundex_code[j++] = '0'; } /* Terminate the string */ soundex_code[j] = '\0'; } int main() { char s[100], soundex_code[5]; printf("Enter a word: "); fgets(s, sizeof(s), stdin); s[strcspn(s, "\n")] = '\0'; soundex(s, soundex_code); printf("Soundex code: %s\n", soundex_code); return 0; } ``` 在这个示例中,我们首先定义了一个Soundex表格(soundex_table),它将字母映射到它们的Soundex代码。然后,我们实现了一个名为“soundex”的函数,它接受一个输入字符串和一个输出字符串作为参数,并将输入字符串转换为其对应的Soundex代码,存储在输出字符串中。在函数中,我们首先将输入字符串的第一个字母转换为大写字母,并将其存储在输出字符串的第一个位置上。然后,我们将剩余的字母转换为它们对应的Soundex代码,并将其存储在输出字符串中。如果两个相邻的字母具有相同的Soundex代码,则只存储一个代码。最后,我们将输出字符串填充到4个字符,并在结尾处添加一个空字符。在主函数中,我们从用户输入中获取一个单词,并将其传递给“soundex”函数,以获取其对应的Soundex代码。最后,我们将Soundex代码打印到控制台上。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值