use aoko::no_std::{functions::ext::{BoolExt, Utf8Ext}, pipelines::pipe::Pipe};
use itertools::Itertools;
use std::fs;
const P: [char; 34] = [' ', '\r', '\n', ',', '。', '!', '?', '“', '”', '‘', '’', ':', ';', '(', ')', '(', ')', '「', '」', '『', '』', '[', ']', '【', '】', '〖', '〗', '{', '}', '《', '》', '—', '〔', '〕'];
pub fn batch_cn(dict: String, r#in: String, out: String) {
fs::read(r#in).unwrap()
.to_str().unwrap()
.pipe(|s| if let None = dict.chars().next() {
s.chars()
.filter_map(|c| P.iter().all(|&p| c != p).if_true((c, 1)))
.into_grouping_map_by(|t| t.0.to_string()) .fold(0, |acc, _, (_, count)| acc + count) .into_iter()
.sorted_by_key(|t| t.1)
} else {
fs::read(dict).unwrap()
.to_ascii_lowercase()
.to_str().unwrap()
.lines()
.flat_map(|phrase| s.matches(phrase).counts())
.map(|(s, u)| (s.to_string(), u))
.sorted_by_key(|t| t.1)
})
.rev()
.map(|t| format!("{:?}", t))
.join("\n")
.pipe(|byt| fs::write(out, byt).unwrap())
}