# type: ignore """ NLP Avançado de Nível Acadêmico - AKIRA V21 ULTIMATE Sistema de processamento de linguagem natural ultra-potente Capaz de modificar prompts e respostas da API em tempo real """ import re import time import threading from typing import Dict, Any, List, Optional, Tuple from dataclasses import dataclass, field from collections import defaultdict import numpy as np # ============================================================ # 🎯 CONFIGURAÇÃO NLP AVANÇADO # ============================================================ @dataclass class NLPAdvancedConfig: """Configuração do NLP Avançado de Nível Acadêmico""" # Nível de agressividade na modificação do prompt prompt_modification_aggression: float = 0.8 # 0.0-1.0 # Threshold de confiança para mudanças confidence_threshold: float = 0.75 # Enable/disable features enable_semantic_analysis: bool = True enable_academic_detection: bool = True enable_context_enhancement: bool = True enable_response_modification: bool = True enable_emotion_amplification: bool = True # Modelos de análise use_bert_for_semantic: bool = True use_embeddings_for_similarity: bool = True # Cache settings cache_size: int = 1000 cache_ttl_seconds: int = 3600 class AcademicTermDetector: """Detector de termos acadêmicos e científicos""" ACADEMIC_PATTERNS = { # Campos acadêmicos 'ciencias_exatas': [ r'\b(matemática|física|química|biologia|estatística|probabilidade)\b', r'\b(teorema|prova|demonstração|equação|variável|função)\b', r'\b(cálculo|álgebra|geometria|trigonometria)\b', ], 'ciencias_humanas': [ r'\b(filosofia|história|sociologia|psicologia|antropologia)\b', r'\b(teoria|hipótese|tese|dissertação|monografia)\b', r'\b(marxismo|estruturalismo|fenomenologia)\b', ], 'engenharia_tech': [ r'\b(engenharia|programação|algoritmo|arquitetura)\b', r'\b(sistema|rede|banco de dados|backend|frontend)\b', r'\b(machine learning|inteligência artificial|IA)\b', ], 'direito': [ r'\b(direito|lei|artigo|parágrafo|jurídico)\b', r'\b(constituição|código civil|código penal)\b', r'\b(advogado|juiz|ministério público|delegacia)\b', ], 'medicina': [ r'\b(medicina|saúde|diagnóstico|tratamento)\b', r'\b(fármaco|medicamento|biológico|sintético)\b', r'\b(hospital|clínica|ambulatório|UTI)\b', ], 'economia': [ r'\b(economia|mercado|inflação|juros|PIB)\b', r'\b(monetário|fiscal|política econômica)\b', r'\b(ações|bônus|investimento|rendimento)\b', ], } ACADEMIC_INDICATORS = [ # Palavras que indicam contexto acadêmico r'\b(cite|referência|bibliografia|fonte)\b', r'\b(estudo|pesquisa|investigação|análise)\b', r'\b(teórico|empírico|metodologia|metodológico)\b', r'\b(conclusão|resultados|discussão|abstract)\b', r'\b(revisão|literatura|framework|modelo)\b', r'\b(hipótese|variável|indicador|índice)\b', r'\b(significância|relevância|validade)\b', ] def __init__(self): self._compiled_patterns = {} self._compile_patterns() def _compile_patterns(self): """Compila todos os padrões para eficiência""" for category, patterns in self.ACADEMIC_PATTERNS.items(): compiled = [re.compile(p, re.IGNORECASE) for p in patterns] self._compiled_patterns[category] = compiled self._academic_indicators = [ re.compile(p, re.IGNORECASE) for p in self.ACADEMIC_INDICATORS ] def detect(self, text: str) -> Dict[str, Any]: """Detecta contexto acadêmico no texto""" text_lower = text.lower() detected_fields = [] field_confidences = {} for category, patterns in self._compiled_patterns.items(): matches = [] for pattern in patterns: found = pattern.findall(text_lower) matches.extend(found) if matches: confidence = min(0.95, 0.5 + (len(matches) * 0.15)) detected_fields.append(category) field_confidences[category] = confidence # Indicators indicator_count = 0 for indicator in self._academic_indicators: if indicator.search(text_lower): indicator_count += 1 academic_confidence = min(0.95, 0.3 + (indicator_count * 0.1)) return { 'is_academic': indicator_count >= 2 or len(detected_fields) >= 2, 'academic_confidence': academic_confidence, 'detected_fields': detected_fields, 'field_confidences': field_confidences, 'indicator_count': indicator_count, 'academic_level': self._calculate_academic_level(text, detected_fields, indicator_count) } def _calculate_academic_level(self, text: str, fields: List[str], indicators: int) -> str: """Calcula o nível acadêmico do texto""" word_count = len(text.split()) # Very formal academic if indicators >= 4 and word_count > 100: return "phd" elif indicators >= 3 and word_count > 50: return "masters" elif indicators >= 2 and word_count > 30: return "undergraduate" elif indicators >= 1 or fields: return "high_school" else: return "casual" class SemanticAnalyzer: """Analisador semântico profundo""" def __init__(self, embedding_model=None): self.embedding_model = embedding_model self._semantic_cache = {} self._semantic_lock = threading.Lock() def analyze(self, text: str, context: Optional[List[str]] = None) -> Dict[str, Any]: """Análise semântica completa""" # Cache check cache_key = hash(text) if cache_key in self._semantic_cache: cached = self._semantic_cache[cache_key] if time.time() - cached['timestamp'] < 3600: return cached['result'] # Basic semantic analysis analysis = { 'entities': self._extract_entities(text), 'concepts': self._extract_concepts(text), 'relations': self._extract_relations(text), 'sentiment': self._analyze_sentiment(text), 'formality': self._analyze_formality(text), 'complexity': self._analyze_complexity(text), 'topics': self._extract_topics(text), 'keywords': self._extract_keywords(text), } # Context enhancement if context: analysis['context_coherence'] = self._check_context_coherence(text, context) # Store in cache with self._semantic_lock: self._semantic_cache[cache_key] = { 'timestamp': time.time(), 'result': analysis } return analysis def _extract_entities(self, text: str) -> List[Dict[str, Any]]: """Extrai entidades do texto""" entities = [] # Patterns for common entity types patterns = { 'person': r'\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)+)\b', 'organization': r'\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)\b', 'date': r'\b(\d{1,2}[/-]\d{1,2}[/-]\d{2,4})\b', 'money': r'\b(R\$|USD|EUR|\$)\s*\d+(?:[.,]\d{2})?\b', 'location': r'\b([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*)\b', } for entity_type, pattern in patterns.items(): matches = re.findall(pattern, text) for match in matches: entities.append({ 'type': entity_type, 'value': match if isinstance(match, str) else match[0] if match else '', 'position': text.find(match[0]) if isinstance(match, tuple) else -1 }) return entities def _extract_concepts(self, text: str) -> List[str]: """Extrai conceitos principais""" concepts = [] # Look for noun phrases and important concepts stopwords = {'o', 'a', 'de', 'da', 'do', 'em', 'para', 'com', 'não', 'é', 'são'} words = text.lower().split() for i, word in enumerate(words): if word not in stopwords and len(word) > 4: concepts.append(word) return list(set(concepts))[:10] def _extract_relations(self, text: str) -> List[Dict[str, str]]: """Extrai relações entre conceitos""" relations = [] # Pattern: X é/foi/será Y relation_patterns = [ (r'(\w+)\s+é\s+(\w+)', 'is_a'), (r'(\w+)\s+foi\s+(\w+)', 'was'), (r'(\w+)\s+tem\s+(\w+)', 'has'), (r'(\w+)\s+pertence\s+a\s+(\w+)', 'belongs_to'), ] for pattern, rel_type in relation_patterns: matches = re.findall(pattern, text.lower()) for match in matches: relations.append({ 'subject': match[0], 'relation': rel_type, 'object': match[1] if len(match) > 1 else '' }) return relations def _analyze_sentiment(self, text: str) -> Dict[str, Any]: """Análise de sentimento detalhada""" text_lower = text.lower() positive_words = ['bom', 'ótimo', 'excelente', 'fixe', 'feliz', 'alegre', 'amor', 'gosto'] negative_words = ['ruim', 'péssimo', 'terrível', 'odio', 'triste', 'raiva', 'raivoso'] neutral_words = ['neutro', 'normal', 'tanto faz'] pos_count = sum(1 for w in positive_words if w in text_lower) neg_count = sum(1 for w in negative_words if w in text_lower) if pos_count > neg_count: sentiment = 'positive' score = min(0.95, 0.5 + (pos_count * 0.1)) elif neg_count > pos_count: sentiment = 'negative' score = min(0.95, 0.5 + (neg_count * 0.1)) else: sentiment = 'neutral' score = 0.5 return { 'sentiment': sentiment, 'score': score, 'positive_count': pos_count, 'negative_count': neg_count } def _analyze_formality(self, text: str) -> Dict[str, Any]: """Análise de formalidade""" text_lower = text.lower() formal_indicators = [ 'senhor', 'doutor', 'professor', 'agradecido', 'gentilmente', 'por favor', 'conforme', 'destarte', 'outrossim', 'visto' ] informal_indicators = [ 'puto', 'mano', 'kkk', 'tio', 'bro', 'fala', 'eae', 'vlw' ] formal_count = sum(1 for w in formal_indicators if w in text_lower) informal_count = sum(1 for w in informal_indicators if w in text_lower) formality_score = 0.5 if formal_count > informal_count: formality_score = min(0.9, 0.5 + (formal_count * 0.1)) elif informal_count > formal_count: formality_score = max(0.1, 0.5 - (informal_count * 0.1)) return { 'formality_score': formality_score, 'formal_level': 'formal' if formality_score > 0.6 else 'informal' if formality_score < 0.4 else 'neutral', 'formal_indicators': formal_count, 'informal_indicators': informal_count } def _analyze_complexity(self, text: str) -> Dict[str, Any]: """Análise de complexidade do texto""" words = text.split() sentences = re.split(r'[.!?]+', text) avg_word_length = np.mean([len(w) for w in words]) if words else 0 avg_sentence_length = len(words) / max(len(sentences), 1) # Complex words (more than 10 characters) complex_words = [w for w in words if len(w) > 10] complexity_ratio = len(complex_words) / max(len(words), 1) # Calculate complexity score complexity_score = min(1.0, ( (avg_word_length / 10) * 0.3 + (avg_sentence_length / 20) * 0.3 + (complexity_ratio * 2) * 0.4 )) return { 'complexity_score': complexity_score, 'avg_word_length': avg_word_length, 'avg_sentence_length': avg_sentence_length, 'complex_word_ratio': complexity_ratio, 'complexity_level': 'high' if complexity_score > 0.7 else 'medium' if complexity_score > 0.4 else 'low' } def _extract_topics(self, text: str) -> List[str]: """Extrai tópicos principais""" topics = [] # Simple keyword extraction important_words = [] stopwords = {'o', 'a', 'de', 'da', 'do', 'em', 'para', 'com', 'não', 'é', 'são', 'um', 'uma', 'os', 'as'} for word in text.lower().split(): if word not in stopwords and len(word) > 3: important_words.append(word) # Count frequency word_freq = defaultdict(int) for word in important_words: word_freq[word] += 1 # Get top topics sorted_words = sorted(word_freq.items(), key=lambda x: x[1], reverse=True) topics = [w[0] for w in sorted_words[:5]] return topics def _extract_keywords(self, text: str) -> List[str]: """Extrai palavras-chave""" return self._extract_concepts(text) def _check_context_coherence(self, text: str, context: List[str]) -> float: """Verifica coerência com contexto anterior""" if not context: return 0.5 text_lower = text.lower() context_text = ' '.join(context).lower() # Check for topic continuity text_words = set(text_lower.split()) context_words = set(context_text.split()) # Jaccard similarity intersection = len(text_words & context_words) union = len(text_words | context_words) similarity = intersection / max(union, 1) return similarity class PromptModifier: """Modificador de prompts para nível acadêmico""" ACADEMIC_ENHANCEMENTS = { 'formal_intro': [ "Considerando os pressupostos teóricos relevantes e a literatura especializada, ", "Do ponto de vista epistemológico, ", "À luz das contribuições recentes no campo, ", "Em consonância com a tradição acadêmica, ", ], 'academic_bridges': [ "Destarte, ", "Outrossim, ", "Nessa perspectiva, ", "Diante do exposto, ", "Por conseguinte, ", ], 'critical_questions': [ "Qual a implicação disso para a teoria?", "Como isso se relaciona com a literatura existente?", "Quais as limitações dessa análise?", "Como operacionalizar esse conceito?", ], 'methodological_notes': [ "Do ponto de vista metodológico, ", "Considerando a abordagem adotada, ", "A partir de uma perspectiva empírica, ", "Teoricamente fundamentado em, ", ], } def __init__(self, config: NLPAdvancedConfig): self.config = config self.academic_detector = AcademicTermDetector() def modify_prompt(self, original_prompt: str, semantic_analysis: Dict[str, Any], user_context: Optional[Dict[str, Any]] = None) -> str: """Modifica o prompt para nível acadêmico se necessário""" if not self.config.enable_context_enhancement: return original_prompt # Detect academic context academic_info = self.academic_detector.detect(original_prompt) # If academic, enhance the prompt if academic_info['is_academic'] and academic_info['academic_confidence'] > self.config.confidence_threshold: enhanced_prompt = self._academicize(original_prompt, academic_info, semantic_analysis) return enhanced_prompt return original_prompt def _academicize(self, prompt: str, academic_info: Dict[str, Any], semantic: Dict[str, Any]) -> str: """Converte prompt para formato acadêmico""" # Add formal introduction if prompt is short if len(prompt.split()) < 20: intro = np.random.choice(self.ACADEMIC_ENHANCEMENTS['formal_intro']) prompt = intro + prompt # Add academic bridging if continuing discussion if semantic.get('context_coherence', 0) > 0.3: bridge = np.random.choice(self.ACADEMIC_ENHANCEMENTS['academic_bridges']) prompt = prompt + " " + bridge.rstrip(',') + ", " # Enhance with methodological note if appropriate if academic_info['academic_level'] in ['phd', 'masters']: method_note = np.random.choice(self.ACADEMIC_ENHANCEMENTS['methodological_notes']) prompt = method_note + prompt return prompt class ResponseModifier: """Modificador de respostas para nível acadêmico""" def __init__(self, config: NLPAdvancedConfig): self.config = config self.academic_detector = AcademicTermDetector() def modify_response(self, response: str, original_prompt: str, semantic_analysis: Dict[str, Any]) -> str: """Modifica a resposta da API se necessário""" if not self.config.enable_response_modification: return response academic_info = self.academic_detector.detect(original_prompt) # If academic context, enhance response if academic_info['is_academic']: enhanced = self._academicize_response(response, academic_info, semantic_analysis) return enhanced return response def _academicize_response(self, response: str, academic_info: Dict[str, Any], semantic: Dict[str, Any]) -> str: """Academiciza a resposta""" # Add nuance if response is too simplistic if semantic.get('complexity', {}).get('complexity_level') == 'low': response = self._add_nuance(response, academic_info) # Add critical thinking element if academic_info['academic_level'] in ['phd', 'masters']: response = self._add_critical_element(response, academic_info) return response def _add_nuance(self, response: str, academic_info: Dict[str, Any]) -> str: """Adiciona nuances à resposta""" nuances = [ " do ponto de vista teórico, ", " considerando as variáveis relevantes, ", " observadas as devidas ressalvas, ", " ressalvados os limites da análise, ", ] if len(response.split()) < 15: nuance = np.random.choice(nuances) # Insert nuance somewhere in the response words = response.split() insert_pos = len(words) // 2 words.insert(insert_pos, nuance.strip()) response = ' '.join(words) return response def _add_critical_element(self, response: str, academic_info: Dict[str, Any]) -> str: """Adiciona elemento de pensamento crítico""" critical_elements = [ "\n\nNota crítica: Esta análise pressupõe X, mas Y pode desafiar essa conclusão.", "\n\nConsiderando as limitações metodológicas, os resultados devem ser interpretados com cautela.", "\nDo ponto de vista epistemológico, cabe questionar: quais as premissas subjacentes?", ] if len(response.split()) > 30: element = np.random.choice(critical_elements) response = response + element return response class EmotionAmplifier: """Amplificador de emoções para modelo de moções""" EMOTION_MAPPING = { 'joy': { 'intensity_words': ['muito', 'bastante', 'extremamente', 'intensamente'], 'action_words': ['celebrar', 'comemorar', 'alegrar-se'], }, 'sadness': { 'intensity_words': ['profundamente', 'intensamente', ['muito']], 'action_words': ['lamentar', 'entristecer-se', 'afligir-se'], }, 'anger': { 'intensity_words': ['intensamente', 'bastante', 'muito'], 'action_words': ['irritar-se', 'enfurecer-se', 'indignar-se'], }, 'fear': { 'intensity_words': ['bastante', 'muito', 'intensamente'], 'action_words': ['preocupar-se', 'ansiar', 'temer'], }, } def __init__(self, config: NLPAdvancedConfig): self.config = config def amplify(self, emotion_data: Dict[str, Any], text: str) -> Dict[str, Any]: """Amplifica a detecção emocional""" if not self.config.enable_emotion_amplification: return emotion_data emotion = emotion_data.get('emotion', 'neutral') if emotion in self.EMOTION_MAPPING: mapping = self.EMOTION_MAPPING[emotion] # Check for intensity words text_lower = text.lower() intensity_count = sum(1 for w in mapping['intensity_words'] if w in text_lower) if intensity_count > 0: # Amplify the emotion original_confidence = emotion_data.get('confidence', 0.5) amplified_confidence = min(0.98, original_confidence + (intensity_count * 0.1)) emotion_data['confidence'] = amplified_confidence emotion_data['intensity'] = 'high' if intensity_count >= 2 else 'medium' emotion_data['amplified'] = True else: emotion_data['intensity'] = 'low' emotion_data['amplified'] = False return emotion_data class AdvancedNLP: """Sistema NLP Avançado Principal""" def __init__(self, config: Optional[NLPAdvancedConfig] = None): self.config = config or NLPAdvancedConfig() self.semantic_analyzer = SemanticAnalyzer() self.prompt_modifier = PromptModifier(self.config) self.response_modifier = ResponseModifier(self.config) self.emotion_amplifier = EmotionAmplifier(self.config) self.academic_detector = AcademicTermDetector() # Statistics self.stats = { 'total_analyses': 0, 'academic_prompts': 0, 'modified_prompts': 0, 'modified_responses': 0, 'avg_confidence': 0.0 } def process_input(self, text: str, context: Optional[List[str]] = None, user_info: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: """Processa entrada completa""" self.stats['total_analyses'] += 1 # Semantic analysis semantic = self.semantic_analyzer.analyze(text, context) # Academic detection academic = self.academic_detector.detect(text) if academic['is_academic']: self.stats['academic_prompts'] += 1 # Prompt modification modified_prompt = self.prompt_modifier.modify_prompt(text, semantic, user_info) if modified_prompt != text: self.stats['modified_prompts'] += 1 # Emotion amplification emotion_data = semantic.get('sentiment', {}) amplified_emotion = self.emotion_amplifier.amplify(emotion_data, text) return { 'original_text': text, 'modified_prompt': modified_prompt, 'semantic_analysis': semantic, 'academic_info': academic, 'emotion_data': amplified_emotion, 'needs_academic_mode': academic['is_academic'] and academic['academic_confidence'] > 0.7, 'academic_level': academic['academic_level'], } def process_output(self, response: str, original_prompt: str, semantic: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: """Processa saída (modifica resposta se necessário)""" modified_response = self.response_modifier.modify_response( response, original_prompt, semantic or {} ) if modified_response != response: self.stats['modified_responses'] += 1 return { 'original_response': response, 'modified_response': modified_response, 'was_modified': modified_response != response, } def get_stats(self) -> Dict[str, Any]: """Retorna estatísticas""" stats = self.stats.copy() stats['avg_confidence'] = ( stats['academic_prompts'] / max(stats['total_analyses'], 1) ) return stats # ============================================================ # 🔄 SINGLETON # ============================================================ _advanced_nlp: Optional[AdvancedNLP] = None def get_advanced_nlp(config: Optional[NLPAdvancedConfig] = None) -> AdvancedNLP: """Obtém instância do NLP Avançado""" global _advanced_nlp if _advanced_nlp is None: _advanced_nlp = AdvancedNLP(config) return _advanced_nlp # ============================================================ # 🎯 EXPORTAÇÃO # ============================================================ __all__ = [ 'NLPAdvancedConfig', 'AcademicTermDetector', 'SemanticAnalyzer', 'PromptModifier', 'ResponseModifier', 'EmotionAmplifier', 'AdvancedNLP', 'get_advanced_nlp', ]