From ad316aeabca8da1ad179eaea00c4962f3d64e59a Mon Sep 17 00:00:00 2001 From: Mike Gerber Date: Tue, 9 Jan 2024 15:58:29 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=8D=20mypy:=20Use=20a=20compatible=20s?= =?UTF-8?q?yntax=20for=20multimethod?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/dinglehopper/character_error_rate.py | 10 ++++------ src/dinglehopper/edit_distance.py | 8 ++++---- src/dinglehopper/word_error_rate.py | 18 ++++++++---------- 3 files changed, 16 insertions(+), 20 deletions(-) diff --git a/src/dinglehopper/character_error_rate.py b/src/dinglehopper/character_error_rate.py index 5e2e02c..35d3b07 100644 --- a/src/dinglehopper/character_error_rate.py +++ b/src/dinglehopper/character_error_rate.py @@ -30,17 +30,15 @@ def character_error_rate_n( # XXX Should we really count newlines here? -@multimethod -def character_error_rate_n(reference: str, compared: str) -> Tuple[float, int]: +@character_error_rate_n.register +def _(reference: str, compared: str) -> Tuple[float, int]: seq1 = list(grapheme_clusters(unicodedata.normalize("NFC", reference))) seq2 = list(grapheme_clusters(unicodedata.normalize("NFC", compared))) return character_error_rate_n(seq1, seq2) -@multimethod -def character_error_rate_n( - reference: ExtractedText, compared: ExtractedText -) -> Tuple[float, int]: +@character_error_rate_n.register +def _(reference: ExtractedText, compared: ExtractedText) -> Tuple[float, int]: return character_error_rate_n( reference.grapheme_clusters, compared.grapheme_clusters ) diff --git a/src/dinglehopper/edit_distance.py b/src/dinglehopper/edit_distance.py index 8eec5e2..ac4a847 100644 --- a/src/dinglehopper/edit_distance.py +++ b/src/dinglehopper/edit_distance.py @@ -19,8 +19,8 @@ def distance(seq1: List[str], seq2: List[str]): return Levenshtein.distance(seq1, seq2) -@multimethod -def distance(s1: str, s2: str): +@distance.register +def _(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 @@ -32,8 +32,8 @@ def distance(s1: str, s2: str): return Levenshtein.distance(seq1, seq2) -@multimethod -def distance(s1: ExtractedText, s2: ExtractedText): +@distance.register +def _(s1: ExtractedText, s2: ExtractedText): return Levenshtein.distance(s1.grapheme_clusters, s2.grapheme_clusters) diff --git a/src/dinglehopper/word_error_rate.py b/src/dinglehopper/word_error_rate.py index 2e65760..afb4fe0 100644 --- a/src/dinglehopper/word_error_rate.py +++ b/src/dinglehopper/word_error_rate.py @@ -60,8 +60,8 @@ def words(s: str): yield word -@multimethod -def words(s: ExtractedText): +@words.register +def _(s: ExtractedText): return words(s.text) @@ -70,8 +70,8 @@ def words_normalized(s: str): return words(unicodedata.normalize("NFC", s)) -@multimethod -def words_normalized(s: ExtractedText): +@words_normalized.register +def _(s: ExtractedText): return words_normalized(s.text) @@ -82,15 +82,13 @@ def word_error_rate_n(reference: str, compared: str) -> Tuple[float, int]: return word_error_rate_n(reference_seq, compared_seq) -@multimethod -def word_error_rate_n( - reference: ExtractedText, compared: ExtractedText -) -> Tuple[float, int]: +@word_error_rate_n.register +def _(reference: ExtractedText, compared: ExtractedText) -> Tuple[float, int]: return word_error_rate_n(reference.text, compared.text) -@multimethod -def word_error_rate_n(reference: Iterable, compared: Iterable) -> Tuple[float, int]: +@word_error_rate_n.register +def _(reference: Iterable, compared: Iterable) -> Tuple[float, int]: reference_seq = list(reference) compared_seq = list(compared)