2 [Header: soundexmodule.c,v 1.2 95/05/02 15:40:45 dwwillia Exp ]
4 Perform soundex comparisons on strings.
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.
10 From: David Wayne Williams <dwwillia@iucf.indiana.edu>
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)
21 static char soundex_module__doc__
[] =
22 "Perform Soundex comparisons on strings, allowing non-literal matching.";
24 static void soundex_hash(char *str
, char *result
)
26 char *sptr
= str
; /* pointer into str */
27 char *rptr
= result
; /* pointer into result */
31 strcpy(result
,"000000");
35 /* Preserve the first character of the input string.
37 *(rptr
++) = toupper(*(sptr
++));
39 /* Translate the rest of the input string into result. The following
40 transformations are used:
42 1) All vowels, W, and H, are skipped.
51 3) Only translate the first of adjacent equal translations. I.E.
52 remove duplicate digits.
55 for(;(rptr
- result
) < 6 && *sptr
!= '\0';sptr
++)
57 switch (toupper(*sptr
))
71 if(*(rptr
- 1) != '1')
82 if(*(rptr
- 1) != '2')
87 if(*(rptr
- 1) != '3')
91 if(*(rptr
- 1) != '4')
96 if(*(rptr
- 1) != '5')
100 if(*(rptr
-1) != '6')
107 /* Pad 0's on right side of string out to 6 characters.
109 for(; rptr
< result
+ 6; rptr
++)
112 /* Terminate the result string.
114 *(result
+ 6) = '\0';
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.";
123 get_soundex(PyObject
*self
, PyObject
*args
)
128 if(!PyArg_ParseTuple( args
, "s", &str
))
131 soundex_hash(str
, sdx
);
133 return PyString_FromString(sdx
);
136 static char soundex_sound_similar__doc__
[] =
137 "Compare two strings to see if they sound similar (English).";
139 sound_similar(PyObject
*self
, PyObject
*args
)
142 char res1
[7], res2
[7];
144 if(!PyArg_ParseTuple(args
, "ss", &str1
, &str2
))
147 soundex_hash(str1
, res1
);
148 soundex_hash(str2
, res2
);
150 if(!strcmp(res1
,res2
))
151 return Py_BuildValue("i",1);
153 return Py_BuildValue("i",0);
156 /* Python Method Table.
158 static PyMethodDef SoundexMethods
[] =
160 {"sound_similar", sound_similar
, 1, soundex_sound_similar__doc__
},
161 {"get_soundex", get_soundex
, 1, soundex_get_soundex__doc__
},
163 {NULL
, NULL
} /* sentinel */
167 /* Register the method table.
172 (void) Py_InitModule4("soundex",
174 soundex_module__doc__
,