【问题标题】:Anyone have method to find best match of string in list of strings in C#?任何人都有方法在 C# 中的字符串列表中找到字符串的最佳匹配?
【发布时间】:2021-10-22 22:35:32
【问题描述】:

我正在尝试实现某种最简单的拼写检查器来找出并纠正简单的拼写错误。我正在寻找一种方法,如果可能的话,可以帮助我在 C# 中找到我的字符串 最近的字符串

例如

string source = "gnail"; // simple typo: 'n' instead of 'm'

List<string> KnownWords = new List<string> {
  "orange", 
  "gmail",    // <- the best fit for source: just one (edit) letter away
  "hotmail",  // <- drop two letters, edit one leeter
  "live", 
  "outlook"
};

预期结果:

"gmail"

输出应该是"gmail",因为"gmail" 距离source ("gnail") 仅一个字母(一个编辑距离)

提前致谢!

【问题讨论】:

  • 你有没有尝试过?如果输入字符串是“nail”,预期输出是 hotmail 还是 gmail?
  • 您可以计算从sL中所有其他单词的编辑距离,并将具有最短距离的单词作为回答
  • 这能回答你的问题吗? Detect differences between two strings
  • @PrasadTelkikar 你是对的,但在这种情况下,他应该计算字符数以找出“gmail”是最近的!

标签: java c#


【解决方案1】:
string source = "gnail"; // simple typo: 'n' instead of 'm'

List<string> KnownWords = new List<string> {
  "orange", 
  "gmail",    // <- the best fit for source: just one (edit) letter away
  "hotmail",  // <- drop two letters, edit one leeter
  "live", 
  "outlook"
};

private static string spellChecker(string toCheck)
        {
            List<string> updatedList = new List<string>();

            try
            {
                updatedList = KnownWords.FindAll(x => x.Length == toCheck.Length);
            }
            catch (Exception e)
            {
                Console.Error.WriteLine($"Could not find any match : {e.Message}");
                return "No Match Found";
            }

            int[] matches = new int[updatedList.Count];
            int matchedElements = 0, index = 0;

            foreach (string str in updatedList)
            {
                matchedElements = 0;
                for (int i = 0; i < toCheck.Length; i++)
                {
                    if(str[i] == toCheck[i])
                    {
                        matchedElements++;
                    }
                }

                matches[index] = matchedElements;
                index++;
            }

            string toReturn = string.Empty;

            try
            {
                toReturn = updatedList[matches.ToList().IndexOf(matches.Max())];
            }
            catch (InvalidOperationException ex)
            {
                Console.Error.WriteLine($"No Elements matches the length of given words in the list : {ex.Message}");
            }

            return toReturn == string.Empty ? "Error Output" : toReturn;
        }

这可能太长了,但当给定单词与已知单词列表中的任何字符串长度相同时,这种方法效果最好。

【讨论】:

  • 非常感谢,我会试试这个:)
猜你喜欢
  • 2015-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-03
  • 2012-06-05
  • 2016-09-12
  • 2020-10-05
  • 1970-01-01
相关资源
最近更新 更多