not found proposition

This commit is contained in:
klemek
2020-04-25 13:41:01 +02:00
parent 2f5d0f8d30
commit dc4e68ad75
5 changed files with 63 additions and 35 deletions
+8 -4
View File
@@ -120,10 +120,14 @@ async def on_message(message):
left_wmark_text = f"By {message.author.display_name}"
img = meme_otron.compute(*args, left_wmark_text=left_wmark_text)
if img is None:
await message.channel.send(f":warning: Template `{args[0]}` not found\n"
f"You can find a more detailed help and a list of templates at:\n"
f"<https://github.com/klemek/meme-otron/tree/master/docs/README.md>")
return
hint = db.find_nearest(args[0])
response = f":warning: Template `{args[0]}` not found\n"
if hint is not None:
response += f"Did you mean `{hint}`?\n"
response += f"You can find a more detailed help and a list of templates at:\n" \
f"<https://github.com/klemek/meme-otron/tree/master/docs/README.md>"
await message.channel.send(response)
else:
with tempfile.NamedTemporaryFile(delete=False) as output:
img.save(output, format="JPEG")
response = None
+3 -2
View File
@@ -26,6 +26,9 @@ if __name__ == "__main__":
del sys.argv[i]
img = meme_otron.compute(*sys.argv[1:])
if img is None:
hint = db.find_nearest(sys.argv[1])
if hint is not None:
print(f"Did you mean '{hint}'?")
sys.exit(1)
if output_f is None:
with os.fdopen(os.dup(sys.stdout.fileno())) as output:
@@ -40,5 +43,3 @@ if __name__ == "__main__":
except ValueError as e:
print(f"Cannot write '{output_f}': {e}", file=sys.stderr)
sys.exit(1)
+5
View File
@@ -165,3 +165,8 @@ def get_meme(name):
return DATA[ALIASES[name]].clone()
else:
return None
def find_nearest(word):
word = word.lower().strip().replace(" ", "_")
return utils.find_nearest(word, ALIASES.keys())
+17
View File
@@ -1,5 +1,6 @@
import re
import os.path as path
from Levenshtein import distance
def relative_path(file, *args):
@@ -120,3 +121,19 @@ def parse_arguments(s):
:return:
"""
return [[g for g in m if len(g) > 0][0] for m in args_regex.findall(s)]
def find_nearest(word, wlist, threshold=5):
"""
TODO
:param (str) word:
:param (list of str) wlist:
:param (int) threshold:
:rtype: str | None
:return:
"""
found = min([(distance(word, w) - abs(len(w) - len(word)), w) for w in wlist], key=lambda v: v[0])
if found[0] > threshold:
return None
return found[1]
+1
View File
@@ -1 +1,2 @@
Pillow
python-Levenshtein