1
0
Fork 0
mirror of https://github.com/qurator-spk/dinglehopper.git synced 2025-07-09 02:19:58 +02:00
dinglehopper/src/dinglehopper/edit_distance.py

50 lines
1.6 KiB
Python
Raw Normal View History

import unicodedata
from typing import List
from multimethod import multimethod
from rapidfuzz.distance import Levenshtein
2024-01-03 22:40:43 +03:30
from typing import List
2023-08-03 19:21:21 +02:00
from uniseg.graphemecluster import grapheme_clusters
2020-10-08 13:33:19 +02:00
from .extracted_text import ExtractedText
@multimethod
def distance(seq1: List[str], seq2: List[str]):
"""Compute the Levenshtein edit distance between two lists of grapheme clusters.
This assumes that the grapheme clusters are already normalized.
Use distance(str, str) instead if you need to compare two Unicode strings.
"""
return Levenshtein.distance(seq1, seq2)
2022-08-29 01:50:19 +02:00
@multimethod
def distance(s1: str, s2: str):
"""Compute the Levenshtein edit distance between two Unicode strings
Note that this is different from levenshtein() as this function knows about Unicode
normalization and grapheme clusters. This should be the correct way to compare two
Unicode strings.
"""
seq1 = list(grapheme_clusters(unicodedata.normalize("NFC", s1)))
seq2 = list(grapheme_clusters(unicodedata.normalize("NFC", s2)))
return Levenshtein.distance(seq1, seq2)
@multimethod
def distance(s1: ExtractedText, s2: ExtractedText):
return Levenshtein.distance(s1.grapheme_clusters, s2.grapheme_clusters)
def editops(word1, word2):
2020-06-12 17:01:28 +02:00
"""
Return sequence of edit operations transforming one string to another.
Note that this returns indices to the _grapheme clusters_, not characters!
"""
word1 = list(grapheme_clusters(unicodedata.normalize("NFC", word1)))
word2 = list(grapheme_clusters(unicodedata.normalize("NFC", word2)))
return Levenshtein.editops(word1, word2).as_list()