Soundex Matching
From Erlang Community
Revision as of 12:52, 23 November 2006 by 213.171.204.166 (Talk)
[edit] Problem
You want to generate Soundex hashes of surnames, for doing "sounds-like" indexing databases, or retrieving information from the US Census records and similar pre-existing databases.
[edit] Solution
Use the soundex module below:
> soundex:soundex("Smith").
"S530"
> soundex:soundex("Smyth").
"S530"
|
Soundex is a string hash historically used by the US Census for indexing surnames by a function of what they "sound" like, rather than their precise spelling. Further general information on Soundex is available at http://www.archives.gov/research_room/genealogy/census/soundex.html.
[edit] Implementation
-module(soundex). -export([soundex/1]). soundex([]) -> []; %% soundex([First|T]) -> pad([First|[num(X) || X <- strip(httpd_util:to_upper(T))]]). strip(String) -> strip(String, []). strip([$A|T], Acc) -> strip(T, Acc); strip([$E|T], Acc) -> strip(T, Acc); strip([$H|T], Acc) -> strip(T, Acc); strip([$I|T], Acc) -> strip(T, Acc); strip([$O|T], Acc) -> strip(T, Acc); strip([$U|T], Acc) -> strip(T, Acc); strip([$W|T], Acc) -> strip(T, Acc); strip([$Y|T], Acc) -> strip(T, Acc); strip([32|T], Acc) -> strip(T, Acc); strip([C|T], Acc) -> strip(T, [C|Acc]); strip([], Acc) -> lists:reverse(Acc). num($B) -> $1; num($F) -> $1; num($P) -> $1; num($V) -> $1; num($C) -> $2; num($G) -> $2; num($J) -> $2; num($K) -> $2; num($Q) -> $2; num($S) -> $2; num($X) -> $2; num($Z) -> $2; num($D) -> $3; num($T) -> $3; num($L) -> $4; num($M) -> $5; num($N) -> $5; num($R) -> $6. dedup(String) -> dedup(String, []). dedup([X,X|T], Acc) -> dedup([X|T], Acc); dedup([C|T], Acc) -> dedup(T, [C|Acc]); dedup([], Acc) -> lists:reverse(Acc). pad([A,B,C,D]) -> [A,B,C,D]; pad([A,B,C]) -> [A,B,C,$0]; pad([A,B]) -> [A,B,$0,$0]; pad([A]) -> [A,$0,$0,$0]; pad([]) -> [$0,$0,$0,$0]. |

Digg It
Del.icio.us
Reddit
Facebook
Stumble Upon
Technorati

