diff options
| author | sigoden <sigoden@gmail.com> | 2024-07-11 12:25:30 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-07-11 12:25:30 +0800 |
| commit | 8e5c17b5543e0e79b1b74d9723f808861f70c162 (patch) | |
| tree | 516ed00e17f83f70fb8075cd8306477c014ada47 /src/utils | |
| parent | 0264ab80ab56d1ae78470c3fe860580b5bdc676a (diff) | |
| download | aichat-8e5c17b5543e0e79b1b74d9723f808861f70c162.tar.gz | |
refactor: improve estimate_token_length (#703)
Diffstat (limited to 'src/utils')
| -rw-r--r-- | src/utils/mod.rs | 32 |
1 files changed, 12 insertions, 20 deletions
diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 35f41a2..aa63f3b 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -23,6 +23,7 @@ use fancy_regex::Regex; use is_terminal::IsTerminal; use lazy_static::lazy_static; use std::{env, path::PathBuf, process}; +use unicode_segmentation::UnicodeSegmentation; lazy_static! { pub static ref CODE_BLOCK_RE: Regex = Regex::new(r"(?ms)```\w*(.*)```").unwrap(); @@ -42,31 +43,22 @@ pub fn get_env_name(key: &str) -> String { ) } -pub fn tokenize(text: &str) -> Vec<&str> { - if text.is_ascii() { - text.split_inclusive(|c: char| c.is_ascii_whitespace()) - .collect() - } else { - unicode_segmentation::UnicodeSegmentation::graphemes(text, true).collect() - } -} - pub fn estimate_token_length(text: &str) -> usize { - let mut token_length: f32 = 0.0; - - for char in text.chars() { - if char.is_ascii() { - if char.is_ascii_alphabetic() { - token_length += 0.25; + let words: Vec<&str> = text.unicode_words().collect(); + let mut output: f32 = 0.0; + for word in words { + if word.is_ascii() { + output += 1.3; + } else { + let count = word.chars().count(); + if count == 1 { + output += 1.0 } else { - token_length += 0.5; + output += (count as f32) * 0.5; } - } else { - token_length += 1.5; } } - - token_length.ceil() as usize + output.ceil() as usize } pub fn light_theme_from_colorfgbg(colorfgbg: &str) -> Option<bool> { |
