final_agent_course / utils /text_tool.py
tuan3335's picture
use langchain
040a6c6
"""
Text Tool - Process reversed text questions
"""
from typing import Dict, Any
def is_likely_reversed(text: str) -> bool:
words = text.split()
if len(words) < 3:
return False
reversed_indicators = [
text.startswith('.'),
text.endswith('fI'),
'ecnetnes' in text,
'rewsna' in text,
'noitseuq' in text
]
return sum(reversed_indicators) >= 2
def reverse_text(text: str) -> str:
return text[::-1]
def reverse_text_if_needed(question: str, ai_brain=None) -> Dict[str, Any]:
if is_likely_reversed(question):
reversed_question = reverse_text(question)
return {
"should_reverse": True,
"original_text": question,
"processed_text": reversed_question,
"confidence": "high",
"reasoning": "Text appears to be written backwards"
}
return {
"should_reverse": False,
"original_text": question,
"processed_text": question,
"confidence": "high",
"reasoning": "Text appears normal"
}