"""
EnhancedKnowledgeRetrieval
Generated by Eden via recursive self-improvement
2025-11-01 00:03:48.247089
"""

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.metrics.pairwise import cosine_similarity

class EnhancedKnowledgeRetrieval:
    def __init__(self):
        self.documents = []  # List to store documents (lines of code, user queries, etc.)
        self.tfidf_vectorizer = TfidfVectorizer()
    
    def add_document(self, document: str) -> None:
        """Add a new document to the knowledge base."""
        self.documents.append(document)
    
    def retrieve_relevant_info(self, query: str) -> list:
        """
        Retrieve relevant information based on input queries.
        
        Args:
            query (str): The user's input query.
            
        Returns:
            A list of documents that are most relevant to the query.
        """
        # Convert documents and query into TF-IDF feature vectors
        tfidf_matrix = self.tfidf_vectorizer.fit_transform(self.documents + [query])
        
        # Compute cosine similarity between query and all other documents
        cosine_similarities = cosine_similarity(tfidf_matrix[-1], tfidf_matrix[:-1]).flatten()
        
        # Get the indices of the top 3 most relevant documents
        top_indices = cosine_similarities.argsort()[:-4:-1]
        
        return [self.documents[i] for i in top_indices]

# Example Usage
if __name__ == "__main__":
    # Initialize the capability
    retrieval_system = EnhancedKnowledgeRetrieval()
    
    # Add some example documents to the knowledge base
    retrieval_system.add_document("CAPABILITY_NAME: [name]")
    retrieval_system.add_document("PURPOSE: [what it does]")
    retrieval_system.add_document("CODE:")
    
    # Query for relevant information
    query = "How can I improve my search capabilities?"
    results = retrieval_system.retrieve_relevant_info(query)
    
    print(f"Relevant Information:\n{''.join(results)}")