Skip to content

Instantly share code, notes, and snippets.

@mhtocs
Created October 16, 2023 13:51
Show Gist options
  • Save mhtocs/1bf31a5054ed9a052ef7df31d812cfad to your computer and use it in GitHub Desktop.
Save mhtocs/1bf31a5054ed9a052ef7df31d812cfad to your computer and use it in GitHub Desktop.
Trigrams
#[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