Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reduce len() calls #412

Merged
merged 7 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
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
9 changes: 5 additions & 4 deletions src/rapidfuzz/fuzz_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,17 +316,18 @@ def partial_ratio_alignment(

if not s1 and not s2:
return ScoreAlignment(100.0, 0, 0, 0, 0)

s1, s2 = conv_sequences(s1, s2)
if len(s1) <= len(s2):
len1 = len(s1)
len2 = len(s2)
if len1 <= len2:
shorter = s1
longer = s2
else:
shorter = s2
longer = s1

res = _partial_ratio_impl(shorter, longer, score_cutoff / 100)
if res.score != 100 and len(s1) == len(s2):
if res.score != 100 and len1 == len2:
score_cutoff = max(score_cutoff, res.score)
res2 = _partial_ratio_impl(longer, shorter, score_cutoff / 100)
if res2.score > res.score:
Expand All @@ -335,7 +336,7 @@ def partial_ratio_alignment(
if res.score < score_cutoff:
return None

if len(s1) <= len(s2):
if len1 <= len2:
maxbachmann marked this conversation as resolved.
Show resolved Hide resolved
return res

return ScoreAlignment(res.score, res.dest_start, res.dest_end, res.src_start, res.src_end)
Expand Down
12 changes: 7 additions & 5 deletions src/rapidfuzz/process_cpp_impl.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -1208,6 +1208,7 @@ def extract(query, choices, *, scorer=WRatio, processor=None, limit=5, score_cut
cdef RF_Scorer* scorer_context = NULL
cdef RF_ScorerFlags scorer_flags
cdef int64_t c_limit
cdef int64_t choices_len
scorer_kwargs = scorer_kwargs.copy() if scorer_kwargs else {}

setupPandas()
Expand All @@ -1216,16 +1217,17 @@ def extract(query, choices, *, scorer=WRatio, processor=None, limit=5, score_cut
return []

try:
if limit is None or limit > len(choices):
limit = len(choices)
choices_len = <int64_t>len(choices)
except TypeError:
# handle generators. In Theory we could retrieve the length later on while
# preprocessing the choices, but this is good enough for now
choices = list(choices)
if limit is None or limit > len(choices):
limit = len(choices)
choices_len = <int64_t>len(choices)

c_limit = choices_len
if limit is not None:
c_limit = min(c_limit, <int64_t>limit)

c_limit = limit
if c_limit == 1:
res = extractOne(
query,
Expand Down
7 changes: 5 additions & 2 deletions src/rapidfuzz/process_py.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,14 +643,17 @@ def cpdist(
"""
import numpy as np

if len(queries) != len(choices):
len_queries = len(queries)
len_choices = len(choices)

if len_queries != len_choices:
error_message = "Length of queries and choices must be the same!"
raise ValueError(error_message)

_ = workers, score_hint
scorer_kwargs = scorer_kwargs or {}
dtype = _dtype_to_type_num(dtype, scorer, scorer_kwargs)
results = np.zeros((len(queries),), dtype=dtype)
results = np.zeros((len_queries,), dtype=dtype)
maxbachmann marked this conversation as resolved.
Show resolved Hide resolved

setupPandas()

Expand Down