Soundex Function Implementation
The soundex scheme is used to compare phonetically equal strings. it is applied in voice recognition. Here is the code snippet:
#include <stdio.h>
#define MAXLEN 80
#define NALPHA 26
char *soundexGroups[] = {
"aeiouhyw",
"kcgjqsxz",
"td",
"bpfv",
"l",
"mn",
"r"
};
int soundexCodes[NALPHA];
void soundexInit() {
/*
* build an inverted index from the global table soundexGroups[].
* the inverted index is stored in global soundexCodes[].
*/
int i;
char *sptr;
for(i=sizeof(soundexGroups)/sizeof(char *)−1; i>=0; -i)
for(sptr=soundexGroups[i]; *sptr; ++sptr)
soundexCodes[*sptr-'a'] = i;
}
int compareCodes(int *soundex1, int *soundex2) {
int *ptr1, *ptr2;
for(ptr1=soundex1, ptr2=soundex2; *ptr1!=−1 && *ptr2!=−1 && *ptr1==*ptr2; ++
ptr1, ++ptr2)
;
return *ptr1 == *ptr2;
}
void findSoundex(char *s, int *soundex, char lastchar) {
/*
* find the soundex code for s and save in soundex.
* the stored value is the index in the array of soundex codes.
* function is recursive.
* start by changing multiple occurrences of chars in consecutive positions
* by single occurrences.
* end soundex by −1.
* lastchar == −1 implies this is the first call to this function.
*/
if(!*s)
*soundex = −1, lastchar = 0;
else if(*s == lastchar)
findSoundex(s+1, soundex, lastchar);
else if(lastchar == −1) // *s is the first char.
*soundex=soundexCodes[*s-'a'], findSoundex(s+1, soundex+1, *s);
else if(soundexCodes[*s-'a'] == 0) // vowel group.
findSoundex(s+1, soundex, *s);
else
*soundex=soundexCodes[*s-'a'], findSoundex(s+1, soundex+1, *s);
}
int compareSoundex(char *s1, char *s2) {
/*
* find soundex codes for s1 and s2.
* return 1 if codes are equal else 0.
*/
int soundex1[MAXLEN], soundex2[MAXLEN];
findSoundex(s1, soundex1, −1);
findSoundex(s2, soundex2, −1);
return compareCodes(soundex1, soundex2);
}
int main() {
char s1[MAXLEN];
char s2[MAXLEN];
soundexInit();
printf("Enter string 1: ");
gets(s1);
while(*s1) {
printf("Enter string 2: ");
gets(s2);
printf("(%s == %s) = %d.\n", s1, s2, compareSoundex(s1, s2));
printf("Enter string 1(enter to end): ");
gets(s1);
}
return 0;
}
