Soundex: Difference between revisions

4,513 bytes added ,  3 months ago
m
m (→‎{{header|Wren}}: Minor tidy)
 
(6 intermediate revisions by 4 users not shown)
Line 1,727:
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}}==
Line 4,512 ⟶ 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>
 
Line 4,606 ⟶ 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}}==
Line 4,840 ⟶ 4,977:
 
# Discard the first letter
word.ftlast!(-1)
 
# Remove A, E, H, I, O, U, W, and Y
Line 5,600 ⟶ 5,737:
{{libheader|Wren-str}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./str" for Char
import "./fmt" for Fmt
 
var getCode = Fn.new { |c|
9,476

edits