File size: 4,694 Bytes
1eb76aa
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env python3
"""
Test simplifié de l'agent NER médical + Mapper de template
Extraction → Affichage → Mapping → Affichage → Fichier TXT
"""
import os
import sys
from type3_extract_entities import MedicalNERAgent, ExtractedData
from medical_template3_mapper import MedicalTemplateMapper, create_filled_medical_report


def main():
    """Test simplifié : extraction + mapping + génération fichier"""
    print("🏥 TEST AGENT NER MÉDICAL + MAPPER")
    print("=" * 50)
    
    # Transcription à analyser
    transcription = """Compte rendu classique. L'utérus est antéversé de taille 7,8 cm 60 d'hystérométrie 
    3,7 d'endomètre triangulaire zone jonctionnelle épaissie focale d'adénomyose diffuse fibromes 
    myomètre pas de fibromes. Le col voulut le laisser comme il est la morphologie triangulaire. 
    L'ovaire droit mesure 26 x 20 mm, 5 follicules. L'ovaire gauche accessibilité au maître rétro 
    thérape par contre l'autre il est normal il mesure 25 x 19 mm siège de CFA : 22 follicules. 
    Le Doppler : IP 3,24 - IR 0,91 et le reste tout en fait qui est l'ovaire gauche d'accès 
    difficile à rétro-utérin."""
    
    try:
        # ÉTAPE 1: EXTRACTION DES ENTITÉS
        print("🔬 ÉTAPE 1: EXTRACTION DES ENTITÉS MÉDICALES")
        print("-" * 50)
        
        agent = MedicalNERAgent()
        extracted_data = agent.extract_medical_entities(transcription)
        
        # Affichage des résultats d'extraction
        print(agent.print_extraction_report(extracted_data))
        
        # ÉTAPE 2: MAPPING VERS TEMPLATE
        print("\n📋 ÉTAPE 2: MAPPING VERS TEMPLATE")
        print("-" * 50)
        
        mapper = MedicalTemplateMapper()
        mapping_result = mapper.map_extracted_data_to_template(extracted_data)
        
        # Affichage des résultats de mapping
        print(mapper.print_mapping_report(mapping_result))
        
        # ÉTAPE 3: GÉNÉRATION DU FICHIER
        print("\n💾 ÉTAPE 3: GÉNÉRATION DU FICHIER")
        print("-" * 50)
        
        output_file = "rapport_medical_final.txt"
        with open(output_file, 'w', encoding='utf-8') as f:
            f.write("RAPPORT MÉDICAL GÉNÉRÉ AUTOMATIQUEMENT\n")
            f.write("=" * 50 + "\n\n")
            f.write("📋 DONNÉES EXTRAITES:\n")
            f.write(agent.print_extraction_report(extracted_data))
            f.write("\n\n📄 TEMPLATE REMPLI:\n")
            f.write("-" * 50 + "\n")
            f.write(mapping_result.filled_template)
            
            # Ajouter les statistiques
            f.write("\n\n📊 STATISTIQUES:\n")
            f.write("-" * 30 + "\n")
            f.write(f"Score d'extraction: {extracted_data.extraction_confidence:.1%}\n")
            f.write(f"Champs mappés: {len(mapping_result.mapped_fields)}\n")
            f.write(f"Score de mapping: {mapping_result.mapping_confidence:.1%}\n")
            f.write(f"Erreurs de mapping: {len(mapping_result.errors)}\n")
        
        print(f"✅ Rapport médical sauvegardé dans: {output_file}")
        
        # Affichage du résumé final
        print(f"\n📊 RÉSUMÉ FINAL:")
        print(f"   🎯 Score d'extraction: {extracted_data.extraction_confidence:.1%}")
        print(f"   🎯 Champs mappés: {len(mapping_result.mapped_fields)}")  
        print(f"   🎯 Score de mapping: {mapping_result.mapping_confidence:.1%}")
        print(f"   ⚠️  Erreurs: {len(mapping_result.errors)}")
        print(f"   📝 Placeholders non mappés: {len(mapping_result.unmapped_placeholders)}")
        
        # Affichage des erreurs si présentes
        if mapping_result.errors:
            print(f"\n⚠️  ERREURS DE MAPPING:")
            for error in mapping_result.errors:
                print(f"   - {error}")
        
        # Affichage de quelques placeholders non mappés
        if mapping_result.unmapped_placeholders:
            print(f"\n📝 QUELQUES PLACEHOLDERS NON MAPPÉS:")
            for placeholder in mapping_result.unmapped_placeholders[:5]:
                print(f"   - {placeholder[:60]}...")
        
        print("\n🎉 PROCESSUS TERMINÉ AVEC SUCCÈS!")
        
        # Affichage d'un aperçu du template rempli
        print(f"\n👁️  APERÇU DU TEMPLATE REMPLI (100 premiers caractères):")
        print("-" * 50)
        preview = mapping_result.filled_template[:200] + "..." if len(mapping_result.filled_template) > 200 else mapping_result.filled_template
        print(preview)
        
    except Exception as e:
        print(f"\n💥 ERREUR: {e}")
        import traceback
        traceback.print_exc()

if __name__ == "__main__":
    main()