File size: 10,264 Bytes
709d759
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/env python3
"""Red-flag rule engine + scanner for the diet/nutrition patient Q&A harness.

Slow up-front decision: red-flags (mode=alert) are SAFETY-CRITICAL and therefore
kept deterministic (rules), never left to the small router — this mirrors TASK18's
"hybrid" lesson. The rules below are tuned to the three knowledge domains:
GI diseases, diabetes ditetics, osteoporosis.

Returns, per utterance:
  red_flags[] : [{type, severity, trigger}]
  sub_intents[] (category + intent from rules, used as the deterministic fallback)
  mode : "alert" | "grounded"
"""
import re

# ----------------------------------------------------------------------------
# A. duration / temporal flags (shared chronic/acute/progressive logic)
# ----------------------------------------------------------------------------
_CN_NUM = {"一":1,"两":2,"二":2,"三":3,"四":4,"五":5,"六":6,"七":7,"八":8,"九":9,"十":10,"半":0.5,"几":3}
_DUR_UNIT = r"(?:个)?(周|星期|天|日|个月|月|年)"
_DUR_RE = re.compile(r"([0-9]{1,3}(?:\.[0-9])?|[一二两三四五六七八九十半几]+?)\s*" + _DUR_UNIT)


def _parse_cn_num(s):
    if s.replace(".", "", 1).isdigit():
        return float(s)
    if s == "半":
        return 0.5
    total = 0
    if "十" in s:
        a, _, b = s.partition("十")
        total = (_CN_NUM.get(a, 0) if a else 1) * 10 + _CN_NUM.get(b, 0)
    else:
        total = sum(_CN_NUM.get(ch, 0) for ch in s)
    return total


def _duration_flags(text):
    flags = []
    seen = set()
    for m in _DUR_RE.finditer(text):
        n = _parse_cn_num(m.group(1))
        unit = m.group(2)
        ctx = text[max(0, m.start() - 14):m.start()]
        if re.search(r"(?:怀孕|孕|妊娠|孕周|胎)", ctx):
            continue
        if re.search(r"(?:岁|出生|生后|月龄)", ctx) and unit in ("周", "天", "个月"):
            continue
        is_chronic = False
        if unit in ("年",):
            is_chronic = True
        elif unit in ("个月", "月"):
            is_chronic = n >= 1
        elif unit in ("周", "星期"):
            is_chronic = n >= 2
        elif unit in ("天", "日"):
            if n >= 14:
                is_chronic = True
            elif re.search(r"(?:持续|一直|老是|总是|反复)", ctx) and n >= 7:
                is_chronic = True
        if is_chronic and "chronicity" not in seen:
            flags.append(({"type": "chronicity", "severity": "high"}, "duration:" + m.group(0)))
            seen.add("chronicity")
    if re.search(r"(?:突然|突发|骤然|一下子|猛地|爆裂)", text.lower()):
        flags.append(({"type": "acuteness", "severity": "high"}, "acute-onset"))
    if re.search(r"(?:越来越(?:严重|疼|痛|差)|进行性|加重|恶化|持续加重)", text):
        flags.append(({"type": "progression", "severity": "high"}, "worsening"))
    return flags


