-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
312 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from typing import Any, Callable, Dict, Literal, Optional | ||
|
||
from typing_extensions import TypedDict | ||
|
||
METRICS = Literal[ | ||
"damerau_levenshtein", | ||
"levenshtein", | ||
"jaro", | ||
"jaro_winkler", | ||
"hamming", | ||
"indel", | ||
] | ||
|
||
|
||
class EditDistanceConfig(TypedDict, total=False): | ||
metric: METRICS | ||
normalize_score: bool | ||
|
||
|
||
class EditDistance: | ||
def __init__( | ||
self, | ||
config: Optional[EditDistanceConfig] = None, | ||
): | ||
config = config or {} | ||
metric = config.get("metric") or "damerau_levenshtein" | ||
self.metric = self._get_metric(metric, config.get("normalize_score", True)) | ||
|
||
def evaluate( | ||
self, | ||
prediction: str, | ||
reference: Optional[str] = None, | ||
) -> float: | ||
return self.metric(prediction, reference) | ||
|
||
@staticmethod | ||
def _get_metric(distance: str, normalize_score: bool = False) -> Callable: | ||
try: | ||
from rapidfuzz import distance as rf_distance | ||
except ImportError: | ||
raise ImportError( | ||
"This operation requires the rapidfuzz library to use." | ||
"Please install it with `pip install -U rapidfuzz`." | ||
) | ||
|
||
module_map: Dict[str, Any] = { | ||
"damerau_levenshtein": rf_distance.DamerauLevenshtein, | ||
"levenshtein": rf_distance.Levenshtein, | ||
"jaro": rf_distance.Jaro, | ||
"jaro_winkler": rf_distance.JaroWinkler, | ||
"hamming": rf_distance.Hamming, | ||
"indel": rf_distance.Indel, | ||
} | ||
if distance not in module_map: | ||
raise ValueError( | ||
f"Invalid distance metric: {distance}" | ||
f"\nMust be one of: {list(module_map)}" | ||
) | ||
module = module_map[distance] | ||
if normalize_score: | ||
return module.normalized_distance | ||
else: | ||
return module.distance |
Oops, something went wrong.