FINALMENTE ACABOU PAPAPAAAAA

This commit is contained in:
2026-05-17 23:01:22 +01:00
parent c979692fd9
commit 058bbaaea2
2 changed files with 960 additions and 40 deletions

View File

@@ -558,6 +558,76 @@ class MaterialsRAGService {
return scoredChunks.take(maxChunks).map((e) => e.key).toList();
}
/// Formatar texto extraído do PDF para melhor legibilidade
static String _formatPDFText(String text) {
if (text.isEmpty) return text;
String formatted = text;
// Corrigir quebras de linha excessivas
formatted = formatted.replaceAll(RegExp(r'\n{3,}'), '\n\n');
// Corrigir espaços excessivos
formatted = formatted.replaceAll(RegExp(r'[ \t]+'), ' ');
// Remover espaços no início/fim das linhas
formatted = formatted.split('\n').map((line) => line.trim()).join('\n');
// Corrigir parágrafos (linhas que terminam com ponto e seguem sem espaço)
formatted = formatted.replaceAllMapped(
RegExp(r'\.(\n)([A-ZÁÉÍÓÚÀÂÊÔÃÕÇ])'),
(match) => '.\n\n${match.group(2)}',
);
// Corrigir quebras de palavras com hífen no fim da linha
formatted = formatted.replaceAllMapped(
RegExp(r'([a-zA-Záéíóúàâêôãõç])-\n([a-zA-Záéíóúàâêôãõç])'),
(match) => '${match.group(1)}${match.group(2)}',
);
// Adicionar quebras de parágrafo para títulos (linhas em maiúsculas)
formatted = formatted.replaceAllMapped(
RegExp(r'\n([A-ZÁÉÍÓÚÀÂÊÔÃÕÇ][A-ZÁÉÍÓÚÀÂÊÔÃÕÇ\s]{10,})\n'),
(match) => '\n\n${match.group(1)}\n\n',
);
// Limpar quebras de linha no início e fim
formatted = formatted.trim();
return formatted;
}
/// Obter o texto completo de um PDF específico para pré-visualização
static Future<String> getFullPDFText(String fileName, String teacherId) async {
try {
// Remover extensão se existir
final cleanFileName = fileName.endsWith('.pdf') ? fileName : '$fileName.pdf';
// Usar cache do texto completo se disponível
final cacheKey = '${cleanFileName}_preview_v6';
if (_chunksCache.containsKey(cacheKey) && _chunksCache[cacheKey]!.isNotEmpty) {
final fullText = _chunksCache[cacheKey]!.first;
Logger.info('Using cached preview text for $cleanFileName: ${fullText.length} chars');
return fullText;
}
// Extrair texto completo
final rawText = await _extractFullText(cleanFileName, teacherId);
// Formatar texto para melhor legibilidade
final formattedText = _formatPDFText(rawText);
// Guardar em cache
_chunksCache[cacheKey] = [formattedText];
Logger.info('PDF "$cleanFileName" -> ${formattedText.length} chars extracted and formatted for preview');
return formattedText;
} catch (e) {
Logger.error('Error getting full PDF text for $fileName: $e');
return '';
}
}
/// Clear the chunks cache
static void clearCache() {
_chunksCache.clear();