Skip to content

Commit

Permalink
feat: Add utility to calculate string similarity score
Browse files Browse the repository at this point in the history
  • Loading branch information
caffeine-addictt committed Apr 14, 2024
1 parent c24e154 commit 3690c3c
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/utils/strings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,24 @@ func InputPrompt(text string) string {
}
return strings.TrimSpace(s)
}

func SimilarityScore(s1, s2 string) int {
// Calculate how similar two strings are [0-100]
if s1 == s2 {
return 100
}

similar := 0
least := min(len(s1), len(s2))
for i := 0; i < least; i++ {
if s1[i] == s2[i] {
similar++
}
}

if least == 0 {
least = 1
}

return similar * 100 / least
}

0 comments on commit 3690c3c

Please sign in to comment.