pub struct CostModel { /* private fields */ }Expand description
Costs of the four edit operations.
Built for a keyboard Layout with CostModel::for_layout
(CostModel::qwerty is the default). The layout only decides which
substitutions are cheap; the minimum costs the search bounds use
(c_min, c_indel_min,
c_transpose_min) do not depend on it.
Implementations§
Source§impl CostModel
impl CostModel
Sourcepub fn qwerty() -> Self
pub fn qwerty() -> Self
The default model with a QWERTY keyboard. Same as
CostModel::for_layout(Layout::Qwerty).
Sourcepub fn for_layout(layout: Layout) -> Self
pub fn for_layout(layout: Layout) -> Self
A model whose cheap substitutions are the neighbouring keys of
layout (Layout::are_neighbours); see Layout for what the
geometry leaves out. Every other pair of characters costs an ordinary
substitution. Does not panic.
§Examples
use keyhammer::cost::{CostModel, Layout};
let qwerty = CostModel::for_layout(Layout::Qwerty);
let azerty = CostModel::for_layout(Layout::Azerty);
// q and z touch on AZERTY, not on QWERTY.
assert_eq!(azerty.sub_cost(b'q', b'z', 1), 8);
assert_eq!(qwerty.sub_cost(b'q', b'z', 1), 16);
// m is next to n on QWERTY only.
assert_eq!(qwerty.sub_cost(b'm', b'n', 1), 8);
assert_eq!(azerty.sub_cost(b'm', b'n', 1), 16);
// ABNT2 has the QWERTY letters, and ç next to l and p.
let abnt2 = CostModel::for_layout(Layout::Abnt2);
assert_eq!(abnt2.sub_cost('ç', 'l', 1), 8);
assert_eq!(qwerty.sub_cost('ç', 'l', 1), 16);
assert_eq!(abnt2.sub_cost('a', 's', 1), qwerty.sub_cost('a', 's', 1));Sourcepub fn sub_cost<S: Into<u32>>(&self, q: S, t: S, qpos: usize) -> Cost
pub fn sub_cost<S: Into<u32>>(&self, q: S, t: S, qpos: usize) -> Cost
Cost of typing q where the term has t; qpos is the index of q in the query.
Symbols are Unicode scalar values: a u8 is read as U+0000 to U+00FF,
a char or a u32 as itself.
Sourcepub fn del_cost<S: PartialEq>(&self, q: &[S], qpos: usize) -> Cost
pub fn del_cost<S: PartialEq>(&self, q: &[S], qpos: usize) -> Cost
Cost of deleting the query symbol at qpos (the user typed an extra one).
Sourcepub fn ins_cost<S: PartialEq>(
&self,
t: S,
prev_t: Option<S>,
qpos: usize,
) -> Cost
pub fn ins_cost<S: PartialEq>( &self, t: S, prev_t: Option<S>, qpos: usize, ) -> Cost
Cost of inserting term symbol t (the user skipped it) when qpos query symbols are consumed.
Sourcepub fn transpose_cost(&self, qpos: usize) -> Cost
pub fn transpose_cost(&self, qpos: usize) -> Cost
Cost of swapping two adjacent symbols, the first at qpos in the query.
Sourcepub fn c_indel_min(&self) -> Cost
pub fn c_indel_min(&self) -> Cost
Smallest cost of an insertion or deletion.
Sourcepub fn c_transpose_min(&self) -> Cost
pub fn c_transpose_min(&self) -> Cost
Smallest cost of a transposition.