Soundex c#正在尝试获取编码的第一个数字
如您所见,我在main方法中将我的值设置为“SMITH”和“SMYTHE”。该值的输出应为25030,但出于某种原因,其编码为250300。我认为这是因为它在单词的第一个字符之前进行编码。e、 史密斯是“S”,所以这是编码为“S”的第一个字符。我如何使它成为一个数字或值Soundex c#正在尝试获取编码的第一个数字,c#,.net,visual-studio,C#,.net,Visual Studio,如您所见,我在main方法中将我的值设置为“SMITH”和“SMYTHE”。该值的输出应为25030,但出于某种原因,其编码为250300。我认为这是因为它在单词的第一个字符之前进行编码。e、 史密斯是“S”,所以这是编码为“S”的第一个字符。我如何使它成为一个数字或值 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks
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;
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
名称空间SoundDexFinal
{
班级计划
{
静态void Main(字符串[]参数)
{
字符串值1=“SMITH”;
字符串值2=“Smythe”;
soundex soundex=新的soundex();
Console.WriteLine(soundex.GetSoundex(value1));//输出“S50300”
Console.WriteLine(soundex.GetSoundex(value2));//输出“S530”
Console.WriteLine(soundex.Compare(value1,value2));//输出“4”
Console.ReadLine();
}
}
名称空间SoundDexFinal
{
soundex级
{
公共字符串GetSoundex(字符串值)
{
value=value.ToUpper();
StringBuilder soundex=新的StringBuilder();
foreach(字符值)
{
if(字符(ch))
AddCharacter(soundex,ch);
}
移除占位符(soundex);
固定长度(soundex);
返回soundex.ToString();
}
私有void AddCharacter(StringBuilder soundex,char ch)
{
如果(soundex.Length==0)
soundex.Append(ch);
其他的
{
字符串代码=GetSoundexDigit(ch);
if(code!=soundex[soundex.Length-1].ToString())
soundex.Append(代码);
}
}
私有字符串GetSoundexDigit(字符ch)
{
字符串chString=ch.ToString();
if(“AEIOUHWY”.Contains(chString))
返回“0”;
else if(“BFPV.”包含(chString))
返回“1”;
else if(“CGJKQSXZ”.Contains(chString))
返回“2”;
else如果(“DT.”包含(chString))
返回“3”;
else if(ch='L')
返回“4”;
如果(“MN”。包含(chString))
返回“5”;
否则,如果(“R”。包含(chString))
返回“6”;
其他的
“返回”;
}
专用void removePlaceholder(StringBuilder soundex)
{
soundex.替换(“.”,“);
}
专用空隙固定长度(StringBuilder soundex)
{
int length=声音长度;
如果(长度<6)
Append(新字符串('0',6-长度));
其他的
soundex.长度=6;
}
公共整数比较(字符串值1、字符串值2)
{
int匹配=0;
字符串soundex1=GetSoundex(值1);
字符串soundex2=GetSoundex(value2);
对于(int i=0;i<6;i++)
如果(soundex1[i]==soundex2[i])匹配++;
返回比赛;
}
}
}
}
}
您正在调用FixLength
函数,如果字符串长度小于6,该函数会在字符串末尾追加额外的“0”
这就是为什么您要使用“250300”而不是“25030”调用
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”,因为它不再是了。根据讨论,像这样更改
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”,因为它不再是了。那是什么soundex变体?通常删除{AEIOUHWY}而不是赋值
0
,并保留第一个字母。史密斯将是“S530”,他们都为我输出S50300:。我已经构建了一个soundex函数,你可以找到它是否有用。我已经彻底检查过了,我确信它应该是25030或2530,因为我不希望它检查名称的第一个字符,但我希望它得到值。有什么想法吗?您不想检查第一个字符的值,但想要值是什么意思?听起来您应该更改AddCharacter
方法,使其仅包含else
分支中的代码。但我不认为你真的可以称之为“soundex:)那是什么soundex变体?通常删除{AEIOUHWY}而不是赋值0
,并保留第一个字母。史密斯将是“S530”,他们都为我输出S50300:。我已经构建了一个soundex函数,你可以找到它是否有用。我已经彻底检查过了,我确信它应该是25030或2530,因为我不希望它检查名称的第一个字符,但我希望它得到值。有什么想法吗?您不想检查第一个字符的值,但想要值是什么意思?听起来您应该更改AddCharacter
方法,使其仅包含else
分支中的代码。但我认为你再也不能称之为“soundex了:)