home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / programm / 2062 < prev    next >
Encoding:
Text File  |  1992-07-22  |  4.7 KB  |  185 lines

  1. Path: sparky!uunet!zephyr.ens.tek.com!shaman!endeavor18!alanj
  2. From: alanj@endeavor18.tek.com (Alan Jeddeloh)
  3. Newsgroups: comp.programming
  4. Subject: Re: Soundex algorithms, database indexing
  5. Message-ID: <1453@shaman.wv.tek.com>
  6. Date: 22 Jul 92 17:01:27 GMT
  7. Sender: nobody@shaman.wv.tek.com
  8. Reply-To: Alan.Jeddeloh@tek.com
  9. Distribution: usa
  10. Organization: Tektronix, Inc.
  11. Lines: 172
  12.  
  13. In article <1992Jul22.122831.27758@cbnews.cb.att.com> you write:
  14. >       I would like to get info/ a 'C' program (function) that makes use of
  15. >       the Soundex algorithm.
  16.  
  17. This has worked for me for generating Soundex code for searching Federal Census
  18. indexes.  I tried to reply by mail, but it bounced of of att:
  19. >From postmaster Wed Jul 22 12:53 EDT 1992
  20. >Mail to `marc' alias `ssn=' from `att!endeavor18.wv.tek.com!alanj' failed.
  21. >The command `exec post -x -o '%^24name %20ema %^city, %+state' -- ssn=' returned error status 100.
  22. >The error message was:
  23. >post: ssn=: Ambiguous
  24. >0 Jung-Kuei_Chen                                Murray Hill, NJ
  25. >1 Gernot_Kubin                                  Murray Hill, NJ
  26. >2 David_Mansour                                 Murray Hill, NJ
  27. >3 Jacques_Terken                                Murray Hill, NJ
  28.  
  29. ------------- CUT HERE ---------------
  30.  
  31. #include <stdio.h>
  32. #include <strings.h>
  33. #include <ctype.h>
  34.  
  35. /*
  36.  *
  37.  * This is a summary of the soundex codes, and interpretations, should you run
  38.  * across a Soundex Census.  These REALLY are helpful (at least in my
  39.  * experience).
  40.  * 
  41.  * SOUNDEX CODE
  42.  * ------------
  43.  * 
  44.  * Code   Key Letters & Equivalent
  45.  * ----   ------------------------
  46.  * 
  47.  * 1      b, p, f, v
  48.  * 2      c, s, k, g, j, q, x, z
  49.  * 3      d, t
  50.  * 4      l
  51.  * 5      m, n
  52.  * 6      r
  53.  * 
  54.  * 
  55.  * Rules: 
  56.  * ------
  57.  * 
  58.  * Letters a, e, i, o, u, y, w, h are NOT coded
  59.  *       -  -  -  -  -  -  -  -     ---
  60.  * 
  61.  * First letter of the SURNAME is NOT encoded
  62.  * -----               -------    ---
  63.  * 
  64.  * All Soundex codes must contain 3 digits. Shorter numbers, append 0's.
  65.  * ---               ----         -
  66.  * 
  67.  * When two key letters or equivalent appear together, (preceeding or following)
  68.  *** IN THE ORIGINAL NAME ***
  69.  * they are counted as one.  (ie, KELLER = K-460 (two "L's" together combine)
  70.  * (also, MENNON = M-500  N,N, and N all counted together, (remove all vowels))
  71.  * 
  72.  *  
  73.  * When searching through records that contain the same Soundex code, names are
  74.  * arranged alphabetically. (Prefixes COULD be dropped, ie. van, Von, Di, de,
  75.  * le, D, etc).
  76.  * 
  77.  * Soundex codes are not limited by county/city locality, but rather by state.
  78.  * This enables you to search an entire state for similar sounding names.
  79.  * 
  80.  * 1880 Soundex Census will list households IF they had children under 10 yrs
  81.  * old.                                     __
  82.  * 
  83.  * 1900 Soundex Census is said to list ALL homes, regardless of children, etc.
  84.  * 
  85.  * 
  86.  * Here are some examples to get an idea of how this code works.
  87.  * 
  88.  * NAME            CODE        WHY
  89.  * ----            ----    ----------------------
  90.  * 
  91.  * Masters        M-236    M-str (truncate rest)
  92.  * Anderson        A-536    A-ndr (truncate rest)
  93.  * Pratt        P-630    P-rt (double letters, add 0)
  94.  * McGee        M-200    M-c (g-same group. Not counted, add 0's)
  95.  * Lee            L-000    L-  (vowels not counted, add 0's)
  96.  * 
  97.  */
  98.  
  99. int    letter_values[26] = {
  100.         0, 1, 2, 3, 0, 1, 2, 0,    /* abcdefgh */
  101.         0, 2, 2, 4, 5, 5, 0, 1,    /* ijklmnop */
  102.         2, 6, 2, 3, 0, 1, 0, 2,    /* qrstuvwx */
  103.         0, 2            /* yz */
  104.     };
  105.  
  106.  
  107. char    name[256];
  108. char    buffer[256];
  109.  
  110. char    alphas[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  111. char    delims[] = "\t\b\r\n ~`!@#$%^&*()_+{}:\"|,.?-=[];'\\,./";
  112.  
  113.  
  114. main (argc, argv)
  115. int    argc;
  116. char    *argv[];
  117. {
  118.     char    *p1;
  119.     char    *p2;
  120.  
  121.     if (argc == 2) {
  122.         p1 = strpbrk (argv[1], alphas);
  123.         p2 = strpbrk (p1, delims);
  124.         if (p2)
  125.             *p2 = '\0';
  126.         soundex (p1);
  127.     } else {
  128.         while (fgets (buffer, sizeof (buffer), stdin) != NULL) {
  129.             p1 = strpbrk (buffer, alphas);
  130.             p2 = strpbrk (p1, delims);
  131.             if (p2)
  132.                 *p2 = '\0';
  133.             soundex (p1);
  134.         }
  135.     }
  136. }
  137.  
  138.  
  139.  
  140.  
  141. soundex (nme)
  142. char    *nme;
  143. {
  144.     int    length;
  145.     int    i;
  146.     int    last;
  147.     int    count;
  148.  
  149.     printf ("%s:\t", nme);
  150.  
  151.     length = strlen (nme);
  152.     strcpy (name, nme);
  153.  
  154.     putchar (toupper (name[0]));
  155.     putchar ('-');
  156.  
  157.     for (i = 0; i < length; i++) {
  158.         name[i] = toupper (name[i]);
  159.         if ((name[i] >= 'A') && (name[i] <= 'Z')) {
  160.             name[i] = letter_values[name[i] - 'A'];
  161.         }
  162.     }
  163.  
  164.     last = name[0];
  165.     count = 0;
  166.     for (i = 1; i < length; i++) {
  167.         if ((name[i] != 0)  && (name[i] != last)){
  168.             printf ("%d", name[i]);
  169.             if (++count >= 3)
  170.                 break;
  171.         }
  172.         last = name[i];
  173.     }
  174.  
  175.     for (; count < 3; count++)
  176.         putchar ('0');
  177.     putchar ('\n');
  178. }
  179. ----------- CUT ----------
  180. --
  181.     Alan Jeddeloh      W:(503) 685-2991  H:(503) 292-9740
  182.     Tektronix, Inc.
  183.     D/S 60-850; PO Box 1000; Wilsonville, OR 97070
  184.     Alan.Jeddeloh@tek.com          Let sleeping dogs^H^H^H^Hbabies lie
  185.