commit 2774374e8315ddbbe530f724c758096833bfc274
parent 298d4e44ee0695331e79bc72b195abd287d128bc
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Tue, 30 May 2023 20:25:51 -0700
auto fix typos in tags using levenshtein
Diffstat:
1 file changed, 27 insertions(+), 1 deletion(-)
diff --git a/pkg/web/handlers/api/v1/topBarHandler.go b/pkg/web/handlers/api/v1/topBarHandler.go
@@ -7,6 +7,8 @@ import (
"dkforest/pkg/database"
dutils "dkforest/pkg/database/utils"
"dkforest/pkg/hashset"
+ "dkforest/pkg/levenshtein"
+ "dkforest/pkg/managers"
"dkforest/pkg/utils"
"errors"
"fmt"
@@ -15,6 +17,7 @@ import (
"github.com/labstack/echo"
"github.com/microcosm-cc/bluemonday"
html2 "html"
+ "math"
"net/http"
"net/url"
"regexp"
@@ -609,9 +612,32 @@ func colorifyTaggedUsers(html string, getUsersByUsername getUsersByUsernameFn) (
if len(usernameMatches) > 0 {
html = tagRgx.ReplaceAllStringFunc(html, func(s string) string {
- if user, ok := taggedUsersMap[strings.ToLower(s)]; ok {
+ lowerS := strings.ToLower(s)
+ if user, ok := taggedUsersMap[lowerS]; ok {
return fmt.Sprintf("<span %s>@%s</span>", user.GenerateChatStyle1(), user.Username)
}
+
+ // Not found, try to fix typos using levenshtein
+ activeUsers := managers.ActiveUsers.GetActiveUsers()
+ if len(activeUsers) > 0 {
+ minDist := math.MaxInt
+ minAu := activeUsers[0]
+ for _, au := range activeUsers {
+ lowerAu := strings.ToLower(string(au.Username))
+ d := levenshtein.ComputeDistance(lowerS, lowerAu)
+ if d < minDist {
+ minDist = d
+ minAu = au
+ }
+ }
+ if minDist <= 3 {
+ if users, _ := getUsersByUsername([]string{minAu.Username.String()}); len(users) > 0 {
+ user := users[0]
+ return fmt.Sprintf("<span %s>@%s</span>", user.GenerateChatStyle1(), user.Username)
+ }
+ }
+ }
+
return s
})
}