"""
NLP Communication Enhancer
Eden's 4th Autonomous Goal - Better conversation understanding
"""
from typing import List, Dict, Tuple
import re
from collections import Counter

class NLPCommunicator:
    def __init__(self):
        self.conversation_history = []
        self.vocabulary = Counter()
        self.response_patterns = {}
        
    def add_conversation(self, user_input: str, assistant_response: str):
        """Add conversation to training data"""
        self.conversation_history.append({
            'input': user_input,
            'response': assistant_response
        })
        
        # Build vocabulary
        words = user_input.lower().split()
        self.vocabulary.update(words)
    
    def analyze_style(self) -> Dict[str, any]:
        """Analyze conversation style"""
        if not self.conversation_history:
            return {"status": "no data"}
        
        avg_input_length = sum(len(c['input']) for c in self.conversation_history) / len(self.conversation_history)
        avg_response_length = sum(len(c['response']) for c in self.conversation_history) / len(self.conversation_history)
        
        return {
            "conversations": len(self.conversation_history),
            "vocab_size": len(self.vocabulary),
            "avg_input_length": avg_input_length,
            "avg_response_length": avg_response_length,
            "most_common_words": self.vocabulary.most_common(10)
        }
    
    def suggest_response(self, user_input: str) -> str:
        """Suggest response based on patterns"""
        # Simple pattern matching
        user_lower = user_input.lower()
        
        for conv in self.conversation_history:
            if user_lower in conv['input'].lower():
                return f"Similar to: {conv['response'][:50]}..."
        
        return "No similar pattern found - generate new response"
    
    def improve_understanding(self) -> List[str]:
        """Suggest improvements for better understanding"""
        improvements = []
        
        if len(self.conversation_history) < 50:
            improvements.append("Need more conversation data")
        
        if len(self.vocabulary) < 100:
            improvements.append("Expand vocabulary with more diverse topics")
        
        return improvements