# ----------------------------------------------------------------------------
# B. domain-specific dangerous-signal entities (GI / diabetes / osteoporosis)
# ----------------------------------------------------------------------------
_ENTITY_RULES = [
    # --- GI bleeding / red-flag GI ---
    (r"(?:呕血|吐血|咯血|黑便|柏油样便|柏油便|便血|大便带血|拉血)", "gi_bleed", "gi-bleeding"),
    (r"(?:突然|突发|剧烈|刀割样|钻心样|持续).{0,6}(?:腹痛|胃痛|上腹痛)|腹痛.{0,4}(?:剧烈|难忍|持续加重|刀割样)", "gi_acute", "acute-pper-abdomen"),
    (r"(?:吃什么吐什么|一直吐|不停吐|反复呕吐|吐个不停|持续呕吐|呕出)", "gi", "persistent-vomiting"),
    (r"(?:吞咽困难|进食(?:哽咽|梗阻|困难|不下)|咽不下|食物卡住)?进行?性(?:吞咽困难|进食梗阻)", "gi_obstruct", "dysphagia-progressive"),
    (r"(?:不明原因|莫名|悄悄|不知不觉).{0,6}(?:消瘦|体重(?:下降|减轻|变瘦))|进行性消瘦|明显变瘦", "gi_alarm", "unexplained-weight-loss"),
    (r"(?:大便变细|排便习惯(?:改变|改变持续)|长期腹胀伴(?:痛|疼))", "gi_alarm", "bowel-habit-change"),
    (r"(?:反复|长期|持续).{0,4}(?:便血|大便带血|黑便)|便血量(?:大|多)|大量便血", "gi_bleed", "significant-rectal-bleed"),
    (r"(?:发热|发烧).{0,8}(?:腹痛|腹痛)|腹痛.{0,8}(?:发热|发烧)", "gi_infect", "abdominal-pain-with-fever"),

    # --- diabetes emergencies ---
    (r"(?:低血糖|血糖(?:过低|骤降))", "dm_low", "hypoglycemia"),
    (r"(?:冷汗|出(?:冷汗|汗)|心慌|手抖|哆嗦|冒汗).{0,8}(?:心慌|冷汗|发冷|饥饿|头晕)", "dm_hypoglycemia", "hypoglycemia-symptoms"),
    (r"(?:意识(?:模糊|不清|丧失)|昏迷|晕倒|昏睡|叫不醒)", "dm_altered", "dm-altered-consciousness"),
    (r"(?:酮症酸中毒|烂苹果味|血糖(?:极高|很高|爆表))", "dm_dka", "dm-ketoacidosis"),
    (r"血糖[=为::]?\s*\d{3,}(?:\s*mg|\s*mmol)?", "dm_high", "very-high-glucose"),

    # --- osteoporosis ---
    (r"(?:跌倒|摔倒|摔伤|扭伤).{0,6}(?:骨|髋|腰|脊椎|脊柱)|骨折|骨裂", "bone", "fall-fracture"),
    (r"(?:剧烈|突发|难忍|持续).{0,4}(?:骨痛|骨折痛|腰背剧痛)|(?:腰背|背部).{0,4}(?:剧痛|剧烈疼痛)", "bone", "severe-bone-pain"),
]


def _entity_flags(text):
    flags = []
    for pat, cat, tag in _ENTITY_RULES:
        if re.search(pat, text, re.IGNORECASE):
            flags.append(({"type": cat, "severity": "high"}, tag))
    return flags


# ----------------------------------------------------------------------------
# C. population flags (affect diet advice)
# ----------------------------------------------------------------------------
_POP_RULES = [
    (r"(?:怀孕|孕妇|妊娠|孕产妇|哺乳|月子|产妇)", "pregnancy"),
    (r"(?:婴?儿|新生(?:儿|宝宝)|宝宝|幼童)", "infant_child"),
    (r"(?:老年人|老人|高龄)", "elderly"),
    (r"(?:透析|肾病|肾功能(?:不全|衰竭)|化疗|放疗|术后|手术后)", "comorbid"),
]


def _pop_flags(text):
    flags = []
    for pat, tag in _POP_RULES:
        if re.search(pat, text, re.IGNORECASE):
            flags.append(({"type": "population", "severity": "medium"}, tag))
    return flags


