Soundex: Difference between revisions

3,205 bytes added ,  6 years ago
Line 3,181:
Soundex S532
Example E251
</pre>
===Alternative Version===
Here we're using as much PowerShell native functionaity as possible, without reaching deep into .NET libraries. The goal here is to have script that can be called from the prompt to be easily used in other scripts.
<lang PowerShell>
# script Soundex.ps1
Param([string]$Phrase)
Process {
$src = $Phrase.ToUpper().Trim()
$coded = $src[0..($src.Length - 1)] | %{
if('BFPV'.Contains($_)) { '1' }
elseif('CGJKQSXZ'.Contains($_)) { '2' }
elseif('DT'.Contains($_)) { '3' }
elseif('L'.Contains($_)) { '4' }
elseif('MN'.Contains($_)) { '5' }
elseif('R'.Contains($_)) { '6' }
elseif('AEIOU'.Contains($_)) { 'v' }
else { '.' }
} | Where { $_ -ne '.'}
$coded2 = 0..($coded.Length - 1) | %{ if ($_ -eq 0 -or $coded[$_] -ne $coded[$_ - 1]) { $coded[$_] } else { '' } }
$coded2 = if ($coded[0] -eq 'v' -or $coded2[0] -ne $coded[0]) { $coded2 } else { $coded2[1..($coded2.Length - 1)] }
$src[0] + ((-join $($coded2 | Where { $_ -ne 'v'})) + "000").Substring(0,3)
}
</lang>
 
<lang PowerShell>
Function t([string]$value, [string]$expect) {
$result = .\Soundex.ps1 -Phrase $value
New-Object –TypeName PSObject –Prop @{ "Value"=$value; "Expect"=$expect; "Result"=$result; "Pass"=$($expect -eq $result) }
}
@(
(t "Ashcraft" "A261"); (t "Ashcroft" "A261"); (t "Burroughs" "B620"); (t "Burrows" "B620");
(t "Ekzampul" "E251"); (t "Example" "E251"); (t "Ellery" "E460"); (t "Euler" "E460");
(t "Ghosh" "G200"); (t "Gauss" "G200"); (t "Gutierrez" "G362"); (t "Heilbronn" "H416");
(t "Hilbert" "H416"); (t "Jackson" "J250"); (t "Kant" "K530"); (t "Knuth" "K530");
(t "Lee" "L000"); (t "Lukasiewicz" "L222"); (t "Lissajous" "L222"); (t "Ladd" "L300");
(t "Lloyd" "L300"); (t "Moses" "M220"); (t "O'Hara" "O600"); (t "Pfister" "P236");
(t "Rubin" "R150"); (t "Robert" "R163"); (t "Rupert" "R163"); (t "Soundex" "S532");
(t "Sownteks" "S532"); (t "Tymczak" "T522"); (t "VanDeusen" "V532"); (t "Washington" "W252");
(t "Wheaton" "W350");
) | Format-Table -Property Value,Expect,Result,Pass
</lang>
{{Out}}
<pre>
Value Expect Result Pass
----- ------ ------ ----
Ashcraft A261 A261 True
Ashcroft A261 A261 True
Burroughs B620 B620 True
Burrows B620 B620 True
Ekzampul E251 E251 True
Example E251 E251 True
Ellery E460 E460 True
Euler E460 E460 True
Ghosh G200 G200 True
Gauss G200 G200 True
Gutierrez G362 G362 True
Heilbronn H416 H416 True
Hilbert H416 H416 True
Jackson J250 J250 True
Kant K530 K530 True
Knuth K530 K530 True
Lee L000 L000 True
Lukasiewicz L222 L222 True
Lissajous L222 L222 True
Ladd L300 L300 True
Lloyd L300 L300 True
Moses M220 M220 True
O'Hara O600 O600 True
Pfister P236 P236 True
Rubin R150 R150 True
Robert R163 R163 True
Rupert R163 R163 True
Soundex S532 S532 True
Sownteks S532 S532 True
Tymczak T522 T522 True
VanDeusen V532 V532 True
Washington W252 W252 True
Wheaton W350 W350 True
</pre>
 
Anonymous user