Created
October 16, 2023 13:51
-
-
Save mhtocs/1bf31a5054ed9a052ef7df31d812cfad to your computer and use it in GitHub Desktop.
Trigrams
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#[test] | |
fn test_trigram() { | |
let trigrams = trigram("Hello, wor杯ld!"); | |
assert_eq!( | |
trigrams, | |
vec![ | |
"hel", "ell", "llo", "lo,", "o, ", ", w", " wo", "wor", "or杯", "r杯l", "杯ld", "ld!" | |
] | |
); | |
} | |
pub fn trigram(word: &str) -> Vec<String> { | |
let chars: Vec<char> = word.to_lowercase().chars().collect(); | |
if chars.len() < 3 { | |
return vec![]; | |
} | |
let mut trigrams = Vec::with_capacity(chars.len() - 2); | |
for i in 0..chars.len() - 2 { | |
let trigram_str: String = chars[i..i + 3].iter().collect(); | |
trigrams.push(trigram_str); | |
} | |
trigrams | |
} | |
pub fn trigram2(word: &str) -> Vec<(usize, usize)> { | |
if word.len() < 3 { | |
return vec![]; | |
} | |
let mut trigram_indices = Vec::with_capacity(word.len() - 2); | |
for i in 0..word.len() - 2 { | |
let trigram_start = i; | |
let trigram_end = i + 2; | |
trigram_indices.push((trigram_start, trigram_end)); | |
} | |
trigram_indices | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment