2024-03-26 01:04:21 +01:00
|
|
|
// other implementations like calculating text_distance on all titles took too much time
|
|
|
|
|
// we keep it now as simple as possible and less memory intensive.
|
|
|
|
|
pub (crate) fn get_title_group(text: &str) -> String {
|
|
|
|
|
let alphabetic_only: String = text.chars().map(|c| if c.is_alphanumeric() { c } else { ' ' }).collect();
|
|
|
|
|
let parts = alphabetic_only.split_whitespace();
|
2024-05-10 10:13:50 +02:00
|
|
|
let mut combination = String::new();
|
|
|
|
|
for p in parts {
|
|
|
|
|
combination = format!("{combination} {p}").trim().to_string();
|
2024-03-26 01:04:21 +01:00
|
|
|
if combination.len() > 2 {
|
|
|
|
|
return combination;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
text.to_string()
|
|
|
|
|
}
|
2024-09-18 19:50:38 +02:00
|
|
|
|
|
|
|
|
pub(crate) trait Capitalize {
|
|
|
|
|
fn capitalize(&self) -> String;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implement the Capitalize trait for &str
|
|
|
|
|
impl Capitalize for &str {
|
|
|
|
|
fn capitalize(&self) -> String {
|
|
|
|
|
let mut chars = self.chars();
|
|
|
|
|
match chars.next() {
|
|
|
|
|
None => String::new(), // Handle empty string case
|
|
|
|
|
Some(first_char) => first_char.to_uppercase().collect::<String>() + chars.as_str(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Implement the trait for String as well
|
|
|
|
|
impl Capitalize for String {
|
|
|
|
|
fn capitalize(&self) -> String {
|
|
|
|
|
self.as_str().capitalize() // Reuse the &str implementation
|
|
|
|
|
}
|
|
|
|
|
}
|