我确定我在 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;
}
}
}
}
}