Explorar el Código

feat(S-cfc-466): 实现 food.rule.diseaseWeightGroup 病症权重分组计算宿主,红单测全部转绿

Sisyphus hace 2 horas
padre
commit
ba681d7139

+ 178 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/FoodDiseaseWeightGroup.java

@@ -0,0 +1,178 @@
+package com.etotem.cfc.service;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.LinkedHashMap;
+import java.util.Locale;
+import java.util.Map;
+
+/**
+ * 病症权重分组计算(能力键 {@code food.rule.diseaseWeightGroup},CR-05 / §4.5 / §4.2 G-2)。
+ *
+ * <p>需求背景:健康问卷/报告检出某病症 → 命中对应 {@code disease_code} 分组 → 该分组权重覆盖通用权重;
+ * {@code food_constraint} 作为附加约束。例:高尿酸人群 {@code base_report 0.5→0.4}、
+ * {@code disease_group 0.1→0.3},嘌呤 &gt; 150mg/100g 的食材额外扣 20 分。</p>
+ *
+ * <p>与 {@link FoodRecommendWeightEngine} 同风格——纯 Java 计算服务,不依赖 Spring / DB / 外部服务,
+ * 可由加权链路直接调用。规则表 {@code food_disease_weight_group} 的后台 CRUD/灰度由独立能力键
+ * {@code ops.foodRuleManagement} 承载(见 {@link FoodRuleManagementService});本类承载命中分组后的
+ * 计算口径,作为 w6({@code disease_group_adjust})的输入来源。</p>
+ */
+public class FoodDiseaseWeightGroup {
+
+    /** §4.5 高尿酸分组编码。 */
+    public static final String CODE_URIC = "uric";
+    /** §4.5 尿酸组嘌呤阈值(mg/100g):严格大于才触发约束。 */
+    public static final double PURINE_THRESHOLD_MG = 150.0;
+    /** §4.5 尿酸组嘌呤超标额外扣分。 */
+    public static final double PURINE_PENALTY = -20.0;
+    /** nutrients 中嘌呤键(mg/100g)。 */
+    public static final String NUTRIENT_PURINE = "purine";
+
+    /**
+     * 各病症分组显式配置的因子权重(§4.5)。仅列出「显式覆盖通用」的因子:
+     * 高尿酸组覆盖 base_report / disease_group;其余分组仅配置 disease_group,
+     * 未显式配置的因子由 {@link #effectiveWeight} 回退通用权重。
+     */
+    private static final Map<String, Map<String, Double>> GROUP_WEIGHTS;
+
+    /** 健康问卷/报告病症标签 → disease_code 别名表(中文包含匹配,英文忽略大小写)。 */
+    private static final Map<String, String> CONDITION_ALIASES;
+
+    static {
+        Map<String, Map<String, Double>> groups = new LinkedHashMap<String, Map<String, Double>>();
+
+        Map<String, Double> uric = new LinkedHashMap<String, Double>();
+        uric.put(FoodRecommendWeightEngine.FACTOR_BASE_REPORT, 0.4);
+        uric.put(FoodRecommendWeightEngine.FACTOR_DISEASE_GROUP, 0.3);
+        groups.put(CODE_URIC, uric);
+
+        String[] otherCodes = {"diabetes", "hypertension", "hyperlipidemia", "allergy"};
+        for (String code : otherCodes) {
+            Map<String, Double> weights = new LinkedHashMap<String, Double>();
+            weights.put(FoodRecommendWeightEngine.FACTOR_DISEASE_GROUP, 0.2);
+            groups.put(code, weights);
+        }
+        GROUP_WEIGHTS = Collections.unmodifiableMap(groups);
+
+        Map<String, String> aliases = new LinkedHashMap<String, String>();
+        aliases.put("高尿酸", CODE_URIC);
+        aliases.put("尿酸", CODE_URIC);
+        aliases.put("痛风", CODE_URIC);
+        aliases.put("uric", CODE_URIC);
+        aliases.put("hyperuricemia", CODE_URIC);
+        aliases.put("gout", CODE_URIC);
+
+        aliases.put("糖尿病", "diabetes");
+        aliases.put("血糖", "diabetes");
+        aliases.put("diabetes", "diabetes");
+
+        aliases.put("高血压", "hypertension");
+        aliases.put("血压", "hypertension");
+        aliases.put("hypertension", "hypertension");
+
+        aliases.put("高血脂", "hyperlipidemia");
+        aliases.put("血脂", "hyperlipidemia");
+        aliases.put("高脂血症", "hyperlipidemia");
+        aliases.put("hyperlipidemia", "hyperlipidemia");
+
+        aliases.put("过敏", "allergy");
+        aliases.put("allergy", "allergy");
+        CONDITION_ALIASES = Collections.unmodifiableMap(aliases);
+    }
+
+    /**
+     * 从健康问卷/报告检出的病症标签集合中命中 disease_code(§4.5)。
+     * 按分组优先级顺序匹配以规避 HashSet 迭代顺序影响;未命中返回 null(通用,无分组)。
+     */
+    public String resolveDiseaseCode(Collection<String> detectedConditions) {
+        if (detectedConditions == null || detectedConditions.isEmpty()) {
+            return null;
+        }
+        for (String code : GROUP_WEIGHTS.keySet()) {
+            for (String condition : detectedConditions) {
+                if (condition == null) {
+                    continue;
+                }
+                String normalized = condition.trim();
+                if (normalized.isEmpty()) {
+                    continue;
+                }
+                for (Map.Entry<String, String> alias : CONDITION_ALIASES.entrySet()) {
+                    if (!code.equals(alias.getValue())) {
+                        continue;
+                    }
+                    String keyword = alias.getKey();
+                    boolean hit = isLatin(keyword)
+                            ? keyword.equalsIgnoreCase(normalized)
+                            : normalized.contains(keyword);
+                    if (hit) {
+                        return code;
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    /** 该分组显式配置的 factor_type → weight 权重表(§4.5);未配置分组返回空表。 */
+    public Map<String, Double> groupWeights(String diseaseCode) {
+        if (diseaseCode == null) {
+            return Collections.emptyMap();
+        }
+        Map<String, Double> weights = GROUP_WEIGHTS.get(normalizeCode(diseaseCode));
+        return weights == null ? Collections.emptyMap() : weights;
+    }
+
+    /**
+     * 命中分组且该因子显式配置 → 返回分组权重(覆盖通用);否则返回 generalWeight(§4.5 / G-2)。
+     */
+    public double effectiveWeight(String diseaseCode, String factorType, double generalWeight) {
+        Double override = groupWeights(diseaseCode).get(factorType);
+        return override == null ? generalWeight : override.doubleValue();
+    }
+
+    /** 附加约束调整值:尿酸组嘌呤 &gt; 150mg/100g → -20;否则 0(§4.5 严格大于阈值)。 */
+    public double foodConstraintAdjust(String diseaseCode, double purineMgPer100g) {
+        if (isCode(diseaseCode, CODE_URIC) && purineMgPer100g > PURINE_THRESHOLD_MG) {
+            return PURINE_PENALTY;
+        }
+        return 0.0;
+    }
+
+    /**
+     * 产出喂给加权引擎 w6 的 {@code disease_group_adjust}(§4.2 / G-2):
+     * 命中分组的附加约束调整值,clamp 到 [-100,100];无分组/无约束返回 0。
+     * nutrients 含键 {@code purine}(mg/100g)。
+     */
+    public double diseaseGroupAdjust(String diseaseCode, Map<String, Double> nutrients) {
+        if (diseaseCode == null || nutrients == null) {
+            return 0.0;
+        }
+        Double purine = nutrients.get(NUTRIENT_PURINE);
+        double raw = foodConstraintAdjust(diseaseCode, purine == null ? 0.0 : purine.doubleValue());
+        return FoodRecommendWeightEngine.clamp(raw);
+    }
+
+    /** 分组 enabled 灰度开关:已配置分组视为启用(默认开),未配置分组返回 false(§4.5)。 */
+    public boolean isGroupEnabled(String diseaseCode) {
+        return diseaseCode != null && GROUP_WEIGHTS.containsKey(normalizeCode(diseaseCode));
+    }
+
+    private static String normalizeCode(String code) {
+        return code.trim().toLowerCase(Locale.ROOT);
+    }
+
+    private static boolean isCode(String actual, String expected) {
+        return actual != null && expected.equalsIgnoreCase(actual.trim());
+    }
+
+    private static boolean isLatin(String s) {
+        for (int i = 0; i < s.length(); i++) {
+            if (s.charAt(i) > 127) {
+                return false;
+            }
+        }
+        return true;
+    }
+}

+ 18 - 0
cfc-backend/src/main/java/com/etotem/cfc/service/FoodRecommendWeightEngine.java

@@ -37,6 +37,24 @@ public class FoodRecommendWeightEngine {
     /** §4.2 factor_type 枚举:病症分组调整因子。 */
     public static final String FACTOR_DISEASE_GROUP = "disease_group";
 
+    /** §4.5 病症权重分组计算宿主(food.rule.diseaseWeightGroup),供给 w6 输入。 */
+    private static final FoodDiseaseWeightGroup DISEASE_WEIGHT_GROUP = new FoodDiseaseWeightGroup();
+
+    /**
+     * 按命中病症分组计算喂给 §4.2 w6 的 {@code disease_group_adjust}(§4.5 / G-2)。
+     * 未检出病症(diseaseCode=null)或无附加约束时返回 0,结果 clamp 到 [-100,100]。
+     */
+    public static double diseaseGroupAdjustFor(String diseaseCode, Map<String, Double> nutrients) {
+        return DISEASE_WEIGHT_GROUP.diseaseGroupAdjust(diseaseCode, nutrients);
+    }
+
+    /**
+     * 按病症分组解析因子有效权重:命中分组且该因子显式配置时覆盖通用权重,否则回退通用(§4.5 / G-2)。
+     */
+    public static double effectiveFactorWeight(String diseaseCode, String factorType, double generalWeight) {
+        return DISEASE_WEIGHT_GROUP.effectiveWeight(diseaseCode, factorType, generalWeight);
+    }
+
     /** final_score 下界(§4.2)。 */
     private static final double SCORE_MIN = -100.0;
     /** final_score 上界(§4.2)。 */