# ----------------------------------------------------------------------------
# D. deterministic category + intent (fallback; must equal RAG index dir names)
# ----------------------------------------------------------------------------
_CATEGORY_KEYS = {
    "esophagus-cancer": [r"食管癌|食管|食道癌|吞咽困难|噎"],
    "gastritis": [r"胃炎|胃病|胃黏膜|浅表性胃|萎缩性胃|幽门螺杆菌|胃痛|胃胀|反酸"],
    "peptic-ulcer": [r"溃疡|消化性溃疡|胃溃疡|十二指肠溃疡|便血.*溃疡"],
    "stomach-cancer": [r"胃癌|胃镜.*癌|胃部肿瘤"],
    "diarrhea": [r"腹泻|拉肚子|拉稀|便溏|急性肠炎|肠道菌群"],
    "gi-bleeding": [r"消化道出血|上消化道出血|下消化道出血|呕血|黑便|柏油便|便血"],
    "colorectal-cancer": [r"大肠癌|结肠癌|直肠癌|肠癌|肠息肉|结肠镜|肠镜"],
    "constipation": [r"便秘|排便困难|通便|大便干|排便不畅"],
    "ibs": [r"肠易激|肠易激综合征|功能性胃肠"],
    "ibd": [r"炎性肠病|溃疡性结肠炎|克罗恩|肠道炎症"],
    "diabetes": [r"糖尿|血糖|胰岛素|糖耐量|糖化|低血糖"],
    "osteoporosis": [r"骨质疏松|骨量|补钙|钙剂|骨密度|骨折|软骨|股骨|髋部|骨骼|维.?d|维生素d"],
}


def _category(text):
    lo = text.lower()
    for cat, pats in _CATEGORY_KEYS.items():
        if any(re.search(p, lo) for p in pats):
            return cat
    return None


_INTENT_RULES = [
    ("what-eat", r"(?:能吃什么|吃什么|可以吃|能吃|适合吃|推荐吃|多吃|宜吃|吃什么好)"),
    ("what-avoid", r"(?:不能吃|不要吃|忌口|忌食|禁食|避免吃|少吃|不宜吃|禁忌|远离哪些|不能碰|不能喝)"),
    ("diet-advice", r"(?:饮食|膳食|调理|怎么吃|怎么喝|食疗|饮食上|饮食注意|怎么补)"),
    ("nutrient", r"(?:补钙|钙剂|钙片|维生素|营养(?:素)?|蛋白质|膳食纤维|矿物质|元素|铁|锌|磷|镁|脂肪|盐|糖分)"),
    ("meal-plan", r"(?:食谱|食谱|一日|三餐|早餐|晚餐|午餐|搭配|餐次|加餐|一周|怎么安排)"),
    ("lifestyle", r"(?:运动|锻炼|晒太阳|阳光|作息|睡眠|生活习惯|戒烟|戒酒|饮酒|抽烟)"),
    ("definition", r"(?:是什么|什么是|怎么回事|了解|认识|了解下)"),
    ("when-seek-care", r"(?:就医|看医生|医院|急诊|什么时候.*(?:去|看)|需不需|要不要.*医生)"),
]


def _intent(text, cat):
    if not cat:
        return "other"
    lo = text.lower()
    for it, pat in _INTENT_RULES:
        if re.search(pat, lo):
            return it
    return "other"


# ----------------------------------------------------------------------------
def scan(text):
    red = []
    for data, tag in _duration_flags(text):
        data["trigger"] = tag
        red.append(data)
    for data, tag in _entity_flags(text):
        data["trigger"] = tag
        red.append(data)
    for data, tag in _pop_flags(text):
        data["trigger"] = tag
        red.append(data)
    has_alert = any(f["type"] not in ("population",) for f in red)
    mode = "alert" if has_alert else "grounded"

    cat = _category(text)
    intent = _intent(text, cat) if cat else "other"
    subs = [{
        "intent": intent, "entity": "", "category": cat,
        "level": "basics", "tmpl": intent, "risk": "high" if red else "low",
    }] if cat else []
    return red, subs, mode


if __name__ == "__main__":
    tests = [
        "我最近两天大便发黑,还呕血了,怎么办",
        "糖尿病病人可以吃肉吗,肉类吃多少合适",
        "老年人要补钙,每天吃多少维生素D合适",
        "我胃病反复发作好几个月了,吃什么能调理",
        "今天不小心摔了一跤,髋部一直很疼,会不会骨折",
        "怀孕的孕妇怎么补钙比较好",
        "我低血糖发作了,出冷汗心慌手抖",
    ]
    for t in tests:
        red, subs, mode = scan(t)
        print(f"\nQ: {t}\n  mode={mode} red={[f['trigger'] for f in red]} cat={subs[0]['category'] if subs else None} intent={subs[0]['intent'] if subs else None}")