簡體   English   中英

Soundex C#試圖獲取第一個數字進行編碼

[英]Soundex c# trying to get first digit for encoding

如您所見,我在主方法中將值設置為“ SMITH”和“ SMYTHE”。 此值的輸出應為25030,但由於某種原因,它的編碼為250300。我認為這是因為它是在單詞的第一個字符之前進行編碼的。 例如,SMITH是“ S”,因此這被編碼為“ S”的第一個字符。 如何使S變成數字或值?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SoundDexFinal
{
    class Program
    {
        static void Main(string[] args)
        {

            string value1 = "SMITH";
            string value2 = "Smythe";

            soundex soundex = new soundex();
            Console.WriteLine(soundex.GetSoundex(value1));      // Outputs "S50300"
            Console.WriteLine(soundex.GetSoundex(value2));      // Outputs "S530"
            Console.WriteLine(soundex.Compare(value1, value2)); // Outputs "4"
            Console.ReadLine();
        }
    }

        namespace SoundDexFinal
    {
        class soundex
        {
            public string GetSoundex(string value)
            {
                value = value.ToUpper();
                StringBuilder soundex = new StringBuilder();
                foreach (char ch in value)
                {
                    if (char.IsLetter(ch))
                        AddCharacter(soundex, ch);

                }
                RemovePlaceholders(soundex);
                FixLength(soundex);
                return soundex.ToString();

            }


            private void AddCharacter(StringBuilder soundex, char ch)
            {
                if (soundex.Length == 0)
                    soundex.Append(ch);
                else
                {
                    string code = GetSoundexDigit(ch);
                    if (code != soundex[soundex.Length - 1].ToString())
                        soundex.Append(code);
                }
            }

            private string GetSoundexDigit(char ch)
            {
                string chString = ch.ToString();

                if ("AEIOUHWY".Contains(chString))
                    return "0";
                else if ("BFPV".Contains(chString))
                    return "1";
                else if ("CGJKQSXZ".Contains(chString))
                    return "2";
                else if ("DT".Contains(chString))
                    return "3";
                else if (ch == 'L')
                    return "4";
                else if ("MN".Contains(chString))
                    return "5";
                else if ("R".Contains(chString))
                    return "6";
                else
                    return ".";
            }

            private void RemovePlaceholders(StringBuilder soundex)
            {
                soundex.Replace(".", "");
            }

            private void FixLength(StringBuilder soundex)
            {
                int length = soundex.Length;
                if (length < 6)
                    soundex.Append(new string('0', 6 - length));
                else
                    soundex.Length = 6;
            }

            public int Compare(string value1, string value2)
            {
                int matches = 0;
                string soundex1 = GetSoundex(value1);
                string soundex2 = GetSoundex(value2);

                for (int i = 0; i < 6; i++)
                    if (soundex1[i] == soundex2[i]) matches++;

                return matches;
            }
        }
    }
}
}

您正在調用FixLength函數,如果字符串長度小於6,則該函數在字符串的末尾附加額外的“ 0”。

這就是您獲得“ 250300”而不是“ 25030”的原因

根據討論,像這樣更改AddCharacter方法將實現您所追求的目標:

private void AddCharacter(StringBuilder soundex, char ch)
{
    string code = GetSoundexDigit(ch);
    if (soundex.Length == 0 || code != soundex[soundex.Length - 1].ToString())
        soundex.Append(code);
}

但是我不再指“ soundex”,因為它不再是。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM