Text soundex encode - CyrilB1531/lodestar GitHub Wiki

Development build. This page describes main, not a released package. The latest published Lodestar.Text is 0.7.0 — read its documentation.

Home › Text › Phonetic encoding

Soundex.Encode

The 4-character Soundex code of one word.

public static string Encode(ReadOnlySpan<char> value)
public static string Encode(string value)

Parameters — value is a single word. Case does not matter: the word is uppercased by the full case mapping and decomposed (NFKD). Its first character leads the code whatever it is, and any character that is not a coded consonant, H or W — a vowel, an apostrophe, a space — separates two equal codes: Keats's is K322, where dropping the apostrophe would give K320. The string overload forwards to the span one.

Returns — string: the first character followed by three digits, or the empty string when value is empty.

Exceptions — ArgumentNullException when value is null (the string overload only; a ReadOnlySpan<char> cannot be null). An empty string is accepted and encodes to the empty string.

Example — a collision, and a word with too few consonants to fill the code.

using Lodestar.Text.Phonetics;

string robert = Soundex.Encode("Robert");  // => R163
string rupert = Soundex.Encode("Rupert");  // => R163
string padded = Soundex.Encode("Lee");  // => L000

Remarks — L000 is the padding rule doing its work: Lee has one codeable consonant and the code is still four characters wide, because a fixed width is what lets Soundex be an index key.

A null word is refused, the same rule the stemmers in Lodestar.Text.Stemming apply — the null-word refusal records why the two used to disagree and why refusing won.

The two overloads are the same algorithm; the span one exists so a word already sliced out of a larger buffer can be encoded without copying it.

Applies to — net10.0, netstandard2.0.

See also — Soundex, Nysiis.Encode, the phonetics index.

⚠️ **GitHub.com Fallback** ⚠️