Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 1 | /* |
Guido van Rossum | ac56297 | 1995-06-14 22:49:20 +0000 | [diff] [blame] | 2 | [Header: soundexmodule.c,v 1.2 95/05/02 15:40:45 dwwillia Exp ] |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 3 | |
| 4 | Perform soundex comparisons on strings. |
| 5 | |
| 6 | Soundex is an algorithm that hashes English strings into numerical value. |
| 7 | Strings that sound the same are hashed to the same value. This allows |
| 8 | for non-literal string matching. |
| 9 | |
| 10 | From: David Wayne Williams <dwwillia@iucf.indiana.edu> |
Guido van Rossum | 5038412 | 1996-05-23 22:54:17 +0000 | [diff] [blame] | 11 | |
| 12 | Apr 29 1996 - added get_soundex method that returns the soundex of a |
| 13 | string (chrish@qnx.com) |
| 14 | May 2 1996 - added doc strings (chrish@qnx.com) |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 15 | */ |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <ctype.h> |
| 19 | #include "Python.h" |
| 20 | |
Guido van Rossum | 5038412 | 1996-05-23 22:54:17 +0000 | [diff] [blame] | 21 | static char soundex_module__doc__[] = |
| 22 | "Perform Soundex comparisons on strings, allowing non-literal matching."; |
| 23 | |
Guido van Rossum | 3d86cc0 | 1996-05-24 20:50:57 +0000 | [diff] [blame] | 24 | static void soundex_hash(char *str, char *result) |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 25 | { |
| 26 | char *sptr = str; /* pointer into str */ |
| 27 | char *rptr = result; /* pointer into result */ |
| 28 | |
Guido van Rossum | 60fca2c | 1996-12-05 23:41:26 +0000 | [diff] [blame] | 29 | if(*str == '\0') |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 30 | { |
| 31 | strcpy(result,"000000"); |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | /* Preserve the first character of the input string. |
| 36 | */ |
| 37 | *(rptr++) = toupper(*(sptr++)); |
| 38 | |
| 39 | /* Translate the rest of the input string into result. The following |
| 40 | transformations are used: |
| 41 | |
Guido van Rossum | 60fca2c | 1996-12-05 23:41:26 +0000 | [diff] [blame] | 42 | 1) All vowels, W, and H, are skipped. |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 43 | |
| 44 | 2) BFPV = 1 |
| 45 | CGJKQSXZ = 2 |
| 46 | DT = 3 |
| 47 | L = 4 |
| 48 | MN = 5 |
Guido van Rossum | 60fca2c | 1996-12-05 23:41:26 +0000 | [diff] [blame] | 49 | R = 6 |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 50 | |
| 51 | 3) Only translate the first of adjacent equal translations. I.E. |
| 52 | remove duplicate digits. |
| 53 | */ |
| 54 | |
Guido van Rossum | 60fca2c | 1996-12-05 23:41:26 +0000 | [diff] [blame] | 55 | for(;(rptr - result) < 6 && *sptr != '\0';sptr++) |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 56 | { |
| 57 | switch (toupper(*sptr)) |
| 58 | { |
| 59 | case 'W': |
| 60 | case 'H': |
| 61 | case 'A': |
| 62 | case 'I': |
| 63 | case 'O': |
| 64 | case 'U': |
| 65 | case 'Y': |
| 66 | break; |
| 67 | case 'B': |
| 68 | case 'F': |
| 69 | case 'P': |
| 70 | case 'V': |
| 71 | if(*(rptr - 1) != '1') |
| 72 | *(rptr++) = '1'; |
| 73 | break; |
| 74 | case 'C': |
| 75 | case 'G': |
| 76 | case 'J': |
| 77 | case 'K': |
| 78 | case 'Q': |
| 79 | case 'S': |
| 80 | case 'X': |
| 81 | case 'Z': |
| 82 | if(*(rptr - 1) != '2') |
| 83 | *(rptr++) = '2'; |
| 84 | break; |
| 85 | case 'D': |
| 86 | case 'T': |
| 87 | if(*(rptr - 1) != '3') |
| 88 | *(rptr++) = '3'; |
| 89 | break; |
| 90 | case 'L': |
| 91 | if(*(rptr - 1) != '4') |
| 92 | *(rptr++) = '4'; |
| 93 | break; |
| 94 | case 'M': |
| 95 | case 'N': |
| 96 | if(*(rptr - 1) != '5') |
| 97 | *(rptr++) = '5'; |
| 98 | break; |
Guido van Rossum | 60fca2c | 1996-12-05 23:41:26 +0000 | [diff] [blame] | 99 | case 'R': |
| 100 | if(*(rptr -1) != '6') |
| 101 | *(rptr++) = '6'; |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 102 | default: |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | /* Pad 0's on right side of string out to 6 characters. |
| 108 | */ |
| 109 | for(; rptr < result + 6; rptr++) |
| 110 | *rptr = '0'; |
| 111 | |
| 112 | /* Terminate the result string. |
| 113 | */ |
Guido van Rossum | 60fca2c | 1996-12-05 23:41:26 +0000 | [diff] [blame] | 114 | *(result + 6) = '\0'; |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | |
Guido van Rossum | 5038412 | 1996-05-23 22:54:17 +0000 | [diff] [blame] | 118 | /* Return the actual soundex value. */ |
| 119 | /* Added by Chris Herborth (chrish@qnx.com) */ |
| 120 | static char soundex_get_soundex__doc__[] = |
| 121 | "Return the (English) Soundex hash value for a string."; |
| 122 | static PyObject * |
| 123 | get_soundex(PyObject *self, PyObject *args) |
| 124 | { |
| 125 | char *str; |
Guido van Rossum | 5038412 | 1996-05-23 22:54:17 +0000 | [diff] [blame] | 126 | char sdx[7]; |
| 127 | |
| 128 | if(!PyArg_ParseTuple( args, "s", &str)) |
| 129 | return NULL; |
| 130 | |
| 131 | soundex_hash(str, sdx); |
| 132 | |
| 133 | return PyString_FromString(sdx); |
| 134 | } |
| 135 | |
| 136 | static char soundex_sound_similar__doc__[] = |
| 137 | "Compare two strings to see if they sound similar (English)."; |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 138 | static PyObject * |
| 139 | sound_similar(PyObject *self, PyObject *args) |
| 140 | { |
| 141 | char *str1, *str2; |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 142 | char res1[7], res2[7]; |
| 143 | |
| 144 | if(!PyArg_ParseTuple(args, "ss", &str1, &str2)) |
| 145 | return NULL; |
| 146 | |
| 147 | soundex_hash(str1, res1); |
| 148 | soundex_hash(str2, res2); |
| 149 | |
| 150 | if(!strcmp(res1,res2)) |
| 151 | return Py_BuildValue("i",1); |
| 152 | else |
| 153 | return Py_BuildValue("i",0); |
| 154 | } |
| 155 | |
| 156 | /* Python Method Table. |
| 157 | */ |
| 158 | static PyMethodDef SoundexMethods[] = |
| 159 | { |
Guido van Rossum | 5038412 | 1996-05-23 22:54:17 +0000 | [diff] [blame] | 160 | {"sound_similar", sound_similar, 1, soundex_sound_similar__doc__}, |
| 161 | {"get_soundex", get_soundex, 1, soundex_get_soundex__doc__}, |
| 162 | |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 163 | {NULL, NULL } /* sentinel */ |
| 164 | }; |
| 165 | |
| 166 | |
| 167 | /* Register the method table. |
| 168 | */ |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 169 | DL_EXPORT(void) |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 170 | initsoundex() |
| 171 | { |
Guido van Rossum | 5038412 | 1996-05-23 22:54:17 +0000 | [diff] [blame] | 172 | (void) Py_InitModule4("soundex", |
| 173 | SoundexMethods, |
| 174 | soundex_module__doc__, |
| 175 | (PyObject *)NULL, |
| 176 | PYTHON_API_VERSION); |
Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame] | 177 | } |