Когда я тестировал алгоритм Soundex
по Wikipedia article on Soundex, я обнаружил, что Tymczak вернулся T520, а не T522 и Пфистер тел .: P123, а не P236.Выход реализации алгоритма Soundex является неправильным для случаев - «Tymczak» и «Пфистер»
Я понятия не имею, почему вывод неправильный.
Мой код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace ConsoleApplication4
{
class Program
{
static void Main(string[] args)
{
string s = "Tymczak";
string result = SoundexByWord(s);
Console.WriteLine(result);
}
private static string Soundex(string data)
{
string first = "pv";
string second = "xz";
string third = "dt";
string forth = "mn";
string fifth = "bf";
string sixth = "cgj";
string seventh = "kqs";
//ana ast5dtmt string builder 34an 3aml zy al list fy apend
// 34an a apend mn al a5r al string
StringBuilder result = new StringBuilder();
if (data != null && data.Length > 0)
{
string previousCode = "", currentCode = "", currentLetter = "";
// append on the string from the last
// get the first characheter of the string data
// append it on the result
//according to algorithm first charachter stays the same
result.Append(data.Substring(0, 1));
RemoveUnwantedChar(data);
for (int i = 1; i < data.Length; i++)
{
// nb2d al algorithm first take the second characheter in data
//n7wlo la lower
currentLetter = data.Substring(i, 1).ToLower();
currentCode = "";
// No string for zero because we will remove it
if (first.IndexOf(currentLetter) > -1)
//search for bfpv in the current letter return number
// -1 is out of string index
currentCode = "1";
else if (fifth.IndexOf(currentLetter) > -1)
//search for bfpv in the current letter return number
// -1 is out of string index
currentCode = "1";
else if (sixth.IndexOf(currentLetter) > -1)
//search for bfpv in the current letter return number
// -1 is out of string index
currentCode = "2";
else if (seventh.IndexOf(currentLetter) > -1)
//search for bfpv in the current letter return number
// -1 is out of string index
currentCode = "2";
else if (second.IndexOf(currentLetter) > -1)
currentCode = "2";
else if (third.IndexOf(currentLetter) > -1)
currentCode = "3";
else if (currentLetter == "l")
currentCode = "4";
else if (forth.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();
}
public static string RemoveUnwantedChar(string input)
{
return Regex.Replace(input, "aeiouyhw", "");
}
private static string SoundexByWord(string data)
{
var soundexes = new List<string>();
foreach (var str in data.Split(' '))
{
soundexes.Add(Soundex(str));
}
#if Net35OrLower
// string.Join in .Net 3.5 and
//before require the second parameter to be an array.
return string.Join(" ", soundexes.ToArray());
#endif
// string.Join in .Net 4 has an overload
//that takes IEnumerable<string>
return string.Join(" ", soundexes);
}
}
}
Вы пытались реализовать второй алгоритм? – TaW
@TaW Я не знаю, как обновить код ко второму алгоритму Я думаю, что они такие же Шаги, которые я сделал в коде – Bav
Возможно, я просто пропустил его, но где _Save - первая буква. Удалите все вхождения «h» и «w», кроме первой буквы ._ part? – TaW