home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Usenet 1994 October
/
usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso
/
unix
/
volume11
/
soundslike
/
soundslike.c
< prev
Wrap
C/C++ Source or Header
|
1987-10-18
|
4KB
|
136 lines
#include <stdio.h>
#include <strings.h>
/*******************************************************
* Soundex (like) filter. Default database is DEFDB *
* or user specifies with "-filename" *
* syntex: soundex word *
* soundex -database word *
* soundex word -database *
******************************************************/
#define DEFDB "/usr/dict/words" /* default database */
main(argc,argv)
int argc;
char **argv;
{
void mkcode(), syntex();
int i;
char c;
char dbname[BUFSIZ], word[BUFSIZ], s[BUFSIZ];
char dbword[BUFSIZ], dbcode[BUFSIZ];
sprintf(dbname,"%s",DEFDB);
if(argc == 1)
syntex(argv[0]); /* print syntex help then exit */
for(i = 1; argv[i] != NULL; i++) { /* process argument line */
if( argv[i][0] == '-')
sprintf(dbname,"%s",argv[i]);
else
sprintf(word,"%s",argv[i]);
}
if( !strlen(word) )
syntex(argv[0]);
if ( freopen(dbname,"r",stdin) == NULL ) {
perror(dbname);
exit(0);
}
/* Sanity checks done. Now, down to buisness */
c = word[0];
mkcode(word);
while( (gets(dbword) ) != NULL ) {
if( c < dbword[0] ) /* assumes database is in alphabetical order */
break;
else if (c > dbword[0])
continue;
else {
sscanf(dbword,"%s%*s",dbcode); /* isolate only first word */
sprintf(dbword,"%s",dbcode); /* truncate dbword to one word */
mkcode(dbcode); /* convert to code */
if( !strcmp(dbcode,word) ) /* check for exact code match */
printf("%s\n",dbword);
}
}
}
/********************************* MKCODE() ****************************/
void mkcode(codeword)
char *codeword;
{
int i, j = 0;
int l;
l = strlen(codeword);
if(l <= 1){ /* this should probably return some snide remark */
codeword[l] = '\0';
return;
}
for(i = 1; codeword[i] != NULL; i++) {
if(codeword[i] == codeword[i - 1])
continue;
switch (codeword[i]) {
case 'b':
case 'f':
case 'p':
case 'v': codeword[j] = '1';
j++;
break;
case 'c':
case 'g':
case 'j':
case 'k':
case 'q':
case 's':
case 'x':
case 'z': codeword[j] = '2';
j++;
break;
case 'd':
case 't': codeword[j] = '3';
j++;
break;
case 'l': codeword[j] = '4';
j++;
break;
case 'm':
case 'n': codeword[j] = '5';
j++;
break;
case 'r': codeword[j] = '6';
j++;
break;
default : break;
}
}
codeword[j] = '\0';
while( strlen(codeword) < 3 ) /* pad with zeroes til minimum length */
strcat(codeword,"0");
}
/******************************* SYNTEX() **************************/
void syntex(name)
char *name;
{
printf("syntex: %s word\n",name);
printf(" or %s -database word\n",name);
printf(" or %s word -database\n",name);
exit(0);
}