Soundex: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(17 intermediate revisions by 12 users not shown) | |||
Line 16:
{{trans|Java}}
<
‘1’ = [‘B’, ‘F’, ‘P’, ‘V’],
‘2’ = [‘C’, ‘G’, ‘J’, ‘K’, ‘Q’, ‘S’, ‘X’, ‘Z’],
Line 45:
print(soundex(‘Example’))
print(soundex(‘Sownteks’))
print(soundex(‘Ekzampul’))</
{{out}}
Line 58:
{{trans|VBScript}}
An example of the use of the TR opcode (translate) and the uppercase trick by 'or' with space (X'40').
<
SOUNDEX CSECT
USING SOUNDEX,R13 base register
Line 138:
ORG
YREGS
END SOUNDEX</
{{out}}
<pre>
Line 160:
=={{header|Ada}}==
<
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Characters.Handling; use Ada.Characters.Handling;
Line 201:
Put_Line(To_String(cases(i))&" = "&toSoundex(To_String(cases(i))));
end loop;
end Soundex;</
{{out}}
<pre>Soundex = S532
Line 237:
Note: The only non-standard prelude functions used are to lower, is alpha, and is digit.
These are easy enough to write, vide [[String case#ALGOL 68|String case]]
<
BEGIN
PROC encode = (CHAR c) CHAR:
Line 301:
printf(($g, 1x, g, 1x$, expected output OF soundex test[i], output));
printf(($b("ok", "not ok"), 1l$, output = expected output OF soundex test[i]))
OD</
=={{header|Arturo}}==
<
"aeiouy": `W`
"bfpv": `1`
Line 374 ⟶ 346:
loop ["Robert", "Rupert", "Rubin", "Ashcraft", "Ashcroft", "Tymczak",
"Pfister", "Honeyman", "Moses", "O'Mally", "O'Hara", "D day"] 'name ->
print [pad name 10 "->" soundex name]</
{{out}}
Line 394 ⟶ 366:
=={{header|AutoHotkey}}==
{{trans|VBScript}}
<
If c in B,F,P,V
return 1
Line 431 ⟶ 403:
}
MsgBox % Soundex("Soundex") "`n" Soundex("Sowndeks") "`n" Soundex("Ashcroft") "`n" Soundex("Ashkrofd")</
=={{header|AWK}}==
Line 437 ⟶ 409:
The soundex function is embedded in a program to build a table of soundex "homonyms".
<
BEGIN {
subsep = ", "
Line 496 ⟶ 468:
}
}
</syntaxhighlight>
Line 551 ⟶ 523:
</pre>
=={{header|
==={{header|ANSI BASIC}}===
{{trans|BBC Basic}}
{{works with|Decimal BASIC}}
Note: Line numbers (strict ANSI interpretation), <code>LET</code> and the variable after <code>NEXT</code> are obligatory.
<syntaxhighlight lang="basic">100 DECLARE EXTERNAL FUNCTION FNSoundex$
110
120 DATA Ashcraft, Ashcroft, Gauss, Ghosh, Hilbert, Heilbronn, Lee, Lloyd
130 DATA Moses, Pfister, Robert, Rupert, Rubin, Tymczak, Soundex, Example
140 FOR i = 1 TO 16
150 READ name$
160 PRINT """"; name$; """"; TAB(15); FNsoundex$(name$)
170 NEXT i
180 END
190
200 EXTERNAL FUNCTION FNsoundex$(name$)
210 LET name$ = UCASE$(name$)
220 LET n$ = "01230129022455012623019202"
230 LET s$ = name$(1:1)
240 LET p = VAL(n$(ORD(s$) - 64 : ORD(s$) - 64))
250 FOR i = 2 TO LEN(name$)
260 LET n = VAL(n$(ORD(name$(i:i)) - 64: ORD(name$(i:i)) - 64))
270 IF n <> 0 AND n <> 9 AND n <> p THEN LET s$ = s$ & STR$(n)
280 IF n <> 9 THEN LET p = n
290 NEXT i
300 LET s$ = s$ & "000"
310 LET FNSoundex$ = s$(1:4)
320 END FUNCTION</syntaxhighlight>
{{out}}
<pre>"Ashcraft" A261
"Ashcroft" A261
"Gauss" G200
"Ghosh" G200
"Hilbert" H416
"Heilbronn" H416
"Lee" L000
"Lloyd" L300
"Moses" M220
"Pfister" P236
"Robert" R163
"Rupert" R163
"Rubin" R150
"Tymczak" T522
"Soundex" S532
"Example" E251
</pre>
==={{header|BBC BASIC}}===
<syntaxhighlight lang="bbcbasic"> DATA Ashcraft, Ashcroft, Gauss, Ghosh, Hilbert, Heilbronn, Lee, Lloyd
DATA Moses, Pfister, Robert, Rupert, Rubin, Tymczak, Soundex, Example
FOR i% = 1 TO 16
Line 579 ⟶ 598:
IF C% >= 97 IF C% <= 122 MID$(A$,A%,1) = CHR$(C%-32)
NEXT
= A$</
{{out}}
<pre>
Line 605 ⟶ 624:
The word to translate is read from stdin, and its corresponding soundex encoding is written to stdout.
<
^$$_v#!*`*8 8\`\"["::-**84<
>$1^>:88*>v>$$1->vp7+2\"0"<
Line 613 ⟶ 632:
v$$:$$_^#\<1!-"0"<^1,<g7:<<
v??????????????????????????
v01230120022455012623010202</
{{out}} (multiple runs)
Line 632 ⟶ 651:
Ashcraft
A226</pre>
=={{header|BQN}}==
Defines a Soundex function which returns a string. The split function is used for generating input data.
<syntaxhighlight lang="bqn">ToUpper ← -⟜(32×1="a{"⊸⍋)
Split ← ((⊢-˜+`׬)∘=⊔⊢)
replace ← ⟨
"AEIOUYHW"
"BFPV"
"CGJKQSXZ"
"DT"
"L"
"MN"
"R"
⟩
Soundex ← ⊑∾{'0'+»⟜0‿0‿0⊑¨0⊸≠⊸/(0≠⊑)⊸↓⊑¨(¯1+·+`1»≠⟜«)⊸⊔∾/¨<˘⍉>replace∊˜¨<ToUpper 𝕩}
names ← ' ' Split "Lloyd Woolcock Donnell Baragwanath Williams Ashcroft Euler Ellery Gauss Ghosh Hilbert Heilbronn Knuth Kant Ladd Lukasiewicz Lissajous"
vals ← ' ' Split "L300 W422 D540 B625 W452 A226 E460 E460 G200 G200 H416 H416 K530 K530 L300 L222 L222"
•Show >(⊢ ⋈ Soundex)¨names
•Show vals≡Soundex¨names</syntaxhighlight>
<syntaxhighlight lang="bqn">┌─
╵ "Lloyd" "L300"
"Woolcock" "W422"
"Donnell" "D540"
"Baragwanath" "B625"
"Williams" "W452"
"Ashcroft" "A226"
"Euler" "E460"
"Ellery" "E460"
"Gauss" "G200"
"Ghosh" "G200"
"Hilbert" "H416"
"Heilbronn" "H416"
"Knuth" "K530"
"Kant" "K530"
"Ladd" "L300"
"Lukasiewicz" "L222"
"Lissajous" "L222"
┘
1</syntaxhighlight>
=={{header|C}}==
Some string examples and rules from [[http://www.archives.gov/research/census/soundex.html]].
<
#include <stdlib.h>
#include <string.h>
Line 731 ⟶ 794:
return 0;
}</
=={{header|C sharp}}==
<
using System.Collections.Generic;
using System.Linq;
Line 834 ⟶ 897:
}
}
}</
{{out}}
Line 868 ⟶ 931:
=={{header|C++}}==
<syntaxhighlight lang="c">
#include <iostream> // required for debug code in main() only
#include <iomanip> // required for debug code in main() only
Line 947 ⟶ 1,010:
}
</syntaxhighlight>
{{out|Example output}}
<pre>
Line 982 ⟶ 1,045:
=={{header|Caché ObjectScript}}==
<
Class Utils.Phonetic [ Abstract ]
{
Line 992 ⟶ 1,055:
}
</syntaxhighlight>
{{out|Examples}}
<pre>
Line 1,029 ⟶ 1,092:
=={{header|Clipper/XBase++}}==
<
/*
Line 1,092 ⟶ 1,155:
RETURN(nMatch)
*******************************************************************************</
--[[User:Clippersolutions|Clippersolutions]] 23:14, 4 November 2010 (UTC)--[[User:Clippersolutions|Clippersolutions]] 23:14, 4 November 2010 (UTC)
=={{header|Clojure}}==
<
(case c
(\B \F \P \V) 1
Line 1,115 ⟶ 1,178:
(remove nil? ,)
(take 4 ,)
(apply str ,)))))</
Bug here? The distinct function eliminates duplicates. What is needed in Soundex is to eliminate consecutive duplicates.
<syntaxhighlight lang="clojure">
;;; With proper consecutive duplicates elimination
Line 1,146 ⟶ 1,209:
(recur (apply str first-char (rest the-rest)))
(let [soundex-nums (reduce reduce-fn [] the-rest)]
(apply str first-char (take 3 (conj soundex-nums 0 0 0)))))))</
=={{header|CLU}}==
<syntaxhighlight lang="clu">lower = proc (c: char) returns (char)
if c >= 'A' & c <= 'Z' then
c := char$i2c(32 + char$c2i(c))
end
return(c)
end lower
soundex = proc (name: string) returns (string)
own coding: array[string] := array[string]$
[0:"aeiou","bfpv","cgjkqsxz","dt","l","mn","r"]
nums: array[int] := array[int]$[]
for i: int in int$from_to(1, string$size(name)) do
c: char := lower(name[i])
for n: int in array[string]$indexes(coding) do
if string$indexc(c, coding[n]) ~= 0 then
array[int]$addh(nums, n)
break
end
end
end
filtered: array[int] := array[int]$[]
for i: int in array[int]$indexes(nums) do
if nums[i]=0 cor i=1 then continue end
if nums[i]~=nums[i-1] then
array[int]$addh(filtered,nums[i])
end
end
code: string := string$c2s(name[1])
for i: int in array[int]$elements(filtered) do
if string$size(code) >= 4 then break end
code := code || int$unparse(i)
end
while string$size(code) < 4 do
code := code || "0"
end
return(code)
end soundex
start_up = proc ()
test = struct[name, code: string]
po: stream := stream$primary_output()
tests: array[test] := array[test]$[
test${name:"Ashcraft", code:"A261"},
test${name:"Burroughs", code:"B620"},
test${name:"Burrows", code:"B620"},
test${name:"Ekzampul", code:"E251"},
test${name:"Ellery", code:"E460"},
test${name:"Euler", code:"E460"},
test${name:"Example", code:"E251"},
test${name:"Gauss", code:"G200"},
test${name:"Ghosh", code:"G200"},
test${name:"Gutierrez", code:"G362"},
test${name:"Heilbronn", code:"H416"},
test${name:"Hilbert", code:"H416"},
test${name:"Jackson", code:"J250"},
test${name:"Kant", code:"K530"},
test${name:"Knuth", code:"K530"},
test${name:"Ladd", code:"L300"},
test${name:"Lee", code:"L000"},
test${name:"Lissajous", code:"L222"},
test${name:"Lloyd", code:"L300"},
test${name:"Lukasiewicz", code:"L222"},
test${name:"O'Hara", code:"O600"},
test${name:"Pfister", code:"P236"},
test${name:"Soundex", code:"S532"},
test${name:"Sownteks", code:"S532"},
test${name:"Tymczak", code:"T522"},
test${name:"VanDeusen", code:"V532"},
test${name:"Washington", code:"W252"},
test${name:"Wheaton", code:"W350"}
]
for t: test in array[test]$elements(tests) do
stream$putleft(po, t.name, 12)
stream$puts(po, " -> ")
c: string := soundex(t.name)
stream$puts(po, c)
if c ~= t.code
then stream$putl(po, " (Wrong!)")
else stream$putl(po, " (OK)")
end
end
end start_up</syntaxhighlight>
{{out}}
<pre style='height:50ex;'>Ashcraft -> A261 (OK)
Burroughs -> B620 (OK)
Burrows -> B620 (OK)
Ekzampul -> E251 (OK)
Ellery -> E460 (OK)
Euler -> E460 (OK)
Example -> E251 (OK)
Gauss -> G200 (OK)
Ghosh -> G200 (OK)
Gutierrez -> G362 (OK)
Heilbronn -> H416 (OK)
Hilbert -> H416 (OK)
Jackson -> J250 (OK)
Kant -> K530 (OK)
Knuth -> K530 (OK)
Ladd -> L300 (OK)
Lee -> L000 (OK)
Lissajous -> L222 (OK)
Lloyd -> L300 (OK)
Lukasiewicz -> L222 (OK)
O'Hara -> O600 (OK)
Pfister -> P236 (OK)
Soundex -> S532 (OK)
Sownteks -> S532 (OK)
Tymczak -> T522 (OK)
VanDeusen -> V532 (OK)
Washington -> W252 (OK)
Wheaton -> W350 (OK)</pre>
=={{header|COBOL}}==
Line 1,153 ⟶ 1,335:
{{works with|IBM Enterprise COBOL for z/OS}}
<
* Demonstrate the soundex encoding functions.
***************************************************************
Line 1,301 ⟶ 1,483:
else move 0 to char-sdx-code
end-if.
End program sndxchar.</
{{out}}
Line 1,336 ⟶ 1,518:
=={{header|Common Lisp}}==
<
(case c
((#\B #\F #\P #\V) #\1)
Line 1,357 ⟶ 1,539:
(push cg o)
finally
(return (subseq (coerce (nreverse `(#\0 #\0 #\0 ,@o)) 'string) 0 4))))))</
=={{header|Crystal}}==
{{trans|VBScript}}
<
def get_code(c : Char)
Line 1,418 ⟶ 1,600:
pairs.each { |pair|
puts "#{pair[0].ljust(9)} -> #{pair[1]} -> #{soundex(pair[0]) == pair[1]}"
}</
{{out}}
Line 1,443 ⟶ 1,625:
===Standard Version===
The D standard library (Phobos) contains a soundex function:
<
import std.string: soundex;
Line 1,457 ⟶ 1,639:
assert(soundex("Ashcroft") == "A261");
assert(soundex("Tymczak") == "T522");
}</
It works according to this document:
http://www.archives.gov/publications/general-info-leaflets/55.html
Line 1,466 ⟶ 1,648:
This version uses dynamic heap allocations in some places (replace, toupper, several string join) to allow a higher level style of coding, but this function may also be written to perform zero heap allocations. It may even return a char[4] by value, or use a given buffer like the C version.
<
/**
Line 1,526 ⟶ 1,708:
}
void main() {}</
=={{header|Delphi}}==
<syntaxhighlight lang="delphi">program SoundexDemo;
{$APPTYPE CONSOLE}
uses
System.StrUtils;
begin
Writeln(Soundex('
Writeln(Soundex('
end.</syntaxhighlight>
{{out}}
<pre>
A261
T522
</pre>
=={{header|EasyLang}}==
{{trans|Java}}
<syntaxhighlight>
trans$ = "01230120022455012623010202"
func$ code c$ .
c = strcode c$ - 64
if c > 26
c -= 32
.
return substr trans$ c 1
.
func$ soundex s$ .
code$ = substr s$ 1 1
prev$ = code code$
for i = 2 to len s$
cur$ = code substr s$ i 1
if cur$ <> "" and cur$ <> "0" and cur$ <> prev$
code$ &= cur$
.
prev$ = cur$
.
return substr code$ & "0000" 1 4
.
for v$ in [ "Soundex" "Example" "Sownteks" "Ekzampul" ]
print soundex v$
.
</syntaxhighlight>
=={{header|Elixir}}==
{{trans|Erlang}}
<
def soundex([]), do: []
def soundex(str) do
Line 1,594 ⟶ 1,797:
IO.puts Soundex.soundex("Example")
IO.puts Soundex.soundex("Sownteks")
IO.puts Soundex.soundex("Ekzampul")</
{{out}}
Line 1,606 ⟶ 1,809:
=={{header|Erlang}}==
This implements the US Census rules, where W and H are ignored but, unlike vowels, are not separators.
<
-export([soundex/1]).
Line 1,651 ⟶ 1,854:
$0
end.
</syntaxhighlight>
=={{header|F_Sharp|F#}}==
<
let soundex (s : string) =
Line 1,702 ⟶ 1,905:
0
</syntaxhighlight>
{{out}}
<pre>
Line 1,741 ⟶ 1,944:
=={{header|Factor}}==
<
"soundex" soundex ! S532
"example" soundex ! E251
"ciondecks" soundex ! C532
"ekzampul" soundex ! E251</
=={{header|Forth}}==
This implements the US Census rules, where W and H are ignored but, unlike vowels, aren't separators. Further corner cases welcome...
<
alpha-table soundex-code
Line 1,791 ⟶ 1,994:
s" Burroughs" soundex cr type \ B620
s" Burrows" soundex cr type \ B620 (W test) (any Welsh names?)
s" O'Hara" soundex cr type \ O600 (punctuation test)</
=={{header|FreeBASIC}}==
{{trans|PureBasic}}
<
Function getCode(c As String) As String
If Instr("BFPV", c) Then Return "1"
Line 1,833 ⟶ 2,036:
Data "Moses", "Pfister", "Robert", "Rupert", "Rubin", "Tymczak", "VanDeusen", "Wheaton", "Soundex", "Example"
Sleep
</syntaxhighlight>
=={{header|FutureBasic}}==
<
local fn SoundexCode( charCode as unsigned char ) as unsigned char
select charCode
case _"B", _"F", _"P", _"V"
charCode = _"1"
case _"C", _"G", _"J", _"K", _"Q", _"S", _"X", _"Z"
charCode = _"2"
case _"D", _"T"
charCode = _"3"
case _"L"
charCode = _"4"
case _"M", _"N"
charCode = _"5"
case _"R"
charCode = _"6"
case else
charCode = 0
end select
end fn = charCode
local fn SoundexCodeForWord( codeWord as CFStringRef ) as CFStringRef
NSUInteger i
CFStringRef outputStr = @"0000"
CFMutableStringRef tempStr
if ( len(codeWord) == 0 ) then exit fn
tempStr = fn MutableStringWithCapacity(0)
codeWord = ucase(fn StringByApplyingTransform( codeWord, NSStringTransformStripDiacritics, NO ))
MutableStringAppendString( tempStr, left(codeWord,1) )
charCode = fn StringCharacterAtIndex( codeWord, 0 )
charCode = fn SoundexCode( charCode )
while i < len(codeWord) - 1
i++
if (
MutableStringAppendString( tempStr, fn StringWithFormat( @"%c",charCode ) )
if ( len(tempStr) == 4 ) then break
end if
lastCode = charCode
wend
while ( len(tempStr) < 4 )
MutableStringAppendString( tempStr, @"0" )
wend
outputStr = fn StringWithString( tempStr )
end fn = outputStr
CFArrayRef names
CFStringRef name
names = @[
@"Smith",@"Johnson",@"Williams",@"Jones",@"Brown",@"Davis",@"Miller",@"Wilson",@"Moore",@"Taylor",
@"Anderson",@"Thomas",@"Jackson",@"White",@"Harris",@"Martin",@"Thompson",@"Garcia",@"Martinez",@"Robinson",
@"Clark",@"Rodriguez",@"Lewis",@"Lee",@"Walker",@"Hall",@"Allen",@"Young",@"Hernandez",@"King",
@"Wright",@"Lopez",@"Hill",@"Scott",@"Green",@"Adams",@"Baker",@"Gonzalez",@"Nelson",@"Carter",
@"Mitchell",@"Perez",@"Roberts",@"Turner",@"Phillips",@"Campbell",@"Parker",@"Evans",@"Edwards",@"Collins",
@"Stewart",@"Sanchez",@"Morris",@"Rogers",@"Reed",@"Cook",@"Morgan",@"Bell",@"Murphy",@"Bailey",
@"Rivera",@"Cooper",@"Richardson",@"Cox",@"Howard",@"Ward",@"Torres",@"Peterson",@"Gray",@"Ramirez",
@"James",@"Watson",@"Brooks",@"Kelly",@"Sanders",@"Price",@"Bennett",@"Wood",@"Barnes",@"Ross",
@"Henderson",@"Coleman",@"Jenkins",@"Perry",@"Powell",@"Long",@"Patterson",@"Hughes",@"Flores",@"Washington",
@"Butler",@"Simmons",@"Foster",@"Gonzales",@"Bryant",@"Alexander",@"Russell",@"Griffin",@"Diaz",@"Hayes"
]
NSLogSetTabInterval( 80 )
NSLog( @"Soundex codes for %ld popular American surnames:",fn ArrayCount(names) )
for name in names
NSLog( @"%@\t= %@",name,fn SoundexCodeForWord(name) )
next
NSLog(@"")
NSLog( @"Soundex codes for similar sounding names:" )
NSLog( @"Seward\t= %@" , fn SoundexCodeForWord( @"Seward" ) )
HandleEvents</syntaxhighlight>
Output:
Line 2,125 ⟶ 2,240:
=={{header|Go}}==
WP article rules, plus my interpretation for input validation.
<
import (
Line 2,207 ⟶ 2,322:
}
}
}</
{{out}}
<pre>
Line 2,226 ⟶ 2,341:
=={{header|Groovy}}==
<
def soundex(s) {
def code = ""
Line 2,250 ⟶ 2,365:
println(soundex("Example"))
println(soundex("Ekzampul"))
</syntaxhighlight>
=={{header|Haskell}}==
{{libheader|Text.PhoneticCode.Soundex}}
<
main :: IO ()
main =
mapM_ print $
((,) <*> soundexSimple) <$> ["Soundex", "Example", "Sownteks", "Ekzampul"]</
{{Out}}
<pre>("Soundex","S532")
Line 2,267 ⟶ 2,382:
=={{header|Icon}} and {{header|Unicon}}==
<
every write(x := !arglist, " => ",soundex(x))
end
Line 2,287 ⟶ 2,402:
while name[upto('.',name)] := "" # kill .
return left(name,4,"0")
end</
{{libheader|Icon Programming Library}} implements [http://www.cs.arizona.edu/icon/library/procs/soundex.htm soundex]. The above version is an adaptation of that procedure
=={{header|IS-BASIC}}==
<
110 FOR I=1 TO 20
120 READ NAME$
Line 2,310 ⟶ 2,425:
270 END DEF
280 DATA Aschraft,Ashcroft,Euler,Gauss,Ghosh,Hilbert,Heilbronn,Lee,Lissajous,Lloyd
290 DATA Moses,Pfister,Robert,Rupert,Rubin,Tymczak,VanDeusen,Wheaton,Soundex,Example</
=={{header|J}}==
'''Solution'''
<
codes =: ;: 'BFPV CGJKQSXZ DT L MN R HW'
Line 2,320 ⟶ 2,435:
if. 0=# k=.toupper y do. '0' return. end.
({.k), ,": ,. 3 {. 0-.~ }. removeDups 7 0:`(I.@:=)`]} , k >:@I.@:(e. &>)"0 _ codes
)</
'''Usage'''
<
soundexNames=: 'L300 W422 D540 B625 W452 A226 E460 E460 G200 G200 H416 H416 K530 K530 L300 L222 L222'
Line 2,331 ⟶ 2,446:
B625
W452
....</
'''Test'''
<
1</
=={{header|Java}}==
{{trans|VBScript}}
<
System.out.println(soundex("Soundex"));
System.out.println(soundex("Example"));
Line 2,381 ⟶ 2,496:
soundex = (code + "0000").substring(0, 4);
return soundex;
}</
{{out}}
<pre>S532
Line 2,392 ⟶ 2,507:
===ES5===
==== Version w/o RegExp ====
<
var a = s.toLowerCase().split('')
f = a.shift(),
Line 2,472 ⟶ 2,587:
// Burroughs B622 B622 true
// Burrows B620 B620 true
// O'Hara O600 O600 true</
Line 2,479 ⟶ 2,594:
Note: This version differs from the one above in the following way. According to U.S. National Archives Website, consecutive consonants which map to the same code are not condensed to a single occurrence of the code if they are separated by vowels, but separating W and H do not thus intervene. Therefore Ashcraft is coded A261 and Burroughs is coded B620 rather than A226 and B622
<
function soundex(t) {
t = t.toUpperCase().replace(/[^A-Z]/g, '');
Line 2,521 ⟶ 2,636:
console.log('soundex("' + a + '") was ' + d + ' should be ' + t);
}
}); </
===ES6===
Line 2,528 ⟶ 2,643:
(Reusing set of tests from second contribution)
<
'use strict';
Line 2,656 ⟶ 2,771:
) : a;
}, '');
})();</
{{Out}}
Line 2,666 ⟶ 2,781:
=={{header|Julia}}==
There is a Soundex package for Julia. If that is used:
<
using Soundex
@assert soundex("Ashcroft") == "A261" # true
Line 2,717 ⟶ 2,832:
@assert soundex("Burrows") == "B620"
@assert soundex("O'Hara") == "O600"
</syntaxhighlight>
=={{header|Kotlin}}==
{{trans|VBScript}}
<
fun getCode(c: Char) = when (c) {
Line 2,768 ⟶ 2,883:
println("${pair.first.padEnd(9)} -> ${pair.second} -> ${soundex(pair.first) == pair.second}")
}
}</
{{out}}
Line 2,792 ⟶ 2,907:
=={{header|Lua}}==
Adapt from D Alternative
<
d:gsub(".",function(c)
digits[string.char(alpha)] = c
Line 2,833 ⟶ 2,948:
assert( soundex(itm[1])==itm[2] )
end
print"all tests ok"</
{{out}}
<pre>all tests ok</pre>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
{head, body} = {First@#, Rest@#}&@ToLowerCase@Characters@x;
body = (Select[body, FreeQ[Characters["aeiouyhw"],#]&] /. {("b"|"f"|"p"|"v")->1,
Line 2,846 ⟶ 2,961:
body = DeleteDuplicates[body]
];
StringJoin @@ ToString /@ PrependTo[ body[[1 ;; 3]], ToUpperCase@head]]</
Example usage:
<pre>Map[Soundex,{"Soundex", "Sownteks", "Example", "Ekzampul"}]
Line 2,852 ⟶ 2,967:
=={{header|MUMPS}}==
<
;Converts a string to its Soundex value.
;Empty strings return "0000". Non-alphabetic ASCII characters are ignored.
Line 2,900 ⟶ 3,015:
KILL UP,LO,PREFIX,X1,X2,Y2,C,DX,XD
QUIT Y
</syntaxhighlight>
<p>Examples:<pre>
USER>W $$SOUNDEX^SOUNDEX("")
Line 2,941 ⟶ 3,056:
=={{header|NetRexx}}==
{{trans|Rexx}}
<syntaxhighlight lang="netrexx">
class Soundex
Line 3,045 ⟶ 3,160:
say test[i].left(10) get_soundex(test[i]) '=' result_[i]
end
</syntaxhighlight>
{{out}}
<pre>
Line 3,120 ⟶ 3,235:
=={{header|Nim}}==
<
const
Line 3,163 ⟶ 3,278:
for name in ["Robert", "Rupert", "Rubin", "Ashcraft", "Ashcroft", "Tymczak",
"Pfister", "Honeyman", "Moses", "O'Mally", "O'Hara", "D day"]:
echo name.align(8), " ", soundex(name)</
{{out}}
Line 3,182 ⟶ 3,297:
{{trans|Java}}
<
function : Main(args : String[]) ~ Nil {
SoundEx("Soundex")->PrintLine();
Line 3,244 ⟶ 3,359:
}
}
</syntaxhighlight>
{{out}}
Line 3,258 ⟶ 3,373:
Here is an implementation:
<
| 'B' | 'F' | 'P' | 'V' -> "1"
| 'C' | 'G' | 'J' | 'K' | 'Q' | 'S' | 'X' | 'Z' -> "2"
Line 3,292 ⟶ 3,407:
match dbl [] (List.rev !cl) with
| c::rem -> (String.make 1 c) ^ (soundex_aux rem)
| [] -> invalid_arg "soundex"</
Test our implementation:
<
"Soundex", "S532";
"Example", "E251";
Line 3,326 ⟶ 3,441:
let status = if code1 = code2 then "OK " else "Arg" in
Printf.printf " \"%s\" \t %s %s %s\n" word code1 code2 status
) tests</
{{out}}
Line 3,360 ⟶ 3,475:
{{works with|Free Pascal|2.6.2}}
<
{$mode objfpc}{$H+}
Line 3,467 ⟶ 3,582:
end;
ReadLn;
End.</
{{out}}
Line 3,490 ⟶ 3,605:
=={{header|Perl}}==
The <tt>Text::Soundex</tt> core module supports various soundex algorithms.
<
print soundex("Soundex"), "\n"; # S532
print soundex("Example"), "\n"; # E251
print soundex("Sownteks"), "\n"; # S532
print soundex("Ekzampul"), "\n"; # E251</
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">soundex_alphabet</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0123012#02245501262301#202"</span>
<span style="color: #000080;font-style:italic;">-- ABCDEFGHIJKLMNOPQRSTUVWXYZ</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">soundex</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">name</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0000"</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">rdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">prev</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">ch</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">upper</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;">>=</span><span style="color: #008000;">'A'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">ch</span><span style="color: #0000FF;"><=</span><span style="color: #008000;">'Z'</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">curr</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">soundex_alphabet</span><span style="color: #0000FF;">[</span><span style="color: #000000;">ch</span><span style="color: #0000FF;">-</span><span style="color: #008000;">'A'</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">ch</span>
<span style="color: #000000;">rdx</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">2</span>
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
<span style="color: #008080;">elsif</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">'#'</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">!=</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">and</span> <span style="color: #000000;">curr</span><span style="color: #0000FF;">!=</span><span style="color: #000000;">prev</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">res</span><span style="color: #0000FF;">[</span><span style="color: #000000;">rdx</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">rdx</span><span style="color: #0000FF;">=</span><span style="color: #000000;">4</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">rdx</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">curr</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tests</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Ashcraft"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"A261"</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- not "A226"</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Ashcroft"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"A261"</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- not "A226"</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Ashkrofd"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"A261"</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- not "A226"</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Burroughs"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"B620"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Burrows"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"B620"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"ciondecks"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"C532"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Example"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E251"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Ekzampul"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E251"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Ellery"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E460"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Euler"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"E460"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Gauss"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"G200"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Ghosh"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"G200"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Gutierrez"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"G362"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"He"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"H000"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Heilbronn"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"H416"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Hilbert"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"H416"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Honeyman"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"H555"</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- not "H500"</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Jackson"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"J250"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Kant"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"K530"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Knuth"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"K530"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Lee"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"L000"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Ladd"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"L300"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Lloyd"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"L300"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Lissajous"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"L222"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Lukasiewicz"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"L222"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Moses"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"M220"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"O'Hara"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"O600"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Pfister"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"P236"</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- not "P123"</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Robert"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"R163"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Rupert"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"R163"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Rubin"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"R150"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"r~@sum~@"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"R250"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Soundex"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"S532"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Sownteks"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"S532"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Tymczak"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"T522"</span><span style="color: #0000FF;">},</span> <span style="color: #000080;font-style:italic;">-- not "T520"</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"VanDeusen"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"V532"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Washington"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"W252"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Wheaton"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"W350"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"Weeton"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"W350"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"0000"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">" "</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"0000"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"12346"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"0000"</span><span style="color: #0000FF;">},</span>
<span style="color: #0000FF;">{</span><span style="color: #008000;">"aaa a"</span><span style="color: #0000FF;">,</span> <span style="color: #008000;">"A000"</span><span style="color: #0000FF;">}</span>
<span style="color: #0000FF;">}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tests</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">string</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">expected</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">tests</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">soundex</span><span style="color: #0000FF;">(</span><span style="color: #000000;">name</span><span style="color: #0000FF;">),</span>
<span style="color: #000000;">ok</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #000000;">expected</span><span style="color: #0000FF;">?</span><span style="color: #008000;">""</span><span style="color: #0000FF;">:</span><span style="color: #008000;">"*** ERROR ***"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%-12s -> %s %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">name</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">ok</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,625 ⟶ 3,743:
=={{header|PHP}}==
PHP already has a built-in <tt>soundex()</tt> function:
<
echo soundex("Soundex"), "\n"; // S532
echo soundex("Example"), "\n"; // E251
echo soundex("Sownteks"), "\n"; // S532
echo soundex("Ekzampul"), "\n"; // E251
?></
=={{header|Picat}}==
{{trans|C#}}
<syntaxhighlight lang="picat">go =>
Names = split("Lloyd Woolcock Donnell Baragwanath Williams Ashcroft Ashcraft Euler
Ellery Gauss Ghosh Hilbert Heilbronn Knuth Kant Ladd Lukasiewicz Lissajous O'Hara"),
SoundexNames = split("L300 W422 D540 B625 W452 A261 A261 E460
E460 G200 G200 H416 H416 K530 K530 L300 L222 L222 O600"),
foreach({Name,Correct} in zip(Names, SoundexNames))
S = soundex(Name),
printf("%s: %s ",Name,S),
if S == Correct then
println("ok")
else
printf("not correct! Should be: %s\n", Correct)
end
end,
nl.
soundex("", _) = "" => true.
soundex(Word) = Soundex =>
SoundexAlphabet = "0123012#02245501262301#202",
Soundex = "",
LastC = '?',
foreach(Ch in Word.to_uppercase,
C = ord(Ch), C >= 0'A', C <= 0'Z',
Soundex.len < 4)
ThisC := SoundexAlphabet[C-0'A'+1],
Skip = false, % to handle '#'
if Soundex.len == 0 then
Soundex := Soundex ++ [Ch]
elseif ThisC == '#' then
Skip := true
elseif ThisC != '0', ThisC != LastC then
Soundex := Soundex ++ [ThisC]
end,
if Skip == false then
LastC := ThisC
end
end,
Soundex := Soundex.padRight(4,'0').
padRight(S,Len,PadChar) = S ++ [PadChar : _ in 1..Len-S.len].</syntaxhighlight>
{{out}}
<pre>Lloyd: L300 ok
Woolcock: W422 ok
Donnell: D540 ok
Baragwanath: B625 ok
Williams: W452 ok
Ashcroft: A261 ok
Ashcraft: A261 ok
Euler: E460 ok
Ellery: E460 ok
Gauss: G200 ok
Ghosh: G200 ok
Hilbert: H416 ok
Heilbronn: H416 ok
Knuth: K530 ok
Kant: K530 ok
Ladd: L300 ok
Lukasiewicz: L222 ok
Lissajous: L222 ok
O'Hara: O600 ok</pre>
=={{header|PicoLisp}}==
Simple:
<
(pack
(pad -4
Line 3,654 ⟶ 3,837:
(<> Last C)
(setq Last C) ) )
(cdr (chop Str)) ) ) ) ) ) ) )</
NARA:
<
(let (Str (chop Str) Last)
(pack
Line 3,682 ⟶ 3,865:
(<> Last C)
(setq Last C) ) )
Str ) ) ) ) ) ) ) ) )</
=={{header|PL/I}}==
<
declare pword character (*) varying, value character (length(pword)) varying;
declare word character (length(pword));
Line 3,723 ⟶ 3,906:
return ( left(value, 4, '0') ); /* Pad, if necessary. */
end Soundex;</
=={{header|PowerShell}}==
{{works with|PowerShell 3.0}}
<syntaxhighlight lang="powershell">
function Get-Soundex
{
Line 3,809 ⟶ 3,992:
}
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
"Ashcraft", "Ashcroft", "Gauss", "Ghosh", "Hilbert", "Heilbronn", "Lee", "Lloyd",
"Moses", "Pfister", "Robert", "Rupert", "Rubin", "Tymczak", "Soundex", "Example" | Get-Soundex
</syntaxhighlight>
{{Out}}
<pre>
Line 3,837 ⟶ 4,020:
===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.
<syntaxhighlight lang="powershell">
# script Soundex.ps1
Param([string]$Phrase)
Line 3,856 ⟶ 4,039:
$src[0] + ((-join $($coded2 | Where { $_ -ne 'v'})) + "000").Substring(0,3)
}
</syntaxhighlight>
<syntaxhighlight lang="powershell">
Function t([string]$value, [string]$expect) {
$result = .\Soundex.ps1 -Phrase $value
Line 3,874 ⟶ 4,057:
(t "Wheaton" "W350");
) | Format-Table -Property Value,Expect,Result,Pass
</syntaxhighlight>
{{Out}}
<pre>
Line 3,916 ⟶ 4,099:
=={{header|Prolog}}==
Note: Rather than produce a terse and incomprehensible example, this demonstrates how simply a set of logical rules can be translated into Prolog.
<
% Implements the American soundex algorithm
% as described at https://en.wikipedia.org/wiki/Soundex
Line 3,990 ⟶ 4,173:
test :- test('Tymczak', 't522'), !, fail.
test :- test('Pfister', 'p236'), !, fail.
test. % Succeeds only if all the tests succeed</
=={{header|PureBasic}}==
<
Protected getCode.s = ""
Line 4,033 ⟶ 4,216:
PrintN (soundex("Lukasiewicz"))
PrintN("Press any key to exit"): Repeat: Until Inkey() <> ""</
=={{header|Python}}==
<
def soundex(word):
Line 4,046 ⟶ 4,229:
sdx3 = sdx2[0:4].ljust(4,'0')
return sdx3
</syntaxhighlight>
{{out}}
<
S532
>>>print soundex("example")
Line 4,055 ⟶ 4,238:
C532
>>>print soundex("ekzampul")
E251</
=={{header|Racket}}==
Line 4,064 ⟶ 4,247:
US census algorithm, so "Ashcraft" and "Burroughs" adjusted to match.
We fake up a first consonant in some cases to make up for the fact that we always trim the first numeric code (so that the 'l' of 'Lloyd' is properly deleted).
<syntaxhighlight lang="raku"
my $first = substr($name,0,1).uc;
gather {
Line 4,112 ⟶ 4,295:
my $s2 = soundex($n);
say $n.fmt("%16s "), $s, $s eq $s2 ?? " OK" !! " NOT OK $s2";
}</
{{out}}
<pre> Soundex S532 OK
Line 4,145 ⟶ 4,328:
:* letters of the ASCII-extended character set are ignored.
:* ASCII-extended characters (ÇüéâäàåçêëèïîìÄÅÉæÆôöòûùÿÖÜíóúñÑ) could be added to the program easily.
<
_=; @.= /*set a couple of vars to "null".*/
parse arg @.0 . /*allow input from command line. */
Line 4,243 ⟶ 4,426:
end /*j*/
return left(value,4,0) /*padded value with zeroes. */</
'''output''' when using the default (internal) inputs:
<pre style="height:33ex">
Line 4,318 ⟶ 4,501:
=={{header|Ring}}==
<
# Project: Soundex
Line 4,339 ⟶ 4,522:
next
return left(s + "000", 4)
</syntaxhighlight>
Output:
<pre>
Line 4,358 ⟶ 4,541:
"Soundex" S532
"Example" E251
</pre>
=={{header|RPL}}==
≪ "a123e12Xi22455o12623u1X2X2" "123456" → name table codes
≪ name 1 1 SUB ""
1 name SIZE '''FOR''' j
name j DUP SUB NUM
R→B #DFh AND B→R
'''IF''' 65 OVER ≤ OVER 90 ≤ AND '''THEN'''
table SWAP 64 - DUP SUB
'''IF''' DUP "X" ≠ '''THEN''' + '''ELSE''' DROP '''END'''
'''ELSE''' DROP '''END'''
'''NEXT'''
'name' STO
2 name SIZE '''FOR''' j
name j DUP SUB
'''IF''' codes OVER POS OVER name j 1 - DUP SUB ≠ AND '''THEN''' + '''ELSE''' DROP '''END'''
'''NEXT'''
"000" + 1 4 SUB
≫ ≫ '<span style="color:blue">SOUNDX</span>' STO
French Soundex code can be generated by modifying the table to "a123e97Xi72455o12683u9X8y8" and the codes to "123456789"
≪ { "Ashcraft" "Ashcroft" "Gauss" "Ghosh" "Ghosn" "Hilbert" "Heilbronn" "Lee" "Lloyd" "Moses" "Pfister" "Robert" "Rupert" "Rubin" "Tymczak" "Soundex" "Example" } { }
1 3 PICK SIZE '''FOR''' j
OVER j GET <span style="color:blue">SOUNDX</span> + '''NEXT'''
SWAP DROP
≫ '<span style="color:blue">TESTS</span>' STO
{{out}}
<pre>
1: { { "A261" "A261" "G200" "G200" "G250" "H416" "H416" "L000" "L300" "M220" "P236" "R163" "R163" "R150" "T522" "S532" "E251" } }
</pre>
=={{header|Ruby}}==
Courtesy http://snippets.dzone.com/posts/show/4530
<
SoundexChars = 'BFPVCGJKQSXZDTLMNR'
Line 4,389 ⟶ 4,602:
print word1.sounds_like(word2) ? "sounds" : "does not sound"
print " like '#{word2}'\n"
end</
<pre>Soundex -> S532
Line 4,403 ⟶ 4,616:
=={{header|Run BASIC}}==
Courtesy http://dkokenge.com/rbp
<
val$(1) = "BPFV"
val$(2) = "CSGJKQXZ"
Line 4,444 ⟶ 4,657:
next i
soundex$ = left$(a$,1) + left$(sdx$;"000",3)
end function</
<pre>Robert soundex:R163
Line 4,452 ⟶ 4,665:
O'Mally soundex:O054
d jay soundex:D200</pre>
=={{header|Rust}}==
<syntaxhighlight lang="rust">use std::ops::Deref;
use regex::Regex;
use once_cell::sync::Lazy;
pub trait Soundex {
fn soundex(&self) -> String;
}
fn soundex_match(c: char) -> char {
return match c.to_ascii_lowercase() {
'b' | 'f' | 'p' | 'v' => Some('1'),
'c' | 'g' | 'j' | 'k' | 'q' | 's' | 'x' | 'z' => Some('2'),
'd' | 't' => Some('3'),
'l' => Some('4'),
'm' | 'n' => Some('5'),
'r' => Some('6'),
_ => Some('0'),
}.unwrap();
}
static RE: Lazy<Regex> = Lazy::new(|| {Regex::new("[^a-zA-Z]").unwrap()});
impl<T: Deref<Target = str>> Soundex for T {
fn soundex(&self) -> String {
let s = RE.replace(self, "").chars().collect::<Vec<char>>();
if s.len() == 0 {
return String::new();
}
let mut a = vec![s[0].to_ascii_uppercase(); 1].to_vec();
let mut last_sdex = soundex_match(a[0]);
let mut hadvowel = false;
for ch in &s[1..s.len()] {
let lc_ch = ch.to_ascii_lowercase();
let sdex = soundex_match(lc_ch);
if sdex != '0' {
if sdex != last_sdex || hadvowel {
a.push(sdex);
last_sdex = sdex;
hadvowel = false;
}
}
else if "aeiouy".contains(lc_ch) {
hadvowel = true;
}
}
if a.len() < 4 {
for _ in 0..(4 - a.len()) {
a.push('0');
}
}
return a[0..4].into_iter().collect();
}
}
fn main() {
assert_eq!("Ascroft".soundex(), "A261".to_string());
assert_eq!("Euler".soundex(), "E460".to_string());
assert_eq!("Gausss".soundex(), "G200".to_string());
assert_eq!("Hilbert".soundex(), "H416".to_string());
assert_eq!("Knuth".soundex(), "K530".to_string());
assert_eq!("Lloyd".soundex(), "L300".to_string());
assert_eq!("Lukasiewicz".soundex(), "L222".to_string());
assert_eq!("Ellery".soundex(), "E460".to_string());
assert_eq!("Ghosh".soundex(), "G200".to_string());
assert_eq!("Heilbronn".soundex(), "H416".to_string());
assert_eq!("Kant".soundex(), "K530".to_string());
assert_eq!("Ladd".soundex(), "L300".to_string());
assert_eq!("Lissajous".soundex(), "L222".to_string());
assert_eq!("Wheaton".soundex(), "W350".to_string());
assert_eq!("Ashcraft".soundex(), "A261".to_string());
assert_eq!("Burroughs".soundex(), "B620".to_string());
assert_eq!("Burrows".soundex(), "B620".to_string());
assert_eq!("O'Hara".soundex(), "O600".to_string());
}
</syntaxhighlight>
=={{header|Scala}}==
<
var code=s.head.toUpper.toString
var previous=getCode(code.head)
Line 4,478 ⟶ 4,769:
case _ => ""
}
}</
<
val tests=Map(
"Soundex" -> "S532",
Line 4,506 ⟶ 4,797:
printf("Name: %-20s Code: %s Found: %s - %s\n", v._1, v._2, code, status)
}
}</
=={{header|Scheme}}==
Line 4,514 ⟶ 4,805:
{{works with|any R6RS Scheme}}
<
;;
;; The soundex code consist of the first letter of the name followed
Line 4,583 ⟶ 4,874:
(soundex "Uhrbach")
(soundex "Moskowitz")
(soundex "Moskovitz")</
{{out}}
Line 4,596 ⟶ 4,887:
=={{header|SenseTalk}}==
<
"Heilbronn","Hilbert","Honeyman","Jackson","Lee","LeGrand","Lissajous","Lloyd",
"Moses","Pfister","Robert","Rupert","Rubin","Tymczak","VanDeusen","Van de Graaff","Wheaton"]
Line 4,636 ⟶ 4,927:
return soundex
end soundex
</syntaxhighlight>
{{out}}
<pre>
Line 4,666 ⟶ 4,957:
=={{header|Sidef}}==
<
# Uppercase the argument passed in to normalize it
Line 4,686 ⟶ 4,977:
# Discard the first letter
word.
# Remove A, E, H, I, O, U, W, and Y
Line 4,720 ⟶ 5,011:
}
testSoundex()</
=={{header|Smalltalk}}==
Line 4,726 ⟶ 5,017:
{{works with|Smalltalk/X}}
using a builtin utility:
<
=={{header|SNOBOL4}}==
Line 4,736 ⟶ 5,027:
US National Archives (NARA) Soundex. Includes the "HW" rule omitted by Knuth and many other implementations.
<
* # ABCDEFGHIJKLMNOPQRSTUVWXYZ
* # 01230127022455012623017202
Line 4,761 ⟶ 5,052:
loop test span(' ') break(' ') . name = :f(end)
output = soundex(name) ' ' name :(loop)
end</
{{out}}
Line 4,777 ⟶ 5,068:
This implementation uses datatypes to encode the different rules for handling duplicate digits
when different characters appear in the input:
<
* h and w are ignored completely (letters separated by h or w are considered
* adjacent, or merged together)
Line 4,842 ⟶ 5,133:
val () = test "Rubin" "R150"
val () = test "Tymczak" "T522"
val () = test "Pfister" "P236"</
=={{header|Stata}}==
The soundex function is built-in. See [http://www.stata.com/help.cgi?soundex Stata help].
<
A261
. display soundex_nara("Tymczak")
T522</
There is also a variant:
<
A226</
=={{header|Tcl}}==
{{tcllib|soundex}} contains an implementation of Knuth's soundex algorithm.
<
foreach string {"Soundex" "Example" "Sownteks" "Ekzampul"} {
set soundexCode [soundex::knuth $string]
puts "\"$string\" has code $soundexCode"
}</
{{out}}
<pre>"Soundex" has code S532
Line 4,873 ⟶ 5,164:
=={{header|TMG}}==
Unix TMG:
<
let: peek/done
[ch = ch>140 ? ch-40 : ch ]
Line 4,931 ⟶ 5,222:
ch: 0;
l1: 0;
l2: 0;</
=={{header|TSE SAL}}==
<syntaxhighlight lang="tse sal">
// library: string: get: soundex <description></description> <version>1.0.0.0.35</version> <version control></version control> (filenamemacro=getstgso.s) [kn, ri, sa, 15-10-2011 18:23:04]
Line 5,018 ⟶ 5,309:
END
</syntaxhighlight>
=={{header|TUSCRIPT}}==
<
$$ MODE DATA
Line 5,052 ⟶ 5,343:
PRINT first,soundex,"=",n
ENDLOOP
</syntaxhighlight>
{{out}}
<pre style='height:30ex;overflow:scroll'>
Line 5,086 ⟶ 5,377:
This implements the full Soundex described in [[http://www.archives.gov/research/census/soundex.html U.S. National Archives Website]]. Doubled letters are condensed before separating the first letter, so that for instance "Lloyd" is not treated as L followed by the coding of LOYD but as L followed by the coding of OYD. Consecutive consonants which map to the same code are not condensed to a single occurrence of the code if they are separated by vowels, but separating W and H do not thus intervene. Names with common prefixes are encoded in two ways.
<
@###
@# soundex-related filters
Line 5,159 ⟶ 5,450:
"@first_arg" and "@second_arg" match under soundex
@ (end)
@(end)</
Run:
Line 5,179 ⟶ 5,470:
This solution is similar to some of the solutions in other languages. Its treatment of the algorithm is not as complete as the above solution.
<
(caseq c
((#\B #\F #\P #\V) #\1)
Line 5,205 ⟶ 5,496:
@arg -> @(soundex arg)
@ (end)
@(end)</
Run:
Line 5,218 ⟶ 5,509:
The following functions require this associative array to be declared:
<
[B]=1 [F]=1 [P]=1 [V]=1
[C]=2 [G]=2 [J]=2 [K]=2 [Q]=2 [S]=2 [X]=2 [Z]=2
Line 5,225 ⟶ 5,516:
[M]=5 [N]=5
[R]=6
)</
The first algorithm described at https://en.wikipedia.org/wiki/Soundex#American_Soundex can be implemented like this:
<
local -u word=${1//[^[:alpha:]]/.}
local letter=${word:0:1}
Line 5,264 ⟶ 5,555:
soundex+="000"
echo "${soundex:0:4}"
}</
The "simplified" algorithm can be implemented like this:
<
local -u word=${1//[^[:alpha:]]/}
Line 5,311 ⟶ 5,602:
word+="000"
echo "${word:0:4}"
}</
If we cheat a bit and allow calling out to `tr`, we can do:
<
local -u word=${1//[^[:alpha:]]/}
Line 5,335 ⟶ 5,626:
word+="000"
echo "${word:0:4}"
}</
And some testing code:
<
[Soundex]=S532 [Example]=E251 [Sownteks]=S532 [Ekzampul]=E251
[Euler]=E460 [Gauss]=G200 [Hilbert]=H416 [Knuth]=K530
Line 5,366 ⟶ 5,657:
run_tests soundex
run_tests soundex2
run_tests soundex3</
{{out}}
Line 5,378 ⟶ 5,669:
=={{header|VBScript}}==
<
tt=array( _
"Ashcraft","Ashcroft","Gauss","Ghosh","Hilbert","Heilbronn","Lee","Lloyd", _
Line 5,421 ⟶ 5,712:
soundex = Mid(code & "000", 1, 4)
End Function 'soundex
</syntaxhighlight>
{{out}}
<pre>
Line 5,446 ⟶ 5,737:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<
import "./fmt" for Fmt
var getCode = Fn.new { |c|
Line 5,492 ⟶ 5,783:
for (pair in pairs) {
Fmt.print("$-9s -> $s -> $s", pair[0], pair[1], soundex.call(pair[0]) == pair[1])
}</
{{out}}
Line 5,515 ⟶ 5,806:
=={{header|XPL0}}==
<
string 0; \use zero-terminated strings
Line 5,549 ⟶ 5,840:
Text(0, Name(I)); CrLf(0);
];
]</
{{out}}
|