File size: 2,329 Bytes
cbc783a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import sqlite3

def get_diseases_by_symptoms(symptoms):
    """
    Get diseases that have ALL the given symptoms.
    
    Parameters:
    - symptoms: List of symptom names
    """
    if not symptoms:
        return []
        
    conn = sqlite3.connect("disease.db")
    cur = conn.cursor()

    try:
        placeholder = ",".join(["?"] * len(symptoms))
        
        # Find diseases that have ALL the symptoms
        query = f"""
            SELECT d.name
            FROM disease d
            WHERE (
                SELECT COUNT(DISTINCT s.name)
                FROM disease_symptom ds
                JOIN symptom s ON s.id = ds.symptom_id
                WHERE ds.disease_id = d.id AND s.name IN ({placeholder})
            ) = ?
        """
        
        cur.execute(query, symptoms + [len(symptoms)])
        result = [row[0] for row in cur.fetchall()]
        return result
    except Exception as e:
        print(f"SQL Error in get_diseases_by_symptoms: {e}")
        return []
    finally:
        conn.close()

def get_related_symptoms(disease_names, exclude=[]):
    """
    Get ALL symptoms from the specified diseases, excluding the ones provided.
    
    Parameters:
    - disease_names: List of disease names to get symptoms from
    - exclude: List of symptom names to exclude from the results
    """
    if not disease_names:
        return []

    conn = sqlite3.connect("disease.db")
    cur = conn.cursor()

    try:
        placeholder = ",".join(["?"] * len(disease_names))
        exclude_clause = ""
        if exclude:
            exclude_placeholder = ",".join(["?"] * len(exclude))
            exclude_clause = f"AND s.name NOT IN ({exclude_placeholder})"
        
        # Get ALL OTHER symptoms from the matching diseases
        query = f"""
            SELECT DISTINCT s.name 
            FROM symptom s
            JOIN disease_symptom ds ON s.id = ds.symptom_id
            JOIN disease d ON d.id = ds.disease_id
            WHERE d.name IN ({placeholder}) {exclude_clause}
        """
        
        cur.execute(query, disease_names + (exclude if exclude else []))
        result = [row[0] for row in cur.fetchall()]
        return result
    except Exception as e:
        print(f"SQL Error in get_related_symptoms: {e}")
        return []
    finally:
        conn.close()