feat: add rayon parallel processing and refactor get_numbers to accept clean_content slice
- Add rayon dependency to Cargo.toml and import parallel prelude in both isspam_v3.rs and risspam/src/main.rs - Change get_numbers signature to take &str instead of &str and return Vec<&str> instead of Vec<String>, removing unnecessary allocation - Guard word_count_per_sentence calculation against empty sentences list to prevent division by zero - Enable file reading from command-line arguments in isspam_v1.rs by uncommenting and fixing the arg loop with skip(1) - Remove embedded SPAM1 static test data from isspam_v1.rs and create new isspam_v2.rs with additional analysis functions
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#![feature(let_chains)]
|
||||
|
||||
use rayon::prelude::*;
|
||||
//use rayon::prelude::*;
|
||||
use std::{env, fs};
|
||||
|
||||
fn clean_content(content: &str) -> String {
|
||||
@@ -50,11 +52,8 @@ fn get_capitalized_words(content: &str) -> Vec<&str> {
|
||||
cap_words
|
||||
}
|
||||
|
||||
fn get_numbers(content: &str) -> Vec<String> {
|
||||
let clean = clean_content(content);
|
||||
|
||||
clean.split(|c: char| c.is_ascii_digit())
|
||||
.map(|n| n.to_string())
|
||||
fn get_numbers(clean_content: &str) -> Vec<&str> {
|
||||
clean_content.split(|c: char| c.is_ascii_digit())
|
||||
.collect()
|
||||
}
|
||||
|
||||
@@ -72,7 +71,7 @@ fn get_forbidden_words(content: &str) -> Vec<&str> {
|
||||
|
||||
fn analyze(data: &str) {
|
||||
let clean_data = clean_content(data);
|
||||
drop(clean_data); // You aren't actually using clean_data :O
|
||||
// drop(clean_data); // You aren't actually using clean_data :O
|
||||
|
||||
// All capitalized words
|
||||
let cap_words = get_capitalized_words(data);
|
||||
@@ -87,29 +86,47 @@ fn analyze(data: &str) {
|
||||
println!("Words: {}", words.clone().count());
|
||||
|
||||
// Numbers
|
||||
let numbers = get_numbers(data);
|
||||
let numbers = get_numbers(&clean_data);
|
||||
println!("Numbers: {}", numbers.len());
|
||||
|
||||
// Forbidden words
|
||||
let fw = get_forbidden_words(data);
|
||||
println!("Forbidden words: {}", fw.len());
|
||||
|
||||
let word_count_per_sentence = words.count() / sentences.len();
|
||||
println!("Word count per sentence: {}", word_count_per_sentence);
|
||||
if sentences.len() > 0 {
|
||||
let word_count_per_sentence = words.count() / sentences.len();
|
||||
println!("Word count per sentence: {}", word_count_per_sentence);
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
// Read in files from args
|
||||
for arg in env::args().skip(1) { // skip program arg
|
||||
let Ok(text) = fs::read_to_string(&arg) else {
|
||||
eprintln!("{arg} isn't a valid file or couldn't be read");
|
||||
continue;
|
||||
let mut files = Vec::with_capacity(env::args().len());
|
||||
let mut do_parallel = false;
|
||||
|
||||
for arg in env::args().skip(1) { // skip program arg
|
||||
if arg == "-p" {
|
||||
do_parallel = true;
|
||||
} else {
|
||||
files.push(arg);
|
||||
}
|
||||
}
|
||||
|
||||
// Do the work
|
||||
let work = |file| {
|
||||
let Ok(text) = fs::read_to_string(&file) else {
|
||||
eprintln!("{file} isn't a valid file or couldn't be read");
|
||||
return;
|
||||
};
|
||||
|
||||
analyze(&text);
|
||||
}
|
||||
|
||||
// analyze(&SPAM1);
|
||||
};
|
||||
|
||||
if !do_parallel {
|
||||
files.iter().for_each(work);
|
||||
} else {
|
||||
files.par_iter().for_each(work)
|
||||
}
|
||||
}
|
||||
|
||||
static FORBIDDEN_WORDS: &'static [&'static str] = &[
|
||||
|
||||
Reference in New Issue
Block a user