Initial commit
Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
commit
a1c11f6352
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/target
|
8
Cargo.toml
Normal file
8
Cargo.toml
Normal file
@ -0,0 +1,8 @@
|
||||
[package]
|
||||
name = "metallibruh"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
rand = "0.8.4"
|
||||
|
46
src/main.rs
Normal file
46
src/main.rs
Normal file
@ -0,0 +1,46 @@
|
||||
use std::{fs, env, collections::{HashMap, HashSet}};
|
||||
use rand::{seq::SliceRandom, prelude::ThreadRng};
|
||||
|
||||
fn read_input(path: &str) -> String {
|
||||
return fs::read_to_string(path).expect("Cannot read file.");
|
||||
}
|
||||
|
||||
fn parse_input(s: &str) -> HashMap<String, HashSet<String>> {
|
||||
let mut c = HashMap::new();
|
||||
let mut p;
|
||||
for l in s.lines() {
|
||||
p = "".to_string();
|
||||
for w in l.split_whitespace() {
|
||||
let mut h = c.get(&p.to_string()).unwrap_or(&HashSet::new()).clone();
|
||||
h.insert(w.to_string());
|
||||
c.insert(p, h);
|
||||
p = w.to_string();
|
||||
}
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
fn get_random_next_from(w: &String, m: &HashMap<String, HashSet<String>>, r: &mut ThreadRng) -> String {
|
||||
let _h = HashSet::new();
|
||||
let v = m.get(w).unwrap_or(&_h).iter().collect::<Vec<&String>>();
|
||||
return v.choose(r).unwrap().to_string();
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args: Vec<String> = env::args().collect();
|
||||
for arg in args.iter().skip(1) {
|
||||
let input = read_input(&arg);
|
||||
let vec_in = parse_input(&input);
|
||||
let mut rng = &mut rand::thread_rng();
|
||||
let mut sen = vec![];
|
||||
loop {
|
||||
while !sen.last().unwrap_or(&"".to_string()).ends_with(".") {
|
||||
sen.push(get_random_next_from(sen.last().unwrap_or(&"".to_string()), &vec_in, &mut rng));
|
||||
}
|
||||
if !input.contains(sen.join(" ").as_str()) { break; } else { sen.clear(); }
|
||||
}
|
||||
println!("[{}]", &arg);
|
||||
println!("{}", sen.join(" "));
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user