from typing import Dict, List import re class ResponseFormatter: @staticmethod def clean_text(text: str) -> str: """Clean up raw text by removing excessive whitespace and common boilerplate""" # Remove multiple newlines and spaces text = re.sub(r'\n+', '\n', text) text = re.sub(r'\s+', ' ', text) # Remove common website elements text = re.sub(r'SUBSCRIBE|RECENT|POPULAR|TRENDY', '', text, flags=re.I) text = re.sub(r'Copyright © \d{4}.*', '', text) text = re.sub(r'Privacy Policy|Terms of Service', '', text) return text.strip() @staticmethod def format_sources(sources: List[Dict]) -> str: """Format source URLs into readable references""" if not sources: return "" formatted_sources = "\n\nSources:\n" for i, source in enumerate(sources, 1): formatted_sources += f"{i}. {source['url']}\n" return formatted_sources @staticmethod def format_response(api_response: Dict) -> str: """Convert API response to natural language""" if "error" in api_response: return f"Sorry, I encountered an error: {api_response['error']}" if "response" not in api_response: return "I couldn't find any relevant information." # Clean and format the main response clean_response = ResponseFormatter.clean_text(api_response["response"]) # Add sources if available if "sources" in api_response: clean_response += ResponseFormatter.format_sources(api_response["sources"]) return clean_response