pub struct CharTokenCounter;Expand description
Default counter: CJK characters count as 1 token each, other characters count as 1 token per 4 characters (rounded up).
The common chars/4 approximation underestimates Chinese by roughly 4x (Chinese is roughly 1 token per character), so this implementation distinguishes CJK to stay closer to reality; it is still an approximation — inject a custom implementation when exact counts are needed.
§Example
use molo::memory::{CharTokenCounter, TokenCounter};
let counter = CharTokenCounter;
// 5 characters → 2 tokens (1 token per 4 characters, rounded up).
assert_eq!(counter.count("hello").await?, 2);
// 4 characters → exactly 1 token.
assert_eq!(counter.count("abcd").await?, 1);
// 7 characters → 2 tokens (rounded up).
assert_eq!(counter.count("morning").await?, 2);Trait Implementations§
Source§impl Clone for CharTokenCounter
impl Clone for CharTokenCounter
impl Copy for CharTokenCounter
Source§impl Debug for CharTokenCounter
impl Debug for CharTokenCounter
Source§impl Default for CharTokenCounter
impl Default for CharTokenCounter
impl Eq for CharTokenCounter
Source§impl PartialEq for CharTokenCounter
impl PartialEq for CharTokenCounter
impl StructuralPartialEq for CharTokenCounter
Source§impl TokenCounter for CharTokenCounter
impl TokenCounter for CharTokenCounter
Auto Trait Implementations§
impl Freeze for CharTokenCounter
impl RefUnwindSafe for CharTokenCounter
impl Send for CharTokenCounter
impl Sync for CharTokenCounter
impl Unpin for CharTokenCounter
impl UnsafeUnpin for CharTokenCounter
impl UnwindSafe for CharTokenCounter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key and return true if they are equal.