Kalkwst / MicroLib

MIT License
3 stars 0 forks source link

:sparkles: Text: Add Fuzzy Score Algorithm #4

Closed Kalkwst closed 1 year ago

Kalkwst commented 1 year ago

A fuzzy score is a measure of how similar two strings are. It is calculated using a set of rules or algorithms that take into account various factors such as the length of the strings, the number of common characters, and the position of the characters in the strings. A fuzzy score is typically a value between 0 and 1, with a value of 0 indicating that the strings are completely dissimilar and a value of 1 indicating that the strings are identical.

For example, consider a user who is searching for the string "cat" in a database of animal names. If the database contains the strings "cat", "cats", "catnip", and "catacombs", a fuzzy scoring algorithm might be used to calculate the similarity between the search query and each of the strings in the database. The resulting fuzzy scores could be used to rank the strings in order of similarity, with the string "cat" receiving the highest score and the string "catacombs" receiving the lowest score.

The algorithm calculates the fuzzy score between two strings, term and query. It converts both strings to lowercase and then iterates through each character in the query string. For each character in the query string, it scans the term string looking for a match and increments the score by 1 if a match is found. If the position of the matching character in the term string is immediately after the position of the previously matched character, the score is also incremented by 2. The method returns the final score as the fuzzy score between the two strings.