Skip to content

Commit

Permalink
Add command line argument support
Browse files Browse the repository at this point in the history
  • Loading branch information
kamali-sina committed Jun 22, 2023
1 parent f87c695 commit 5c785a2
Showing 1 changed file with 26 additions and 8 deletions.
34 changes: 26 additions & 8 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
use std::io::{self, BufRead};
use std::{io::{self, BufRead}, path::PathBuf, process::exit};
use structopt::{StructOpt};

const WORDLIST:&str = include_str!("./meaningfullwordlist.txt");
const THRESHHOLD: f32 = 0.3;

#[derive(StructOpt, Debug)]
#[structopt(name = "meaningcheck")]
struct Opt {
/// Use this to meaning check a file
#[structopt(short, long, parse(from_os_str))]
file: Option<PathBuf>,

/// The threshhold for meaning checking
#[structopt(short, long, default_value = "0.3")]
threshhold: f32,
}

fn main() {
let input = get_input();
print_meaningful_lines(&input);
let opt = Opt::from_args();
let threshhold: f32 = opt.threshhold;
if opt.file.is_none() {
let input = get_input();
print_meaningful_lines(&input, &threshhold);
} else {

}
}

fn get_input() -> String {
Expand All @@ -15,26 +33,26 @@ fn get_input() -> String {
return input;
}

fn print_meaningful_lines(input: &String) {
fn print_meaningful_lines(input: &String, threshhold: &f32) {
let lines: Vec<&str> = input.split('\n').collect();
let meaningfulwords: Vec<&str> = WORDLIST.split('\n').collect();

for line in lines {
if line_has_meaning(line, &meaningfulwords) {
if line_has_meaning(line, &meaningfulwords, threshhold) {
println!("{line}");
}
}
}

fn line_has_meaning(line: &str, meaningfulwords: &Vec<&str>) -> bool {
fn line_has_meaning(line: &str, meaningfulwords: &Vec<&str>, threshhold: &f32) -> bool {
let mut meaning_lenght = 0;
for word in meaningfulwords {
if line.contains(word) {
meaning_lenght += word.len();
}
}

if (meaning_lenght as f32 / line.len() as f32) > THRESHHOLD {
if (meaning_lenght as f32 / line.len() as f32) > *threshhold {
return true;
}

Expand Down

0 comments on commit 5c785a2

Please sign in to comment.