File size: 2,880 Bytes
dc4feed
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import json
from typing import Dict, List, Optional

class MedicalKnowledgeBase:
    def __init__(self, db_path: str = "medical_db.json"):
        self.db_path = db_path
        self.data = self._load_db()

    def _load_db(self) -> Dict:
        try:
            with open(self.db_path, 'r', encoding='utf-8') as f:
                return json.load(f)
        except FileNotFoundError:
            print(f"خطا: فایل {self.db_path} پیدا نشد.")
            return {}

    def get_test_info(self, test_code: str):
        return self.data.get(test_code.upper())

class BloodAnalyzer:
    def __init__(self, knowledge_base: MedicalKnowledgeBase):
        self.kb = knowledge_base

    def analyze_single_test(self, test_code: str, value: float, gender: str = "male", age: int = None) -> Dict:
        """تک تست را تحلیل می‌کند. سن نیز برای تشخیص کودک استفاده می‌شود."""
        test_info = self.kb.get_test_info(test_code)
        
        if not test_info:
            return {
                "name_en": test_code, "name_fa": test_code,
                "value": value, "unit": "Unknown",
                "min_ref": 0, "max_ref": 0,
                "status": "UNKNOWN", "message": "این آزمایش در پایگاه داده ما وجود ندارد."
            }

        name_fa = test_info["name_fa"]
        unit = test_info["unit"]
        ranges = test_info["ranges"]
        
        # انتخاب رنج: کودک > جنسیت > پیش‌فرض
        if age and age < 18 and "child" in ranges:
            min_val, max_val = ranges["child"]
        elif gender == "male" and "male" in ranges:
            min_val, max_val = ranges["male"]
        elif gender == "female" and "female" in ranges:
            min_val, max_val = ranges["female"]
        else:
            min_val, max_val = ranges["default"]

        status = "NORMAL"
        message = "نتیجه طبیعی است."

        if value > max_val:
            status = "HIGH"
            message = test_info["conditions"]["high"]
        elif value < min_val:
            status = "LOW"
            message = test_info["conditions"]["low"]

        return {
            "name_en": test_code, "name_fa": name_fa,
            "value": value, "unit": unit,
            "min_ref": min_val, "max_ref": max_val,
            "status": status, "message": message
        }

    def generate_full_report(self, input_data: Dict[str, float], gender: str = "male", age: int = None):
        """تمام تست‌ها را تحلیل کرده و لیست برمی‌گرداند."""
        report = []
        for code, value in input_data.items():
            result = self.analyze_single_test(code, value, gender, age)
            report.append(result)
        return report