Expand description
Layer taxonomy for dictionary entries.
Every (code, word) belongs to exactly one Layer determined at build
time. The layer feeds two purposes:
- Coarse ordering —
LAYER_BASEgives each layer a numeric base weight so 一级简码 always outranks any 二级简码, etc., regardless of per-entry frequency. - User-tunable preference —
WubiDict::set_layer_preflets the host multiply a layer’s contribution at lookup time without touching data.
Index values pack (layer << FREQ_BITS) | freq_score so the runtime can
read both in one stream pass. freq_score is the corpus-derived frequency
from data/weights/weights.tsv (capped at MAX_FREQ_SCORE; real data
tops out around 50k), normalized within layer.
Enums§
- Layer
- Discriminants are ascending priority:
Auto = 0is lowest,Jianma1is highest. This makes the packed(layer << FREQ_BITS) | freqcompare correctly with rawu64ordering — higher u64 = higher priority — so the build-time merge step can keep the larger value on collision without special casing.
Constants§
- DEFAULT_
LAYER_ PREFS - Default
layer_prefs, indexed byLayer as usize.Autois dampened to 0.7 so extension characters don’t pollute the top of common 4-letter codes; everything else is 1.0. - LAYER_
BASE - Per-layer base weight, indexed by
Layer as usize(ascending). Values are spaced so that any in-layer frequency score (capped well below the gap) cannot reorder layers, but a sufficientlayer_prefmultiplier can. - LAYER_
COUNT - Total number of layers. Acts as the array length for
LAYER_BASE,DEFAULT_LAYER_PREFS, and any per-layer table the host might keep. - MAX_
FREQ_ SCORE - Largest
freq_scorethe packed value can represent (2^FREQ_BITS - 1). The single source of truth for the freq domain — tests and any caller that needs to clamp/validate import this rather than hardcoding a copy (a stale copy in the proptest survived the E1FREQ_BITS56→20 change and silently broke the invariants until proptest caught it).
Functions§
- pack
- Pack
(layer, freq_score)into a single u64 index value.freq_scoreis saturated toMAX_FREQ_SCOREif it exceeds the field, never wrapped: for an order-by-value structure a wraparound would invert priority (a very high freq would pack to a tiny value and rank last), whereas clamping keeps “higher freq → higher-or-equal priority”. Real freqs (≤ ~50k) are far inside the field, so this only matters as a defensive guarantee. - unpack
- Reverse of
pack. Unknown layer bytes fall back toLayer::Auto(lowest priority) — preferable to panicking on a corrupt FST.