Skip to main content

Dictionary

Trait Dictionary 

Source
pub trait Dictionary: Debug {
    // Required methods
    fn lookup(
        &self,
        syllables: &[Syllable],
        strategy: LookupStrategy,
    ) -> Vec<Phrase>;
    fn entries(&self) -> Entries<'_>;
    fn about(&self) -> DictionaryInfo;
    fn path(&self) -> Option<&Path>;
    fn set_usage(&mut self, usage: DictionaryUsage);

    // Provided methods
    fn reopen(&mut self) -> Result<(), UpdateDictionaryError> { ... }
    fn flush(&mut self) -> Result<(), UpdateDictionaryError> { ... }
    fn add_phrase(
        &mut self,
        _syllables: &[Syllable],
        _phrase: Phrase,
    ) -> Result<(), UpdateDictionaryError> { ... }
    fn update_phrase(
        &mut self,
        _syllables: &[Syllable],
        _phrase: Phrase,
        _user_freq: u32,
        _time: u64,
    ) -> Result<(), UpdateDictionaryError> { ... }
    fn remove_phrase(
        &mut self,
        _syllables: &[Syllable],
        _phrase_str: &str,
    ) -> Result<(), UpdateDictionaryError> { ... }
}
Expand description

An interface for looking up dictionaries.

This is the main dictionary trait. For more about the concept of dictionaries generally, please see the module-level documentation.

§Examples


use chewing::{dictionary::{Dictionary, LookupStrategy, TrieBuf}, syl, zhuyin::Bopomofo};

let mut dict = TrieBuf::new_in_memory();
dict.add_phrase(&[syl![Bopomofo::C, Bopomofo::E, Bopomofo::TONE4]], ("測", 100).into())?;

for phrase in dict.lookup(
    &[syl![Bopomofo::C, Bopomofo::E, Bopomofo::TONE4]], LookupStrategy::Standard
) {
    assert_eq!("測", phrase.as_str());
    assert_eq!(100, phrase.freq());
}

Required Methods§

Source

fn lookup( &self, syllables: &[Syllable], strategy: LookupStrategy, ) -> Vec<Phrase>

Returns all phrases matched by the syllables.

The result should use a stable order each time for the same input.

Source

fn entries(&self) -> Entries<'_>

Returns an iterator to all phrases in the dictionary.

Source

fn about(&self) -> DictionaryInfo

Returns information about the dictionary instance.

Source

fn path(&self) -> Option<&Path>

Returns the dictionary file path if it’s backed by a file.

Source

fn set_usage(&mut self, usage: DictionaryUsage)

Set the runtime usage of the dictionary

Provided Methods§

Source

fn reopen(&mut self) -> Result<(), UpdateDictionaryError>

Reopens the dictionary if it was changed by a different process

It should not fail if the dictionary is read-only or able to sync across processes automatically.

Source

fn flush(&mut self) -> Result<(), UpdateDictionaryError>

Flushes all the changes back to the filesystem

The change made to the dictionary might not be persisted without calling this method.

Source

fn add_phrase( &mut self, _syllables: &[Syllable], _phrase: Phrase, ) -> Result<(), UpdateDictionaryError>

An method for updating dictionaries.

For more about the concept of dictionaries generally, please see the module-level documentation.

§Examples

use chewing::{dictionary::{Dictionary, TrieBuf}, syl, zhuyin::Bopomofo};

let mut dict = TrieBuf::new_in_memory();
dict.add_phrase(&[syl![Bopomofo::C, Bopomofo::E, Bopomofo::TONE4]], ("測", 100).into())?;

TODO: doc

Source

fn update_phrase( &mut self, _syllables: &[Syllable], _phrase: Phrase, _user_freq: u32, _time: u64, ) -> Result<(), UpdateDictionaryError>

TODO: doc

Source

fn remove_phrase( &mut self, _syllables: &[Syllable], _phrase_str: &str, ) -> Result<(), UpdateDictionaryError>

TODO: doc

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§

Source§

impl Dictionary for Layered

Source§

impl Dictionary for SqliteDictionary

Available on crate feature sqlite only.
Source§

impl Dictionary for Trie

Source§

impl Dictionary for TrieBuf