-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.rs
60 lines (50 loc) · 1.49 KB
/
main.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use std::io::{self, stdin, BufRead};
use std::process::exit;
use clap::Parser;
use heavykeeper::TopK;
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short = 'k')]
k: usize,
#[arg(short = 'w', default_value_t = 8)]
width: usize,
#[arg(short = 'd', default_value_t = 2048)]
depth: usize,
#[arg(short = 'y', default_value_t = 0.9)]
decay: f64,
#[arg(short = 'f')]
input: Option<String>,
}
fn main() {
let args = Args::parse();
let mut topk = TopK::new(args.k, args.width, args.depth, args.decay);
if args.input.is_none() {
let stdin = stdin();
for line in stdin.lock().lines() {
let item = line.unwrap();
// break item into words
for word in item.split_whitespace() {
topk.add(word.as_bytes().to_vec());
}
}
} else {
let file = std::fs::File::open(args.input.unwrap());
if file.is_err() {
eprintln!("Error: {}", file.err().unwrap());
exit(1);
}
let file = file.unwrap();
let reader = io::BufReader::new(file);
for line in reader.lines() {
let item = line.unwrap();
// break item into words
for word in item.split_whitespace() {
topk.add(word.as_bytes().to_vec());
}
}
}
for node in topk.list() {
println!("{} {}", String::from_utf8_lossy(&node.item), node.count);
}
}