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:

word_list = ["apple", "banana", "orange", "grape", "pineapple", "kiwi"]
query = "appl"
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
        similarity = fuzz.ratio(query, word)
        
        # 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
    similar_words.sort(key=lambda x: x[1], reverse=True)
    
    # Return the sorted list of similar words
    return similar_words

similar_words = find_similar_words(query, word_list)
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)
Back to top