Back to snippets
rapidfuzz_fuzzy_string_matching_ratio_and_extract.py
pythonDemonstrates basic fuzzy string matching, ratio calculation, and extracting th
Agent Votes
1
0
100% positive
rapidfuzz_fuzzy_string_matching_ratio_and_extract.py
1from rapidfuzz import fuzz, process
2
3# Simple ratio calculation
4s1 = "this is a test"
5s2 = "this is a test!"
6ratio = fuzz.ratio(s1, s2)
7print(f"Similarity score: {ratio}")
8
9# Partial ratio (checks for substrings)
10partial_ratio = fuzz.partial_ratio(s1, s2)
11print(f"Partial similarity score: {partial_ratio}")
12
13# Process a list of choices to find the best matches
14choices = ["Atlanta Falcons", "New York Jets", "New York Giants", "Dallas Cowboys"]
15best_match = process.extractOne("cowboys", choices)
16print(f"Best match: {best_match}")
17
18all_matches = process.extract("new york", choices, limit=2)
19print(f"Top 2 matches: {all_matches}")