Skip to main content

Module layer

Module layer 

Source
Expand description

Layer taxonomy for dictionary entries.

Every (code, word) belongs to exactly one Layer determined at build time. The layer feeds two purposes:

  1. Coarse orderingLAYER_BASE gives each layer a numeric base weight so 一级简码 always outranks any 二级简码, etc., regardless of per-entry frequency.
  2. User-tunable preferenceWubiDict::set_layer_pref lets 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 = 0 is lowest, Jianma1 is highest. This makes the packed (layer << FREQ_BITS) | freq compare correctly with raw u64 ordering — 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 by Layer as usize. Auto is 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 sufficient layer_pref multiplier 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_score the 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 E1 FREQ_BITS 56→20 change and silently broke the invariants until proptest caught it).

Functions§

pack
Pack (layer, freq_score) into a single u64 index value. freq_score is saturated to MAX_FREQ_SCORE if 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 to Layer::Auto (lowest priority) — preferable to panicking on a corrupt FST.