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

improve performance of find_similar_names #173

Merged
merged 1 commit into from
Jul 19, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 3 additions & 8 deletions cleo/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from html.parser import HTMLParser
from typing import Any

from pylev import levenshtein
from rapidfuzz.distance import Levenshtein


class TagStripper(HTMLParser):
Expand Down Expand Up @@ -54,11 +54,10 @@ def find_similar_names(name: str, names: list[str]) -> list[str]:
"""
threshold = 1e3
distance_by_name = {}
suggested_names = []

for actual_name in names:
# Get Levenshtein distance between the input and each command name
distance = levenshtein(name, actual_name)
distance = Levenshtein.distance(name, actual_name)

is_similar = distance <= len(name) / 3
is_sub_string = actual_name.find(name) != -1
Expand All @@ -75,11 +74,7 @@ def find_similar_names(name: str, names: list[str]) -> list[str]:
}

# Display results with shortest distance first
for k, _v in sorted(distance_by_name.items(), key=lambda i: (i[1][0], i[1][1])):
if k not in suggested_names:
suggested_names.append(k)

return suggested_names
return sorted(distance_by_name, key=distance_by_name.get)


_TIME_FORMATS = [
Expand Down