home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / qtawk / soundix.exp < prev    next >
Text File  |  1990-03-31  |  3KB  |  108 lines

  1. # SOUNDIX Version 1.0
  2.  
  3.     {
  4. #    for ( i = 1 ; i <= NF ; i++ ) print soundex($i);
  5.     for ( i = 1 ; i <= NF ; i++ ) printf(" Result: %s ==> %s\n",$i,soundex($i));
  6. }
  7.  
  8. # SOUNDIX Version 1.0
  9. #
  10. #  This program takes a character string such as a person's last
  11. #  name and translates it to a sound index.  This index can then be
  12. #  used by an application to perform phonetic (i.e. 'sounds-like')
  13. #  search. Algorithm found in D. Knuth, "Art of Computer Programming",
  14. #  Vol. 3, Page 391-392
  15. #
  16. #  Rules:
  17. #  =====
  18. #
  19. #  1) Retain the first letter of the name and drop all occurances of
  20. #     a, e, h, i, o, u, w, and y in other positions
  21. #  2) assign the following numbers to the remaining letters after the first
  22. #     bfpv    ==> 1
  23. #     cgjkqsxz    ==> 2
  24. #     dt    ==> 3
  25. #     l     ==> 4
  26. #     mn    ==> 5
  27. #     r     ==> 6
  28. #
  29. #  3) if two or more letters with the same code were adjacent in the original
  30. #     string (before step 1), omit all but the first
  31. #
  32. #  4) convert to the form "letter, digit, digit, digit" by adding trailing
  33. #     zeros (if there are less than three digits) by dropping rightmost
  34. #     digits (if there are more than three).
  35. #
  36. #
  37. #  Logic:
  38. #  =====
  39. #
  40. #  1) Uppercase the string
  41. #  2) Use suffix to first letter
  42. #  3) Change the following letters:
  43. #     R           to 6
  44. #     M,N          to 5
  45. #     L           to 4
  46. #     D,T          to 3
  47. #     C,G,J,K,Q,S,X,Z to 2
  48. #     B,F,P,V          to 1
  49. #     AEIOUYHW          to 0
  50. #     anything else   to 0
  51. #  4) Remove all adjacent duplicates
  52. #  5) Remove all zeros
  53. #
  54. #  Example: (  and  marks duplicates which are deleted )
  55. #  =======
  56. #              
  57. #  McClowry   -->   52240060   -->   5246 --> M246
  58. #  McLorey    -->   5240600    -->   5246 --> M246
  59. #             
  60. #  Schiller   -->   22004406   -->   246  --> S460
  61. #  Shilar     -->   200406     -->   246  --> S460
  62. #
  63. #  Rosen      -->   60205      -->   625  --> R250
  64. #  Rozin      -->   60205      -->   625  --> R250
  65. #
  66. #  Moynihan   -->   50050005   -->   555  --> M550
  67. #  Monnihan   -->   50550005   -->   555  --> M550
  68. #               
  69. #  Abete      -->   01030      -->   013  --> A130
  70. #  Abadey     -->   010300     -->   013  --> A130
  71. #
  72. #
  73. function soundex(str) {
  74.     local ldl;
  75.     local t_from = "|@#$%:;&*()_-+=![]'{}?/<>.~`^1234567890AEIOUYHWBFPVCGJKQSXZDTLMNR\\";
  76.     local t_to     = "000000000000000000000000000000000007000000000001111222222223345560";
  77.  
  78.     str = strupr(str);
  79.     ldl = substr(str,1,1);    # rule 1
  80.     gsub(/^[AEIOUYH]/,"7",str);     # reserve leading "AEIOUYH"
  81.     str = stran(str,t_to,t_from);   # rule 2
  82.  
  83.     gsub(/11+/,"1",str);    # replace duplicate 1's with single 1 rule 3
  84.     gsub(/22+/,"2",str);    # replace duplicate 2's with single 2 rule 3
  85.     gsub(/33+/,"3",str);    # replace duplicate 3's with single 3 rule 3
  86.     gsub(/44+/,"4",str);    # replace duplicate 4's with single 4 rule 3
  87.     gsub(/55+/,"5",str);    # replace duplicate 5's with single 5 rule 3
  88.     gsub(/66+/,"6",str);    # replace duplicate 6's with single 6 rule 3
  89.     gsub(/0+/,"",str);      # delete internal 0's, rule 1
  90.  
  91.     str = ldl substr(str,2);    # glue leading character back on front
  92.     if ( (ldl = length(str)) < 4 ) {
  93.     switch ( ldl ) {
  94.         case 1:
  95.         str ∩= "000";
  96.         break;
  97.         case 2:
  98.         str ∩= "00";
  99.         break;
  100.         case 3:
  101.         str ∩= "0";
  102.         break;
  103.     }
  104.     } else if ( ldl > 4 ) str = substr(str,1,4);
  105.  
  106.     return str;
  107. }
  108.