64 lines
1.5 KiB
Rust
64 lines
1.5 KiB
Rust
use crate::{FORBIDDEN_WORDS, stats::Stats};
|
|
|
|
#[allow(dead_code)]
|
|
/// probably buggy. for example, are new lines sentences? what if the text has no last period?
|
|
pub fn parse(stats: &mut Stats, text: &str) {
|
|
let mut capitalized = true;
|
|
let mut whitespaced = false;
|
|
let mut dotted = false;
|
|
let mut word = String::new();
|
|
for char in text.chars() {
|
|
if whitespaced {
|
|
if !char.is_whitespace() {
|
|
whitespaced = false; //end whiteness
|
|
}
|
|
continue;
|
|
} else if char.is_whitespace() {
|
|
whitespaced = true;
|
|
stats.word_count += 1; //end of word
|
|
if capitalized {
|
|
stats.capitalized_count += 1;
|
|
} else {
|
|
//reset capitalized word
|
|
capitalized = true;
|
|
}
|
|
let lowercase_word = word.to_lowercase();
|
|
if FORBIDDEN_WORDS.contains(&lowercase_word) {
|
|
stats.forbidden_count += 1;
|
|
}
|
|
word = String::new();
|
|
continue;
|
|
}
|
|
if dotted {
|
|
if char != '.' {
|
|
dotted = false; //end sentencing
|
|
}
|
|
continue;
|
|
} else if char == '.' {
|
|
dotted = true;
|
|
stats.sentence_count += 1;
|
|
stats.word_count += 1; //end of word
|
|
if capitalized {
|
|
stats.capitalized_count += 1;
|
|
} else {
|
|
//reset capitalized word
|
|
capitalized = true;
|
|
}
|
|
let lowercase_word = word.to_lowercase();
|
|
if FORBIDDEN_WORDS.contains(&lowercase_word) {
|
|
stats.forbidden_count += 1;
|
|
}
|
|
word = String::new();
|
|
continue;
|
|
}
|
|
word += &char.to_string();
|
|
if char.is_numeric() {
|
|
stats.numeric_count += 1;
|
|
capitalized = false;
|
|
}
|
|
if !char.is_ascii_uppercase() {
|
|
capitalized = false;
|
|
}
|
|
}
|
|
}
|