扫全库 wikilinks:单一关系词频次 / 总关系引用 > threshold 则报警。 Args: min_total: 关系词总数 < 此值不报警(避免小样本噪声;5 个关系词以下说明 wiki 还在早期,单一关系词主导是正常起步阶段,不是失衡) threshold: 单关系词占比阈值(默认 0.30 = 30%) 不计入:归档区(_is_exempt)、代码块(mask_code_spans)。 只算 7 个白名单关系词(RELATION_TYPES);非标准词由 list-relation-iss
(pages, *, min_total: int = 5, threshold: float = 0.30)
| 722 | # 与 list-relation-issues 正交:后者查"拼写错 / 非标准词",本 lint 查"频次失衡"。 |
| 723 | |
| 724 | def list_relation_balance(pages, *, min_total: int = 5, threshold: float = 0.30) -> list[dict]: |
| 725 | """扫全库 wikilinks:单一关系词频次 / 总关系引用 > threshold 则报警。 |
| 726 | |
| 727 | Args: |
| 728 | min_total: 关系词总数 < 此值不报警(避免小样本噪声;5 个关系词以下说明 wiki |
| 729 | 还在早期,单一关系词主导是正常起步阶段,不是失衡) |
| 730 | threshold: 单关系词占比阈值(默认 0.30 = 30%) |
| 731 | |
| 732 | 不计入:归档区(_is_exempt)、代码块(mask_code_spans)。 |
| 733 | 只算 7 个白名单关系词(RELATION_TYPES);非标准词由 list-relation-issues 报。 |
| 734 | """ |
| 735 | from collections import Counter |
| 736 | counts: Counter = Counter() |
| 737 | for page in pages: |
| 738 | if _is_exempt(page): |
| 739 | continue |
| 740 | scan_text = mask_code_spans(page.raw_content) |
| 741 | for m in WIKILINK_RE.finditer(scan_text): |
| 742 | _, relation = split_alias_or_relation(m.group(3)) |
| 743 | if relation: |
| 744 | counts[relation] += 1 |
| 745 | total = sum(counts.values()) |
| 746 | if total <= min_total: |
| 747 | return [] |
| 748 | out = [] |
| 749 | for relation, n in counts.most_common(): |
| 750 | ratio = n / total |
| 751 | if ratio > threshold: |
| 752 | out.append({ |
| 753 | "relation": relation, |
| 754 | "count": n, |
| 755 | "ratio": round(ratio, 4), |
| 756 | "threshold": threshold, |
| 757 | "total_relations": total, |
| 758 | "suggestion": _relation_balance_suggestion(relation, ratio), |
| 759 | }) |
| 760 | return out |
| 761 | |
| 762 | |
| 763 | def _relation_balance_suggestion(relation: str, ratio: float) -> str: |
no test coverage detected