From 1c44357bade51530c15e546eb593fa52e9166965 Mon Sep 17 00:00:00 2001 From: klemek Date: Mon, 27 Apr 2020 16:27:07 +0200 Subject: [PATCH] fixed nearest word giving weird results --- meme_otron/utils.py | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/meme_otron/utils.py b/meme_otron/utils.py index da522b5..f956d8e 100644 --- a/meme_otron/utils.py +++ b/meme_otron/utils.py @@ -79,10 +79,16 @@ def parse_arguments(src: str) -> List[str]: def find_nearest(word: str, wlist: List[str], threshold: int = 5) -> Optional[str]: - found = min([(distance(word, w) - abs(len(w) - len(word)), w) for w in wlist], key=lambda v: v[0]) - if found[0] > threshold: + distances = [ + (distance(word, w), # distance + abs(len(w) - len(word)), # length diff + w) + for w in wlist] + distances.sort(key=lambda v: v[1]) # sort by length diff to get the closest (in length) first + found = min(distances, key=lambda v: v[0] - v[1]) # get the closest in lev. distance + if found[0] - found[1] > threshold: # distance is too much return None - return found[1] + return found[2] def justify_text(src: str, n_lines: int) -> Optional[str]: