refactor: extract parser module with muncher and for_loops submodules from main.rs
Move Stats processing methods into dedicated parser module with separate muncher and for_loops implementations, update .gitignore to exclude test_books directory, and simplify README to remove outdated benchmark instructions
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
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?
|
||||
/// 500ms is without forbidden words check, but...
|
||||
/// 6000ms if adding forbidden words.. so not faster
|
||||
/// with trie this is 2600ms
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user