Skip to content

Replace editdistance by rapidfuzz for Levenshtein distance #434

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ amazon-textract-caller>=0.2.4,<1
Pillow
tabulate>=0.9,<0.10
XlsxWriter>=3.0,<4
editdistance>=0.6.2,<0.9
rapidfuzz>=3.9.6
22 changes: 2 additions & 20 deletions textractor/utils/search_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
# The latter has numpy as dependency.
pass

import math
import editdistance
from rapidfuzz.distance import Levenshtein
from textractor.data.constants import SimilarityMetric
from textractor.exceptions import MissingDependencyException

Expand Down Expand Up @@ -59,7 +58,7 @@ def get_word_similarity(
cls.util = util

if similarity_metric == SimilarityMetric.LEVENSHTEIN:
return normalized_edit_distance(word_1.lower(), word_2.lower())
return Levenshtein.normalized_similarity(word_1.lower(), word_2.lower())
elif similarity_metric == SimilarityMetric.EUCLIDEAN:
ref_word_emb = cls.model.encode([word_1])
word_emb = cls.model.encode([word_2])
Expand Down Expand Up @@ -110,20 +109,3 @@ def get_metadata_attr_name(cell_atr):
return cell_map[cell_atr]
except:
return ""


def normalized_edit_distance(s1: str, s2: str):
"""
Returns the normalized edit distance

:param s1: First string
:type s1: str
:param s2: Second string
:type s2: str
"""

dist = editdistance.eval(s1, s2)
max_length = max(len(s1), len(s2))
if max_length - dist == 0:
return 0.0
return (max_length - dist) / max_length