Skip to main content

AbbrevMap

Struct AbbrevMap 

Source
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

Source

pub fn empty() -> Self

Create an empty map with no entries.

Source

pub fn builtin() -> Self

Load the built-in Thai abbreviation dictionary.

Includes month abbreviations (ม.ค.ธ.ค.), era markers (พ.ศ., ค.ศ.), academic titles (ดร., ศ., รศ., ผศ.), and common administrative abbreviations (จ., ต., ถ., ซ.).

Source

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].

Source

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);
Source

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("ศ.ดร.สมชาย"), "ศาสตราจารย์ดอกเตอร์สมชาย");
Source

pub fn len(&self) -> usize

Return the number of entries in the map.

Source

pub fn is_empty(&self) -> bool

Return true if the map contains no entries.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.