Soundex: Difference between revisions
Content added Content deleted
(→{{header|Tcl}}: Expand example) |
m (→{{header|Tcl}}: put note where it belongs) |
||
Line 25: | Line 25: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
{{libheader|tcllib}} contains an implementation of |
{{libheader|tcllib}} contains an implementation of Knuth's soundex algorithm in the <code>soundex</code> package. |
||
<lang tcl>package require soundex |
<lang tcl>package require soundex |
||
foreach string {"Soundex" "Example" "Sownteks" "Ekzampul"} { |
foreach string {"Soundex" "Example" "Sownteks" "Ekzampul"} { |
||
# The package uses Knuth's algorithm... |
|||
set soundexCode [soundex::knuth $string] |
set soundexCode [soundex::knuth $string] |
||
puts "\"$string\" has code $soundexCode" |
puts "\"$string\" has code $soundexCode" |
Revision as of 14:27, 12 November 2009
Soundex
You are encouraged to solve this task according to the task description, using any language you may know.
You are encouraged to solve this task according to the task description, using any language you may know.
JavaScript
<lang javascript> var soundex = function (s) {
var a = s .substring(1, s.length) .split(), r = , codes = { a: , e: , i: , o: , u: , b: 1, f: 1, p: 1, v: 1, c: 2, g: 2, j: 2, k: 2, q: 2, s: 2, x: 2, z: 2, d: 3, t: 3, l: 4, m: 5, n: 5, r: 6 }; r = s[0].toUpperCase() + a .filter(function (v, i, a) { return v !== a[i + 1]; }) .map(function (v, i, a) { return codes[v] }).join(); return (r + '000').slice(0, 4); };</lang>
Tcl
contains an implementation of Knuth's soundex algorithm in the soundex
package.
<lang tcl>package require soundex
foreach string {"Soundex" "Example" "Sownteks" "Ekzampul"} {
set soundexCode [soundex::knuth $string] puts "\"$string\" has code $soundexCode"
}</lang> Which produces this output:
"Soundex" has code S532 "Example" has code E251 "Sownteks" has code S532 "Ekzampul" has code E251
VBScript
<lang vbscript> Function getCode(c)
Select Case c Case "B", "F", "P", "V" getCode = "1" Case "C", "G", "J", "K", "Q", "S", "X", "Z" getCode = "2" Case "D", "T" getCode = "3" Case "L" getCode = "4" Case "M", "N" getCode = "5" Case "R" getCode = "6" End Select End Function Function soundex(s) Dim code, previous code = UCase(Mid(s, 1, 1)) previous = 7 For i = 2 to (Len(s) + 1) current = getCode(UCase(Mid(s, i, 1))) If Len(current) > 0 And current <> previous Then code = code & current End If previous = current Next soundex = Mid(code, 1, 4) If Len(code) < 4 Then soundex = soundex & String(4 - Len(code), "0") End If End Function</lang>