# Desensitization Rules

This is the canonical rule set the skill enforces. The implementation lives in [../scripts/desensitize_content.py](../scripts/desensitize_content.py). When this document and the code disagree, the code wins — but please update both.

## Goal

Let a reader see **direction and momentum** (同比 / 环比 / pp / 增速 / 涨幅) without learning the **absolute magnitude** (亿 / 元 / 件 / 单 / 方 / 人 / 分 / 倍).

## Scope

Apply to:

-   Paragraph (`p`) text
-   Block-quote / highlight-block / callout text
-   Table cell text, including tables inside folded blocks

Do **not** apply to:

-   Node `type`, `id`, `width`, `height`, `align`, `backgroundColor`
-   Table merge metadata, row/column counts, alignment marks
-   Block-quote `emoji` (打灯) and other format-control fields

## Text rule

For every numeric run found by `(?P<sign>[+\-±])?(?P<num>\d{1,3}(?:,\d{3})+(?:\.\d+)?|\d+(?:\.\d+)?)`:

| Condition                                                                                                                                                                  | Decision                                                             | Example                                                                                                                               |
| -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------------- |
| Trailing `pp` / `pct`                                                                                                                                                      | KEEP (with suffix)                                                   | `18.0pp` → `18.0pp`                                                                                                                   |
| Within 6 chars after `同比 / 环比 / YOY / 增速 / 增长率 / 降幅 / 涨幅 / 增幅 / 差距 / 改善 / 恶化 / 节降`                                                                  | KEEP (with `%` if present)                                           | `同比增长 64.78%` → keep                                                                                                              |
| Inside a proper-noun token (`零售211`, `98寸`, `Q3`, `H1`, `25M5`/`26M6`/`25M12` year-month, `9月`, `2025年`, `4G/5G/3C/2B/2C`, `工业4`, `100分` score, list-marker `1、`) | KEEP                                                                 | `零售211订单占比` → keep `211`; `25M5环比` → keep `25M5`; `1、一线现场执行标准管控` → keep `1`                                        |
| Pre-pass — signed magnitude immediately followed by a paren-delta `…（±N…）`                                                                                               | BLANK outer value (sign + digits) but keep unit and keep paren chunk | `-2.4亿（+0.5亿）` → `XX亿（+0.5亿）`                                                                                                 |
| Number with no sign immediately followed by paren-delta `…（±N…）`                                                                                                         | BLANK with unit (drop `%`), keep paren chunk                         | `2.30次（-1.5%）` → `XX次（-1.5%）`; `日均167万单（-2.9%）` → `日均XX万单（-2.9%）`; `拉直率60.3%（+8.48pp）` → `拉直率XX（+8.48pp）` |
| Has sign `+`, `-`, `±` (standalone delta, not followed by paren-delta)                                                                                                     | KEEP                                                                 | `+2,070万`, `-0.5pp`, `-4.5%`                                                                                                         |
| Otherwise                                                                                                                                                                  | BLANK with adjacent unit                                             | `0.7亿` → `XX亿`, `57.80%` → `XX`, `194.3元` → `XX元`, `48单/人/天` → `XX单/人/天`                                                    |

The `%` sign is dropped together with the value when blanking (matches the user's `57.80%` → `XX` example).

### Worked examples

| Original                                                                                                  | Desensitized                                                                                           |
| --------------------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------ |
| `零售211订单占比 57.80%（-0.5pp）`                                                                        | `零售211订单占比 XX（-0.5pp）`                                                                         |
| `商务仓收入0.7亿，增速-1.0%（环比-15.8%），26年首次负增长（3月14.5%，1-2月10.7%）`                        | `商务仓收入XX亿，增速-1.0%（环比-15.8%），26年首次负增长（3月14.5%，1-2月10.7%）`                      |
| `4月重点策略收入1.4亿，同比增长2,070万，达成增收1,740万目标要求`                                          | `4月重点策略收入XX亿，同比增长2,070万，达成增收1,740万目标要求`                                        |
| `人效（35.91方/人/天）同比-5.1%：与目标差距15.1pp，在编人数涨幅（+16.3%，+480人）高于业务量涨幅（+8.9%）` | `人效（XX方/人/天）同比-5.1%：与目标差距15.1pp，在编人数涨幅（+16.3%，+480人）高于业务量涨幅（+8.9%）` |
| `大件仓配一体 -0.002亿（-0.004亿）`                                                                       | `大件仓配一体 XX亿（-0.004亿）`                                                                        |
| `非时效业务-2.4亿（+0.5亿）`                                                                              | `非时效业务XX亿（+0.5亿）`                                                                             |
| `2.30次（-1.5%）`                                                                                         | `XX次（-1.5%）`                                                                                        |
| `拉直率60.3%（+8.48pp）`                                                                                  | `拉直率XX（+8.48pp）`                                                                                  |
| `日均167万单（-2.9%）`                                                                                    | `日均XX万单（-2.9%）`                                                                                  |
| `自营：1.87（-4.4%）`                                                                                     | `自营：XX（-4.4%）`                                                                                    |
| `1、一线现场执行标准管控`                                                                                 | `1、一线现场执行标准管控` _(list marker preserved)_                                                    |
| `考核得分100分`                                                                                           | `考核得分100分` _(100 分 is a score, proper noun)_                                                     |

## Table rule

Two-step classification on every `table` node:

1. **Column classification** (scan up to the first 3 leading rows that are header-like — pure-text cells, or month/period tags like `M5` / `Q3` / `9月` / `25M5`. A row stops counting as a header tier as soon as it contains a mixed text-and-digit cell like `5.47元/单（-6.7%）`. Merge per-column kinds across all header tiers using **deepest-tier-wins**: a non-`unknown` signal in a later (deeper) tier overrides any earlier tier for that column. This is critical for tables with a broad category-group row at the top — e.g. row 0 `体验 / 增长 / 利润`, row 1 ` / 签约收入 / 管报签约利润率`, row 2 `绝对值 / 同比 / 绝对值 / 同比`. A naive `delta > absolute > unknown` merge would lock columns to `delta` because row 0's `增长` matches the delta keyword set, and never see row 2's specific `绝对值`. Deepest-tier-wins lets the tier-3 `绝对值 / 同比` labels win for the value cells directly below them. It still handles the simpler two-tier `M5 / M5 / M6 / M6` over `本期 / 同比 / 本期 / 同比` case the same way):
    - Cell contains `同比 / 环比 / YOY / 增速 / 增长 / 降幅 / 涨幅 / 同比% / 排名 / 得分 / 差距 / 占比 / pp / pct / %` → `delta`
    - Cell contains `绝对值 / 原始值 / 金额 / 数值 / 单位 / 本期 / 目标 / 实际 / 今年 / 去年 / 件数 / 单量` or matches `^M\d+$ / ^Q[1-4]$ / ^H[12]$ / ^\d{1,2}月$` → `absolute`
    - Otherwise → `unknown`
2. **Row classification**: if cells[0..1] of a body row contain `同比 / 环比 / YOY / 增速 / 增长率 / 降幅 / 涨幅 / 增幅`, treat all value cells in that row as delta (overrides column).

Then for each body cell, **in this precedence order** (first match wins):

1. `delta` column → keep verbatim (every number is a delta by construction, including compound forms like `+12.5%（-0.5pp）`).
2. `delta` row (cell index ≥ 1) → keep verbatim, same reason.
3. Compound cell with paren-delta (`5.47元/单（-6.7%）`) in an `absolute` or `unknown` column → run the text rule so the outer magnitude is blanked while the paren chunk survives.
4. `absolute` column → blank numeric values (`_blank_cell_inplace`). This blanks even cells that _look_ like deltas on the surface (e.g. `-34.5%` sitting under a `绝对值` header — that's a percentage value being reported as an absolute level, not a YoY change).
5. `unknown` column → run the text rule.

The compound-cell rule must come AFTER the delta-column/delta-row checks — otherwise a cell like `+12.5%（-0.5pp）` sitting in a `同比` column would incorrectly trigger the compound branch.

## Format preservation

The implementation walks the JSON tree and rewrites only `text` leaves. It never:

-   Reorders cells / rows / blocks
-   Drops empty `text: ""` leaves (which carry formatting state in JoySpace's Slate-style schema)
-   Strips `bold`, `italic`, `code`, `underline`, `backgroundColor`, `color`, `align`, `header` marks
-   Touches `block-quote` emojis, `highlight-block` background colors, column `width` arrays, or `height` values

This is why the output, when re-rendered by JoySpace, looks identical to the source page except for the substituted numbers.

## Tuning

If specific terms are misclassified for a particular corpus, edit the keyword lists at the top of [../scripts/desensitize_content.py](../scripts/desensitize_content.py):

-   `_DELTA_PREFIX_KEYWORDS` — words that, when seen just before a number, mark it as a delta.
-   `PROPER_NOUN_PATTERNS` — verbatim-keep regexes (industry terms / model names with digits).
-   `ABSOLUTE_HEADER_TOKENS` / `DELTA_HEADER_TOKENS` — table-header column classifiers.
-   `_ABSOLUTE_UNIT_RE` — units that travel with the blanked value (e.g. `亿`, `元`, `方/人/天`).

After tuning, re-run `desensitize_pipeline.py --dry-run --keep-intermediate` and diff the resulting `*_raw.md` / `*_desens.md` files.
