better misspelling handling
This commit is contained in:
@@ -774,9 +774,46 @@ def search():
|
|||||||
results = lookup_fts(cleaned_keywords)
|
results = lookup_fts(cleaned_keywords)
|
||||||
if not results:
|
if not results:
|
||||||
h = hunspell.HunSpell("data/kjv.dic", "data/kjv.aff")
|
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"<div class='correction-notice'>Showing results for '<strong>{corrected_keyword}</strong>' (corrected from '{cleaned_keywords}')</div>"
|
||||||
|
arg_view = request.args.get("view", None)
|
||||||
|
if arg_view == "plain":
|
||||||
|
response = f"Showing results for '{corrected_keyword}' (corrected from '{cleaned_keywords}')\n<br>" + "<br>".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)
|
suggestions = h.suggest(cleaned_keywords)
|
||||||
return "{} not found, try one of the following: {}".format(
|
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)
|
response_list = scripture_response(results)
|
||||||
arg_view = request.args.get("view", None)
|
arg_view = request.args.get("view", None)
|
||||||
|
|||||||
Reference in New Issue
Block a user