= ["apple", "banana", "orange", "grape", "pineapple", "kiwi"]
word_list = "appl" query
Exercise: Fuzzy matching
Task: Write a function that helps finding the most similar words or tokens from a given list based on a user query using rapidfuzz
.
Instructions:
- Write a Python function called
find_similar_words(query, word_list)
that takes a user query and a list of words or tokens as input. - Inside the function, use rapidfuzz to calculate the similarity between the query and each word/token in the list.
- Return a list of tuples containing the word/token and its corresponding similarity score, sorted in descending order of similarity.
Show solution
# Importing necessary packages
from rapidfuzz import fuzz
def find_similar_words(query, word_list):
# Create an empty list to store word and similarity score tuples
= []
similar_words
# Loop through each word in the word_list
for word in word_list:
# Calculate the similarity score between the query and the current word
= fuzz.ratio(query, word)
similarity
# Append the word and its similarity score to the list
similar_words.append((word, similarity))
# Sort the list of tuples based on similarity score in descending order
=lambda x: x[1], reverse=True)
similar_words.sort(key
# Return the sorted list of similar words
return similar_words
= find_similar_words(query, word_list)
similar_words print("Similar words to '{}' are:".format(query))
for word, similarity in similar_words:
print("{} (Similarity: {})".format(word, round(similarity, 2)))
Similar words to 'appl' are:
apple (Similarity: 88.89)
pineapple (Similarity: 61.54)
grape (Similarity: 44.44)
banana (Similarity: 20.0)
orange (Similarity: 20.0)
kiwi (Similarity: 0.0)