use std::collections::{HashMap, HashSet};
pub fn trim(input: &str) -> String {
input.split_whitespace().collect::<Vec<&str>>().join(" ")
}
pub fn keywords(content: &str, max_count: usize, min_length: usize) -> Vec<String> {
let stop_words: HashSet<&str> = [
"a", "an", "and", "are", "as", "at", "be", "by", "for", "from", "has", "he", "in", "is",
"it", "its", "of", "on", "that", "the", "to", "was", "will", "with", "or", "but", "not",
"this", "they", "have", "had", "what", "said", "each", "which", "do", "how", "their", "if",
"up", "out", "many", "then", "them", "these", "so", "some", "her", "would", "make", "like",
"into", "him", "has", "two", "more", "go", "no", "way", "could", "my", "than", "first",
"been", "call", "who", "oil", "sit", "now", "find", "down", "day", "did", "get", "come",
"made", "may", "part",
]
.iter()
.cloned()
.collect();
let mut word_counts: HashMap<String, usize> = HashMap::new();
for word in content
.to_lowercase()
.split_whitespace()
.map(|w| w.trim_matches(|c: char| !c.is_alphanumeric()))
.filter(|w| w.len() >= min_length && !stop_words.contains(w))
{
*word_counts.entry(word.to_string()).or_insert(0) += 1;
}
let mut words: Vec<(String, usize)> = word_counts.into_iter().collect();
words.sort_by(|a, b| b.1.cmp(&a.1));
words
.into_iter()
.take(max_count)
.map(|(word, _)| word)
.collect()
}
pub fn to_slug(input: &str) -> String {
input
.to_lowercase()
.chars()
.map(|c| {
if c.is_alphanumeric() {
c
} else if c.is_whitespace() || c == '-' || c == '_' {
'-'
} else {
' ' }
})
.collect::<String>()
.split_whitespace()
.collect::<Vec<&str>>()
.join("")
.split('-')
.filter(|s| !s.is_empty())
.collect::<Vec<&str>>()
.join("-")
}
pub fn to_camel_case(input: &str) -> String {
let words: Vec<&str> = input.split_whitespace().collect();
if words.is_empty() {
return String::new();
}
let mut result = words[0].to_lowercase();
for word in &words[1..] {
if !word.is_empty() {
let mut chars = word.chars();
if let Some(first) = chars.next() {
result.push(first.to_uppercase().next().unwrap_or(first));
result.push_str(&chars.as_str().to_lowercase());
}
}
}
result
}
pub fn to_pascal_case(input: &str) -> String {
input
.split_whitespace()
.map(|word| {
let mut chars = word.chars();
match chars.next() {
None => String::new(),
Some(first) => {
first.to_uppercase().collect::<String>() + &chars.as_str().to_lowercase()
}
}
})
.collect()
}
pub fn to_snake_case(input: &str) -> String {
input
.to_lowercase()
.split_whitespace()
.collect::<Vec<&str>>()
.join("_")
}
pub fn truncate(input: &str, max_length: usize) -> String {
if input.len() <= max_length {
input.to_string()
} else if max_length <= 3 {
"...".to_string()
} else {
format!("{}...", &input[..max_length - 3])
}
}
pub fn truncate_words(input: &str, max_length: usize) -> String {
if input.len() <= max_length {
return input.to_string();
}
if max_length <= 3 {
return "...".to_string();
}
let mut result = String::new();
let mut current_length = 0;
for word in input.split_whitespace() {
let word_with_space = if result.is_empty() {
word
} else {
&format!(" {}", word)
};
if current_length + word_with_space.len() + 3 > max_length {
break;
}
result.push_str(word_with_space);
current_length += word_with_space.len();
}
if current_length > 0 && current_length < input.len() {
result.push_str("...");
}
result
}
pub fn word_count(input: &str) -> usize {
input.split_whitespace().count()
}
pub fn title_case(input: &str) -> String {
input
.split_whitespace()
.map(|word| {
let mut chars = word.chars();
match chars.next() {
None => String::new(),
Some(first) => first.to_uppercase().collect::<String>() + chars.as_str(),
}
})
.collect::<Vec<String>>()
.join(" ")
}
pub fn strip_html(input: &str) -> String {
let mut result = String::new();
let mut inside_tag = false;
for ch in input.chars() {
match ch {
'<' => inside_tag = true,
'>' => {
inside_tag = false;
if !result.is_empty() && !result.ends_with(' ') {
result.push(' ');
}
}
_ if !inside_tag => result.push(ch),
_ => {}
}
}
trim(&result)
}
pub fn wrap_text(input: &str, width: usize) -> String {
if width == 0 {
return input.to_string();
}
let mut lines = Vec::new();
let mut current_line = String::new();
let mut current_length = 0;
for word in input.split_whitespace() {
let word_len = word.len();
let space_needed = if current_line.is_empty() { 0 } else { 1 };
if current_length + space_needed + word_len > width && !current_line.is_empty() {
lines.push(current_line);
current_line = word.to_string();
current_length = word_len;
} else {
if !current_line.is_empty() {
current_line.push(' ');
current_length += 1;
}
current_line.push_str(word);
current_length += word_len;
}
}
if !current_line.is_empty() {
lines.push(current_line);
}
lines.join("\n")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_trim() {
assert_eq!(trim(" hello world \n\t"), "hello world");
assert_eq!(trim("single"), "single");
assert_eq!(trim(""), "");
assert_eq!(trim(" "), "");
}
#[test]
fn test_extract_keywords() {
let keywords = keywords(
"This is a sample text for testing keywords extraction",
5,
3,
);
assert!(!keywords.contains(&"this".to_string()));
assert!(!keywords.contains(&"is".to_string()));
assert!(!keywords.contains(&"a".to_string()));
assert!(
keywords.contains(&"sample".to_string())
|| keywords.contains(&"text".to_string())
|| keywords.contains(&"testing".to_string())
);
assert!(keywords.len() <= 5);
}
#[test]
fn test_to_slug() {
assert_eq!(
to_slug("Hello World! This is a Test."),
"hello-world-this-is-a-test"
);
assert_eq!(to_slug(" Multiple Spaces "), "multiple-spaces");
assert_eq!(
to_slug("Special-Characters_Here"),
"special-characters-here"
);
assert_eq!(to_slug(""), "");
}
#[test]
fn test_case_conversions() {
assert_eq!(to_camel_case("hello world test"), "helloWorldTest");
assert_eq!(to_pascal_case("hello world test"), "HelloWorldTest");
assert_eq!(to_snake_case("Hello World Test"), "hello_world_test");
assert_eq!(title_case("hello world test"), "Hello World Test");
}
#[test]
fn test_truncate() {
assert_eq!(
truncate("This is a very long string", 15),
"This is a ve..."
);
assert_eq!(truncate("Short", 15), "Short");
assert_eq!(truncate("Test", 3), "...");
assert_eq!(truncate("", 10), "");
}
#[test]
fn test_truncate_words() {
assert_eq!(
truncate_words("This is a very long string", 15),
"This is a..."
);
assert_eq!(truncate_words("Short text", 15), "Short text");
assert_eq!(truncate_words("", 10), "");
}
#[test]
fn test_word_count() {
assert_eq!(word_count("Hello world, this is a test!"), 6);
assert_eq!(word_count("Single"), 1);
assert_eq!(word_count(""), 0);
assert_eq!(word_count(" "), 0);
}
#[test]
fn test_strip_html() {
assert_eq!(
strip_html("<p>Hello <strong>world</strong>!</p>"),
"Hello world !"
);
assert_eq!(strip_html("No HTML here"), "No HTML here");
assert_eq!(
strip_html("<div><span>Nested</span> tags</div>"),
"Nested tags"
);
assert_eq!(strip_html(""), "");
}
#[test]
fn test_wrap_text() {
let wrapped = wrap_text("This is a very long line that needs to be wrapped", 20);
let lines: Vec<&str> = wrapped.split('\n').collect();
for line in &lines {
assert!(line.len() <= 20);
}
let original_words: Vec<&str> = "This is a very long line that needs to be wrapped"
.split_whitespace()
.collect();
let wrapped_words: Vec<&str> = wrapped.split_whitespace().collect();
assert_eq!(original_words, wrapped_words);
}
}