pub struct AbbrevMap { /* private fields */ }Expand description
Abbreviation lookup and expansion table.
Construct once via AbbrevMap::builtin or AbbrevMap::from_tsv and
reuse across calls.
Implementations§
Source§impl AbbrevMap
impl AbbrevMap
Sourcepub fn builtin() -> Self
pub fn builtin() -> Self
Load the built-in Thai abbreviation dictionary.
Includes month abbreviations (ม.ค.–ธ.ค.), era markers (พ.ศ.,
ค.ศ.), academic titles (ดร., ศ., รศ., ผศ.), and common
administrative abbreviations (จ., ต., ถ., ซ.).
Sourcepub fn from_tsv(data: &str) -> Self
pub fn from_tsv(data: &str) -> Self
Parse a tab-separated abbreviation table.
Format: abbrev\tprimary_expansion[\talt1\talt2…] — one rule per line.
Lines beginning with # and blank lines are skipped.
Duplicate keys are merged: later entries append to the expansion list
and the last duplicate’s first column becomes the primary expansion
used by [expand_text].
Sourcepub fn lookup(&self, abbrev: &str) -> Option<&[String]>
pub fn lookup(&self, abbrev: &str) -> Option<&[String]>
Return all expansions for abbrev, or None if not in the map.
§Examples
use kham_core::abbrev::AbbrevMap;
let map = AbbrevMap::builtin();
let exps = map.lookup("ก.ค.").unwrap();
assert_eq!(exps[0], "กรกฎาคม");
// Ambiguous abbreviation — multiple expansions.
let exps = map.lookup("อ.").unwrap();
assert!(exps.contains(&"อาจารย์".to_string()));
assert_eq!(map.lookup("unknown"), None);Sourcepub fn expand_text(&self, text: &str) -> String
pub fn expand_text(&self, text: &str) -> String
Scan text and replace every occurrence of a known abbreviation key
with its primary expansion (the first expansion in the TSV row).
Matching is greedy and longest-first: at each position the longest matching key is consumed before advancing. Characters that do not start any key are copied through unchanged.
Returns an owned String. Allocates only when at least one replacement
is made; otherwise returns a copy of the input.
§Examples
use kham_core::abbrev::AbbrevMap;
let map = AbbrevMap::builtin();
assert_eq!(map.expand_text("ก.ค."), "กรกฎาคม");
assert_eq!(map.expand_text("5ก.ค.2567"), "5กรกฎาคม2567");
assert_eq!(map.expand_text("ไม่มีอะไร"), "ไม่มีอะไร");
// Compound title: ศ.ดร. matched before ศ.
assert_eq!(map.expand_text("ศ.ดร.สมชาย"), "ศาสตราจารย์ดอกเตอร์สมชาย");