2

我确定我在 Meta 或 SO 上看到了一篇帖子,其中 Jeff 发布了更好地匹配评论的升级算法,例如当有人输入评论时:

@Tom did you see

它将匹配用户名“Tom”。如果有特殊字符,请说我的用户名是“T0m”并且有人键入@Tom它仍然匹配。

如果它确实存在,有人有这个帖子的链接吗?如果我没记错的话,那是他分享的代码,对我很有用!

否则,给出参与讨论的用户名列表:

users[0] = "Tom"
users[1] = "Peanut"
users[2] = "Ashley"
users[3] = "Jon"
users[4] = "AARÓN"

并且您被给出@Aaron@Aron作为输入,选择列表中提到的正确用户的最佳方法是什么?

一个通用的算法会很好,但我正在做的网站是 ASP.net c#,所以如果有一个使用该语言的示例,那就太好了。这是我到目前为止所拥有的,它非常适合精确匹配(全小写):

// Find comment references
if (Search.Type != Alerts.SectionType.error)
{
    // A list of all lower case usernames refered to in this thread
    string[] References = Alerts.CommonFunctions.extractReferences(Comment);

    // Only proceed if any references are found
    if (References.Count() > 0)
    {
        // Extract all usernames involved in this comment discussion
        UserBasic[] UsernamesInThread = getAllUsernamesInThread(Anchor);

        // Loop each reference
        foreach (string r in References)
        {
            // Try to find a match
            foreach (UserBasic u in UsernamesInThread)
            {
                // Exact match found
                if (r == u.Username)
                {
                    // Check it's not original author (we can then ignore as alert already issued)
                    if (u.UserID != Search.OriginalAuthorID)
                    {
                        Alerts.CommonFunctions.createAlert(u.UserID, Settings.CommentReplyAlertID, Search.URL, Search.Title);
                    }
                    break;
                }
            }
        }
    }
}
4

1 回答 1

1

Peter Norvig 在这里写了一个非常小的拼写校正器的漂亮示例:

它列出了两个 C# 实现。

对于您的特定问题(候选集非常小),您可能希望找到目标词和所有候选词之间的最小编辑距离。

于 2011-09-18T22:02:57.197 回答