Text 0.6.0 doublemetaphone encode - CyrilB1531/lodestar GitHub Wiki

Lodestar.Text 0.6.0. This page is frozen at that release. Read the current documentation for what main says now. A link to a decision or a migration page follows main, and leaves the archive.

DoubleMetaphone.Encode

The two Double Metaphone codes of one word.

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

Parameters β€” value is a single word. Only its ASCII letters are read; case does not matter, and everything else β€” accents, non-Latin scripts, digits, punctuation, spacing β€” is dropped rather than rejected. The string overload forwards to the span one.

Returns β€” DoubleMetaphoneCode, a pair of variable-length codes drawn from A F H J K L M N P R S T X 0. Primary is the empty string when value holds no ASCII letter; Secondary is the empty string when the word has no alternate pronunciation.

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

Example β€” one reading, then two.

using Lodestar.Text.Phonetics;

DoubleMetaphoneCode thomas = DoubleMetaphone.Encode("Thomas");
string only = thomas.Primary;  // => TMS
string none = thomas.Secondary;  // =>

DoubleMetaphoneCode knuth = DoubleMetaphone.Encode("Knuth");
string th = knuth.Primary;  // => N0
string t = knuth.Secondary;  // => NT

Remarks β€” match on either code. Two words are phonetic candidates when any of their codes agree, so the test is four comparisons and not one; taking Primary alone throws away the reason to use this encoder over Metaphone.

Knuth above is the ordinary shape of an alternate: TH is 0 to an English ear and a plain T to a German one, and the encoder declines to choose. Wright encodes to RT with no alternate β€” the silent W is not a disagreement, just English spelling, which this encoder models the same way Metaphone.Encode does.

Accents are dropped, not folded. Γ©lan encodes as lan does, to LN, because the reference keeps ASCII letters only. That is a real limitation on non-English names rather than a rounding of one, and it is pinned by the corpus rather than asserted here, so what the reference does stays the reference's to say.

Applies to β€” net10.0, netstandard2.0.

See also β€” DoubleMetaphone, DoubleMetaphoneCode, Metaphone.Encode, the phonetics index.

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