Initial commit

Signed-off-by: Louis Vallat <louis@louis-vallat.xyz>
This commit is contained in:
Louis Vallat 2022-02-02 10:47:12 +01:00
commit a1c11f6352
No known key found for this signature in database
GPG Key ID: 0C87282F76E61283
3 changed files with 55 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

8
Cargo.toml Normal file
View 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
View 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(" "));
}
}