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

Remove rapidfuzz dependency #442

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions news/442.deps.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Removed `rapidfuzz` dependency
107 changes: 3 additions & 104 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ classifiers = [

[tool.poetry.dependencies]
python = "^3.8"
rapidfuzz = "^3.0.0"

[tool.poetry.group.dev.dependencies]
mypy = "^1.5"
Expand Down
15 changes: 6 additions & 9 deletions src/cleo/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import math

from dataclasses import dataclass
from difflib import SequenceMatcher
from html.parser import HTMLParser

from rapidfuzz.distance import Levenshtein


class TagStripper(HTMLParser):
def __init__(self) -> None:
Expand Down Expand Up @@ -51,12 +50,12 @@ def find_similar_names(name: str, names: list[str]) -> list[str]:
"""
Finds names similar to a given command name.
"""
threshold = 1e3
threshold = 0.4
Secrus marked this conversation as resolved.
Show resolved Hide resolved
distance_by_name = {}

if " " in name:
names = [name for name in names if " " in name]
Secrus marked this conversation as resolved.
Show resolved Hide resolved
for actual_name in names:
# Get Levenshtein distance between the input and each command name
distance = Levenshtein.distance(name, actual_name)
distance = SequenceMatcher(None, actual_name, name).ratio()

is_similar = distance <= len(name) / 3
substring_index = actual_name.find(name)
Expand All @@ -70,9 +69,7 @@ def find_similar_names(name: str, names: list[str]) -> list[str]:

# Only keep results with a distance below the threshold
distance_by_name = {
key: value
for key, value in distance_by_name.items()
if value[0] < 2 * threshold
key: value for key, value in distance_by_name.items() if value[0] > threshold
Secrus marked this conversation as resolved.
Show resolved Hide resolved
}
# Display results with shortest distance first
return sorted(distance_by_name, key=lambda key: distance_by_name[key])
Secrus marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 1 addition & 1 deletion tests/test_application.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ def test_find_ambiguous_namespace(app: Application) -> None:
CleoNamespaceNotFoundError,
match=(
r'There are no commands in the "f" namespace\.\n\n'
r"Did you mean one of these\?\n foo\n foo1"
r"Did you mean this\?\n foo"
),
):
app.find_namespace("f")
Expand Down
10 changes: 4 additions & 6 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,13 @@ def test_format_time(input_secs: float, expected: str) -> None:
@pytest.mark.parametrize(
["name", "expected"],
[
("", ["help", "foo1", "foo2", "bar1", "bar2", "foo bar1", "foo bar2"]),
("hellp", ["help"]),
("bar2", ["bar2", "bar1", "foo bar2"]),
("bar1", ["bar1", "bar2", "foo bar1"]),
("foo", ["foo1", "foo2", "foo bar1", "foo bar2"]),
("env add", ["env remove", "env activate", "env info", "env list", "env use"]),
("evn add", ["env activate", "env use"]),
("env", ["env remove", "env info", "env list", "env use"]),
],
)
def test_find_similar_names(name: str, expected: list[str]) -> None:
names = ["help", "foo1", "foo2", "bar1", "bar2", "foo bar1", "foo bar2"]
names = ["env info", "env use", "env activate", "env remove", "env list"]
assert find_similar_names(name, names) == expected


Expand Down
Loading