Skip to content

Commit

Permalink
docs: Add docs for some modules
Browse files Browse the repository at this point in the history
  • Loading branch information
kanru committed Jul 15, 2024
1 parent b31ac2a commit 39875af
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 45 deletions.
6 changes: 3 additions & 3 deletions src/conversion/chewing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,21 @@ use crate::dictionary::{Dictionary, LookupStrategy, Phrase};

use super::{Composition, ConversionEngine, Gap, Interval, Symbol};

/// TODO: doc
/// The default Chewing conversion method.
#[derive(Debug, Default)]
pub struct ChewingEngine {
pub(crate) lookup_strategy: LookupStrategy,
}

impl ChewingEngine {
const MAX_OUT_PATHS: usize = 100;
/// TODO: doc
/// Creates a new conversion engine.
pub fn new() -> ChewingEngine {
ChewingEngine {
lookup_strategy: LookupStrategy::Standard,
}
}
pub fn convert<'a>(
pub(crate) fn convert<'a>(
&'a self,
dict: &'a dyn Dictionary,
comp: &'a Composition,
Expand Down
3 changes: 2 additions & 1 deletion src/conversion/fuzzy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ use crate::dictionary::LookupStrategy;

use super::{ChewingEngine, ConversionEngine};

/// TODO: doc
/// Same conversion method as Chewing but uses fuzzy phrase search.
#[derive(Debug, Default)]
pub struct FuzzyChewingEngine {
inner: ChewingEngine,
}

impl FuzzyChewingEngine {
/// Creates a new conversion engine.
pub fn new() -> FuzzyChewingEngine {
FuzzyChewingEngine {
inner: ChewingEngine {
Expand Down
31 changes: 20 additions & 11 deletions src/conversion/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ pub use self::fuzzy::FuzzyChewingEngine;
pub use self::simple::SimpleEngine;
pub(crate) use self::symbol::{full_width_symbol_input, special_symbol_input};

/// Converts a composition buffer to list of intervals.
///
/// [`Composition`] contains all user inputs and selection information. The out
/// put intervals should cover the whole range of inputs, sorted in first in
/// first out order.
pub trait ConversionEngine: Debug {
fn convert<'a>(
&'a self,
Expand All @@ -29,16 +34,18 @@ pub trait ConversionEngine: Debug {
) -> Box<dyn Iterator<Item = Vec<Interval>> + 'a>;
}

/// TODO: doc
/// Output of conversion.
///
/// Interval represents a segment of input buffer converted to a phrase.
#[derive(Default, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
pub struct Interval {
/// TODO: doc
/// The starting offset of the interval.
pub start: usize,
/// TODO: doc
/// The end (exclusive) of the interval.
pub end: usize,
// TODO doc
/// Whether the output is a phrase from dictionary or just symbols.
pub is_phrase: bool,
/// TODO: doc
/// The output string.
pub str: Box<str>,
}

Expand All @@ -52,7 +59,7 @@ impl Debug for Interval {
}

impl Interval {
/// TODO: doc
/// Whether the interval covers the whole range of the other interval.
pub fn contains(&self, other: &Interval) -> bool {
self.contains_range(other.start, other.end)
}
Expand All @@ -62,17 +69,18 @@ impl Interval {
fn is_contained_by(&self, start: usize, end: usize) -> bool {
start <= self.start && end >= self.end
}
/// Whether the interval covers the part of the other interval.
pub fn intersect(&self, other: &Interval) -> bool {
self.intersect_range(other.start, other.end)
}
fn intersect_range(&self, start: usize, end: usize) -> bool {
max(self.start, start) < min(self.end, end)
}
/// TODO: doc
/// The length of the interval.
pub fn len(&self) -> usize {
self.end - self.start
}
/// TODO: doc
/// Whether the interval is empty (no output).
pub fn is_empty(&self) -> bool {
self.len() == 0
}
Expand Down Expand Up @@ -166,13 +174,14 @@ impl SyllableSlice for Vec<Symbol> {
}
}

/// TODO: doc
/// Input data collected by the Editor.
#[derive(Debug, Default, Clone)]
pub struct Composition {
/// TODO: doc
/// Pre-edit inputs either syllables or symbols.
symbols: Vec<Symbol>,
/// User indicates offset that shouldn't form a phrase.
gaps: Vec<Gap>,
/// TODO: doc
/// User set constraint on that output must match.
selections: Vec<Interval>,
}

Expand Down
15 changes: 15 additions & 0 deletions src/dictionary/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const UD_MEM_FILE_NAME: &str = ":memory:";
const ABBREV_FILE_NAME: &str = "swkb.dat";
const SYMBOLS_FILE_NAME: &str = "symbols.dat";

/// Automatically searchs and loads system dictionaries.
#[derive(Debug, Default)]
pub struct SystemDictionaryLoader {
sys_path: Option<String>,
Expand Down Expand Up @@ -54,13 +55,18 @@ fn io_err(err: io::Error) -> LoadDictionaryError {
}

impl SystemDictionaryLoader {
/// Creates a new system dictionary loader.
pub fn new() -> SystemDictionaryLoader {
SystemDictionaryLoader::default()
}
/// Override the default system dictionary search path.
pub fn sys_path(mut self, path: impl Into<String>) -> SystemDictionaryLoader {
self.sys_path = Some(path.into());
self
}
/// Searches and loads the system dictionaries and extra dictionaries.
///
/// If no dictionary were found, a builtn minimum dictionary will be loaded.
pub fn load(&self) -> Result<Vec<Box<dyn Dictionary>>, LoadDictionaryError> {
let search_path = if let Some(sys_path) = &self.sys_path {
sys_path.to_owned()
Expand Down Expand Up @@ -93,6 +99,7 @@ impl SystemDictionaryLoader {

Ok(results)
}
/// Loads the abbrev table.
pub fn load_abbrev(&self) -> Result<AbbrevTable, LoadDictionaryError> {
let search_path = if let Some(sys_path) = &self.sys_path {
sys_path.to_owned()
Expand All @@ -105,6 +112,7 @@ impl SystemDictionaryLoader {
info!("Loading {ABBREV_FILE_NAME}");
AbbrevTable::open(abbrev_path).map_err(io_err)
}
/// Loads the symbol table.
pub fn load_symbol_selector(&self) -> Result<SymbolSelector, LoadDictionaryError> {
let search_path = if let Some(sys_path) = &self.sys_path {
sys_path.to_owned()
Expand All @@ -119,19 +127,26 @@ impl SystemDictionaryLoader {
}
}

/// Automatically searches and loads the user dictionary.
#[derive(Debug, Default)]
pub struct UserDictionaryLoader {
data_path: Option<PathBuf>,
}

impl UserDictionaryLoader {
/// Creates a user dictionary loader.
pub fn new() -> UserDictionaryLoader {
UserDictionaryLoader::default()
}
/// Override the default user dictionary search path.
pub fn userphrase_path(mut self, path: impl AsRef<Path>) -> UserDictionaryLoader {
self.data_path = Some(path.as_ref().to_path_buf());
self
}
/// Searches and loads the user dictionary.
///
/// If no user dictionary were found, a new dictionary will be created at
/// the default path.
pub fn load(self) -> io::Result<Box<dyn Dictionary>> {
let data_path = self
.data_path
Expand Down
2 changes: 1 addition & 1 deletion src/dictionary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ pub trait DictionaryMut: Debug {
) -> Result<(), UpdateDictionaryError>;
}

/// TODO: doc
/// Errors during dictionary construction.
#[derive(Debug)]
pub struct BuildDictionaryError {
source: Box<dyn Error + Send + Sync>,
Expand Down
3 changes: 3 additions & 0 deletions src/dictionary/trie_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::{
LookupStrategy, Phrase, Trie, TrieBuilder, UpdateDictionaryError,
};

/// A mutable dictionary backed by a Trie and a BTreeMap.
#[derive(Debug)]
pub struct TrieBuf {
trie: Option<Trie>,
Expand All @@ -35,6 +36,7 @@ fn software_version() -> String {
}

impl TrieBuf {
/// Open the target Trie dictionary and wrap it create a TrieBuf.
pub fn open<P: Into<PathBuf>>(path: P) -> io::Result<TrieBuf> {
let path = path.into();
if !path.exists() {
Expand Down Expand Up @@ -63,6 +65,7 @@ impl TrieBuf {
})
}

/// Creates a pure in memory dictionary.
pub fn new_in_memory() -> TrieBuf {
TrieBuf {
trie: None,
Expand Down
18 changes: 12 additions & 6 deletions src/editor/estimate.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
use crate::dictionary::{Dictionary, Phrase};

/// TODO: doc
/// Estimates new user phrase frequency.
///
/// Use UserFreqEstimate to keep track of the time passed and use the original
/// frequency and time to calculate the new frequency of user phrases.
pub trait UserFreqEstimate {
/// TODO: doc
/// Increments the time passed.
///
/// This should be called for every user interaction.
fn tick(&mut self);
/// TODO: doc
/// Returns the current time in ticks.
fn now(&self) -> u64;
/// TODO: doc
/// Returns the estimated new user phrase frequency.
fn estimate(&self, phrase: &Phrase, orig_freq: u32, max_freq: u32) -> u32;
}

/// TODO: doc
/// Loosely tracks time without persisting to disk.
#[derive(Debug)]
pub struct LaxUserFreqEstimate {
lifetime: u64,
}

impl LaxUserFreqEstimate {
/// TODO: doc
/// Initialize with the last time value from the user dictionary.
pub fn max_from(user_dict: &dyn Dictionary) -> LaxUserFreqEstimate {
let lifetime = user_dict
.entries()
Expand All @@ -27,6 +32,7 @@ impl LaxUserFreqEstimate {
LaxUserFreqEstimate { lifetime }
}

/// Creates a new LaxUserFreqEstimate from a initial epoch.
pub fn new(initial_lifetime: u64) -> LaxUserFreqEstimate {
LaxUserFreqEstimate {
lifetime: initial_lifetime,
Expand Down
Loading

0 comments on commit 39875af

Please sign in to comment.