Skip to content

Commit

Permalink
Merge pull request #106 from mammothb/feat-change_distance_comparer
Browse files Browse the repository at this point in the history
Feat: change distance comparer
  • Loading branch information
mammothb authored Dec 2, 2021
2 parents 02ccab4 + c63caef commit 9e1273f
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 10 deletions.
2 changes: 1 addition & 1 deletion symspellpy/editdistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def __init__(self, algorithm: DistanceAlgorithm) -> None:
elif algorithm == DistanceAlgorithm.DAMERAU_OSA_FAST:
self._distance_comparer = DamerauOsaFast()
else:
raise ValueError("Unknown distance algorithm")
raise ValueError("unknown distance algorithm")

def compare(self, string_1: str, string_2: str, max_distance: int) -> int:
"""Compares a string to the base string to determine the edit distance,
Expand Down
6 changes: 3 additions & 3 deletions symspellpy/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ def case_transfer_matching(cased_text: str, uncased_text: str) -> str:
"""
if len(cased_text) != len(uncased_text):
raise ValueError(
"'cased_text' and 'uncased_text' don't have the same length. Use "
"case_transfer_similar() instead."
"'cased_text' and 'uncased_text' don't have the same length, use "
"case_transfer_similar() instead"
)

return "".join(
Expand Down Expand Up @@ -84,7 +84,7 @@ def case_transfer_similar(cased_text: str, uncased_text: str) -> str:
return uncased_text

if not cased_text:
raise ValueError("'cased_text' cannot be empty!")
raise ValueError("'cased_text' cannot be empty")

matcher = SequenceMatcher(a=cased_text.lower(), b=uncased_text)
result = ""
Expand Down
15 changes: 14 additions & 1 deletion symspellpy/symspellpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,19 @@ def deletes(self) -> Dict[str, List[str]]:
"""
return self._deletes

@property
def distance_algorithm(self) -> DistanceAlgorithm:
"""The current distance algorithm."""
return self._distance_algorithm

@distance_algorithm.setter
def distance_algorithm(self, value: DistanceAlgorithm) -> None:
if not isinstance(value, DistanceAlgorithm):
raise TypeError(
"can only assign DistanceAlgorithm type values to distance_algorithm"
)
self._distance_algorithm = value

@property
def entry_count(self) -> int:
"""Number of unique correct spelling words."""
Expand Down Expand Up @@ -365,7 +378,7 @@ def lookup(
if max_edit_distance is None:
max_edit_distance = self._max_dictionary_edit_distance
if max_edit_distance > self._max_dictionary_edit_distance:
raise ValueError("Distance too large")
raise ValueError("distance too large")
suggestions: List[SuggestItem] = []
phrase_len = len(phrase)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_editdistance.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ class TestEditDistance:
def test_unknown_distance_algorithm(self):
with pytest.raises(ValueError) as excinfo:
_ = EditDistance(2)
assert "Unknown distance algorithm" == str(excinfo.value)
assert "unknown distance algorithm" == str(excinfo.value)

def test_abstract_distance_comparer(self):
with pytest.raises(TypeError) as excinfo:
Expand Down
6 changes: 3 additions & 3 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def test_case_transfer_matching_diff_lengths(self):
with pytest.raises(ValueError) as excinfo:
case_transfer_matching("abc", "abcd")
assert (
"'cased_text' and 'uncased_text' don't have the same length. Use "
"case_transfer_similar() instead."
"'cased_text' and 'uncased_text' don't have the same length, use "
"case_transfer_similar() instead"
) == str(excinfo.value)

def test_case_transfer_matching(self):
Expand All @@ -78,7 +78,7 @@ def test_case_transfer_similar_empty_wo_casing(self):
def test_case_transfer_similar_empty_w_casing(self):
with pytest.raises(ValueError) as excinfo:
case_transfer_similar("", "abcd")
assert "'cased_text' cannot be empty!" == str(excinfo.value)
assert "'cased_text' cannot be empty" == str(excinfo.value)

def test_case_transfer_similar(self, get_similar_texts):
for cased_text, uncased_text, expected in get_similar_texts:
Expand Down
22 changes: 22 additions & 0 deletions tests/test_symspellpy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import pytest

from symspellpy import SymSpell, Verbosity
from symspellpy.editdistance import DistanceAlgorithm
from symspellpy.helpers import DictIO

FORTESTS_DIR = Path(__file__).resolve().parent / "fortests"
Expand Down Expand Up @@ -61,6 +62,27 @@ def test_negative_count_threshold(self):
_ = SymSpell(1, 3, -1)
assert "count_threshold cannot be negative" == str(excinfo.value)

@pytest.mark.parametrize(
"algorithm",
[
DistanceAlgorithm.LEVENSHTEIN,
DistanceAlgorithm.DAMERAU_OSA,
DistanceAlgorithm.LEVENSHTEIN_FAST,
DistanceAlgorithm.DAMERAU_OSA_FAST,
],
)
def test_set_distance_algorithm(self, symspell_default, algorithm):
symspell_default.distance_algorithm = algorithm
assert algorithm == symspell_default.distance_algorithm

def test_set_invalid_distance_algorithm(self, symspell_default):
with pytest.raises(TypeError) as excinfo:
symspell_default.distance_algorithm = 1
assert (
"can only assign DistanceAlgorithm type values to distance_algorithm"
== str(excinfo.value)
)

@pytest.mark.parametrize("symspell_short", [None, 0], indirect=True)
def test_create_dictionary_entry_negative_count(self, symspell_short):
assert (
Expand Down
2 changes: 1 addition & 1 deletion tests/test_symspellpy_lookup.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def test_should_not_return_low_count_word_that_are_also_delete_word(
def test_max_edit_distance_too_large(self, symspell_high_thres_flame):
with pytest.raises(ValueError) as excinfo:
_ = symspell_high_thres_flame.lookup("flam", Verbosity.TOP, 3)
assert "Distance too large" == str(excinfo.value)
assert "distance too large" == str(excinfo.value)

def test_include_unknown(self, symspell_high_thres_flame):
result = symspell_high_thres_flame.lookup("flam", Verbosity.TOP, 0, True)
Expand Down

0 comments on commit 9e1273f

Please sign in to comment.