Finding and indexing 'similar' string
Avi Bryant
avi at beta4.com
Tue Aug 26 17:51:47 UTC 2003
On Tue, 26 Aug 2003, Julian Fitzell wrote:
> Other than that, though, it works great: we used it for a sales
> system and it allowed users to stop asking people to spell their names
> over the phone. I've tried typing in every convoluted spelling of my
> name I can think of and it always finds me :)
And the code we used was (man, this is pretty old, these days I would use
streams...):
initialize
SoundexMap _ Dictionary new.
#('bpfv' 'cskgjqxz' 'dt' 'l' 'mn' 'r') doWithIndex:
[:r :i | r do: [:c | SoundexMap at: c put: i asString first]].
'aeiouyhw ' do: [:c | SoundexMap at: c put: nil].
soundex: aString
|soundex i lastCode|
soundex _ '0000' copy.
aString isEmpty ifTrue: [^ soundex].
soundex at: 1 put: (aString asUppercase at: 1).
lastCode _ i _ 1.
aString asLowercase allButFirst do:
[:c | |code|
(i < 4) ifTrue:
[code _ SoundexMap at: c ifAbsent:[].
((code = lastCode) or: [code isNil]) ifFalse:
[i := i + 1.
soundex at: i put: code].
lastCode _ code]].
^ soundex
More information about the Squeak-dev
mailing list
|