Algorytm ten jest ustawiony tak, aby działał nad pierwszym słowem lub dopóki nie wypełni czterech zakodowanych ciągów. Na przykład wynik wejścia "Horrible Great" to: H612. Pomija drugie słowo, lub innymi słowy, zajmuje tylko pierwszą literę z drugiego słowa, aby wypełnić zakodowany ciąg.Niektóre zmiany w algorytmie Soundex
Chciałbym to zmienić, biorąc pierwsze słowo i znaleźć jego zakodowany ciąg, a następnie wziąć drugie słowo i znaleźć jego zakodowany ciąg; wyjście powinno być "H614 G600". Uprzejmie chciałbym wiedzieć, czy jest sposób na zrobienie tego poprzez zmianę ** na ten kod .
Dziękuję bardzo :)
private string Soundex(string data)
{
StringBuilder result = new StringBuilder();
if (data != null && data.Length > 0)
{
string previousCode = "", currentCode = "", currentLetter = "";
result.Append(data.Substring(0, 1));
for (int i = 1; i < data.Length; i++)
{
currentLetter = data.Substring(i,1).ToLower();
currentCode = "";
if ("bfpv".IndexOf(currentLetter) > -1)
currentCode = "1";
else if ("cgjkqsxz".IndexOf(currentLetter) > -1)
currentCode = "2";
else if ("dt".IndexOf(currentLetter) > -1)
currentCode = "3";
else if (currentLetter == "l")
currentCode = "4";
else if ("mn".IndexOf(currentLetter) > -1)
currentCode = "5";
else if (currentLetter == "r")
currentCode = "6";
if (currentCode != previousCode)
result.Append(currentCode);
if (result.Length == 4) break;
if (currentCode != "")
previousCode = currentCode;
}
}
if (result.Length < 4)
result.Append(new String('0', 4 - result.Length));
return result.ToString().ToUpper();
}
JUŻ NIESAMOWITE! dziękuję bardzo za dzielenie się. pozwala mi edytować kod napisałem powyżej: private String SoundexByWord danych (String) {var soundexes = new List(); foreach (var str w data.Split ('')) { soundexes.Add (Soundex (str)); } return string.Join ("", soundexes.ToArray()); // Konwertuj listę } // na tablicę, która łączy funkcję Fun. pobiera ciąg tablic [] :) –
user979014
To dobra uwaga. Oryginalna odpowiedź była oparta na .Net 4. W oparciu o Twoją sugestię, rozszerzyłem odpowiedź o wcześniejsze wersje. – jhamm
Podziwiam twoją ostatnią edycję i sposób wyjaśnienia :) jeszcze raz dziękuję – user979014