From 7983472db594ccf3aec4dd3863a30345f30fbf04 Mon Sep 17 00:00:00 2001 From: Tyler Date: Sat, 25 Oct 2025 00:55:14 -0700 Subject: [PATCH] better misspelling handling --- app.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/app.py b/app.py index af917e5..f812c69 100644 --- a/app.py +++ b/app.py @@ -774,9 +774,46 @@ def search(): results = lookup_fts(cleaned_keywords) if not results: h = hunspell.HunSpell("data/kjv.dic", "data/kjv.aff") + + # Split query into words and correct each one + words = cleaned_keywords.split() + corrected_words = [] + had_corrections = False + + for word in words: + if h.spell(word): + # Word is spelled correctly + corrected_words.append(word) + else: + # Word is misspelled, get suggestions + word_suggestions = h.suggest(word) + if word_suggestions: + corrected_words.append(word_suggestions[0]) + had_corrections = True + else: + # No suggestions, keep original + corrected_words.append(word) + + if had_corrections: + corrected_keyword = " ".join(corrected_words) + results = lookup_fts(corrected_keyword) + if results: + # Prepend correction notice to results + response_list = scripture_response(results) + correction_notice = f"
Showing results for '{corrected_keyword}' (corrected from '{cleaned_keywords}')
" + arg_view = request.args.get("view", None) + if arg_view == "plain": + response = f"Showing results for '{corrected_keyword}' (corrected from '{cleaned_keywords}')\n
" + "
".join(response_list) + elif arg_view == "rich": + response = correction_notice + "".join(response_list) + else: + response = jsonify({"corrected_from": cleaned_keywords, "corrected_to": corrected_keyword, "results": response_list}) + return response + + # If we still have no results, show suggestions suggestions = h.suggest(cleaned_keywords) return "{} not found, try one of the following: {}".format( - cleaned_keywords, ", ".join(suggestions) + cleaned_keywords, ", ".join(suggestions) if suggestions else "No suggestions available" ) response_list = scripture_response(results) arg_view = request.args.get("view", None)