Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Optimized fuzz.py #136

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 7 additions & 20 deletions fuzzywuzzy/fuzz.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,7 @@ def _token_set(s1, s2, partial=True, force_ascii=True, full_process=True):
p1 = utils.full_process(s1, force_ascii=force_ascii) if full_process else s1
p2 = utils.full_process(s2, force_ascii=force_ascii) if full_process else s2

if not utils.validate_string(p1):
return 0
if not utils.validate_string(p2):
if not (utils.validate_string(p1) and utils.validate_string(p2)):
return 0

# pull tokens
Expand All @@ -159,17 +157,10 @@ def _token_set(s1, s2, partial=True, force_ascii=True, full_process=True):
diff1to2 = tokens1.difference(tokens2)
diff2to1 = tokens2.difference(tokens1)

sorted_sect = " ".join(sorted(intersection))
sorted_1to2 = " ".join(sorted(diff1to2))
sorted_2to1 = " ".join(sorted(diff2to1))

combined_1to2 = sorted_sect + " " + sorted_1to2
combined_2to1 = sorted_sect + " " + sorted_2to1

# strip
sorted_sect = sorted_sect.strip()
combined_1to2 = combined_1to2.strip()
combined_2to1 = combined_2to1.strip()
# sort, join, and strip
sorted_sect = (" ".join(sorted(intersection))).strip()
combined_1to2 = (" ".join([sorted_sect, " ".join(sorted(diff1to2))])).strip()
combined_2to1 = " ".join([sorted_sect, " ".join(sorted(diff2to1))]).strip()
Comment on lines +160 to +163

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is pretty much the same, but not completely. In this changed code whitespace from sorted_sect is stripped before the strings are combined, while it was done afterwards in the original code. This does not matter in this case, since the original strip for sorted_sect was not required, since " ".join(sorted(intersection)) can never have whitespaces in begin or end. From a performance standpoint this does not really matter, but is probably slightly slower, since it creates some intermediate lists and calls the join function more often.


if partial:
ratio_func = partial_ratio
Expand Down Expand Up @@ -202,9 +193,7 @@ def QRatio(s1, s2, force_ascii=True):
p1 = utils.full_process(s1, force_ascii=force_ascii)
p2 = utils.full_process(s2, force_ascii=force_ascii)

if not utils.validate_string(p1):
return 0
if not utils.validate_string(p2):
if not (utils.validate_string(p1) and utils.validate_string(p2)):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again no performance implication. Simply a preference, whether these two if statements should be joined

return 0

return ratio(p1, p2)
Expand All @@ -223,9 +212,7 @@ def WRatio(s1, s2, force_ascii=True):
p1 = utils.full_process(s1, force_ascii=force_ascii)
p2 = utils.full_process(s2, force_ascii=force_ascii)

if not utils.validate_string(p1):
return 0
if not utils.validate_string(p2):
if not (utils.validate_string(p1) and utils.validate_string(p2)):
return 0

# should we look at partials?
Expand Down