To improve the robustness of your verification logic, we can enhance it by:
✅ Normalizing both the example sentence and the answer by:
- Converting both to lowercase.
- Optionally stripping punctuation (already partially handled).
- Matching each word from the normalized answer to words in the normalized example.
- Still preserving original casing in logs for clarity.
Only if no match is found even after normalization, should we log an error.
Here’s the corrected version of your method:
import re
def verify_answers(self, result: Result):
for entry in result.entries:
for meaning in entry.meanings:
for example in meaning.examples:
# Normalize both the example sentence and the answer
cleaned_text = re.sub(r"^\W+|\W+$|,", "", example.example) # Remove leading/trailing non-word chars and commas
example_words = cleaned_text.lower().split() # Case-insensitive matching
answer_words = example.answer.split()
normalized_answer_words = [word.lower() for word in answer_words]
# Check if all normalized answer words are present in the normalized example text
all_found = all(word in example_words for word in normalized_answer_words)
if not all_found:
# Try exact match with original casing as a fallback check
# (This step is optional depending on whether you want strict casing or not)
# But per your requirement, only log error after trying case-insensitive
logger.error(f"""Answer error:
Example: {example.example}
Expected Answer: {example.answer}
Example Words: {example_words}
Answer Words: {normalized_answer_words}""")
return result
Wait — actually, in your current code, you're already splitting and cleaning the example, and checking with:
all( word in example_words for word in answer_words )
But that is case-sensitive and will fail e.g. if answer is "Go quickly" and text is "go quickly".
So the fix is to lowercase both sides for comparison, but retain original forms in logs.
Let me adjust and simplify the fixed version more clearly:
✅ Corrected & Improved Version:
import re
def verify_answers(self, result: Result):
for entry in result.entries:
for meaning in entry.meanings:
for example in meaning.examples:
# Clean the example sentence: remove leading/trailing punctuation
cleaned_text = re.sub(r"^\W+|\W+$|,", "", example.example)
# Split into words and normalize to lowercase
example_words = cleaned_text.lower().split()
# Normalize answer words to lowercase
answer_words = [word.lower() for word in example.answer.split()]
# Check if all answer words are present in the cleaned example
all_found = all(word in example_words for word in answer_words)
if not all_found:
# Log detailed error with original casing preserved
logger.error(f"""Answer error:
Example: {example.example}
Expected Answer: {example.answer}
Example Words: {example_words}
Answer Words: {answer_words}""")
return result
Wait — actually, in the above, I replaced example_words in the log with the lowercase version too — but that would show lowercase words, which might confuse.
So we should log the original example.example, and the original example.answer, but compare normalized versions internally.
✅ So here's the truly correct final version:
✅ Final Correct Version:
import re
def verify_answers(self, result: Result):
for entry in result.entries:
for meaning in entry.meanings:
for example in meaning.examples:
# Normalize the example sentence for word matching
cleaned_text = re.sub(r"^\W+|\W+$|,", "", example.example) # Strip leading/trailing non-word chars and commas
example_words = cleaned_text.lower().split() # Lowercase for comparison
# Normalize answer words (lowercase)
answer_words = [word.lower() for word in example.answer.split()]
# Check if all words in answer are found in the example
all_found = all(word in example_words for word in answer_words)
if not all_found:
# Log the error using original casing
logger.error(f"""Answer error:
Example: {example.example}
Expected Answer: {example.answer}
Example Words: {cleaned_text.split()} # Show words as-split from cleaned text (non-lowercased)
Answer Words: {answer_words}""") # But answer words are lowercased for search, so maybe log original?
# Wait, better to log original answer and original example, but split/compare normalized.
# So maybe this is clearer:
logger.error(f"""Answer error:
Example: {example.example}
Expected Answer: {example.answer}
Example Text (cleaned): {cleaned_text}
Example Words (lowercased): {example_words}
Answer Words (lowercased): {answer_words}""")
return result
Wait — there's some confusion in the log message between what is lowercased and what isn't. Let's clarify once more.
✅ FINAL CLEAN VERSION (with clear logs):
import re
def verify_answers(self, result: Result):
for entry in result.entries:
for meaning in entry.meanings:
for example in meaning.examples:
# Clean the example sentence: remove leading/trailing punctuation
cleaned_text = re.sub(r"^\W+|\W+$|,", "", example.example)
# Split and lowercase for comparison
example_words = cleaned_text.lower().split()
# Lowercase the answer words for comparison
answer_words = [word.lower() for word in example.answer.split()]
# Check if all answer words are in the example
all_found = all(word in example_words for word in answer_words)
if not all_found:
logger.error(f"""Answer verification failed.
Example sentence: {example.example}
Cleaned example: {cleaned_text}
Answer: {example.answer}
Example words: {example_words}
Answer words: {answer_words}""")
return result
Wait — in that log, example_words is the lowercase-split list, which may be confusing since the user sees lowercase words in log but original sentence has casing.
So we should either:
- log
cleaned_text.split() instead of example_words (but that includes original casing),
- or just clarify in comment.
Better yet, let's make it crystal clear:
✅ GOLDEN VERSION (precise and readable logs):
import re
def verify_answers(self, result: Result):
for entry in result.entries:
for meaning in entry.meanings:
for example in meaning.examples:
# Preprocess the example: clean and lowercase for matching
cleaned_text = re.sub(r"^\W+|\W+$|,", "", example.example) # Remove stray punctuation
example_words = cleaned_text.lower().split() # Lowercase words for comparison
answer_words = [word.lower() for word in example.answer.split()] # Lowercase answer words
# Check if all words from answer are in the example
all_found = all(word in example_words for word in answer_words)
if not all_found:
# Log with ORIGINAL casing preserved in display
logger.error(f"""❌ Answer mismatch.
Example sentence: {example.example}
Cleaned sentence: {cleaned_text}
Answer provided: {example.answer}
Words in example: {cleaned_text.split()} # <- shown as-is (uncleaned casing)
Lowercased words: {example_words}
Searched answer: {answer_words}""")
return result
Wait — no! That's mixing concepts. Let's decide:
cleaned_text.split() gives words with original casing.
example_words is lowercased.
answer_words is lowercased.
We want to:
- Compare
answer_words (lowercased) against example_words (lowercased).
- But show the user the original forms in logs.
✅ So here's the perfect final version:
✅ FINAL, CORRECT, CLEAR VERSION:
import re
def verify_answers(self, result: Result):
for entry in result.entries:
for meaning in entry.meanings:
for example in meaning.examples:
# Clean punctuation from example and lowercase for comparison
cleaned_text = re.sub(r"^\W+|\W+$|,", "", example.example)
example_words = cleaned_text.lower().split() # Normalized for search
answer_words = [word.lower() for word in example.answer.split()] # Normalized answer
# Check if all answer words appear in the example
all_found = all(word in example_words for word in answer_words)
if not all_found:
logger.error(f"""❌ Answer not found in example.
----------------------------------------
Example sentence: {example.example}
Cleaned sentence: {cleaned_text}
Answer provided: {example.answer}
Lowercased answer words: {answer_words}
Words in (cleaned) example: {example_words}""")
return result
✅ This version:
- Does case-insensitive matching.
- Still shows the user-friendly original forms.
- Logs exactly what was matched and where it failed.
Let me know if you also want to count partial matches or suggest closest matches.