-
Notifications
You must be signed in to change notification settings - Fork 56
/
interactive_parsing_cli.rs
70 lines (65 loc) · 2.21 KB
/
interactive_parsing_cli.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
61
62
63
64
65
66
67
68
69
70
extern crate clap;
extern crate env_logger;
extern crate serde_json;
extern crate snips_nlu_lib;
use clap::{App, Arg};
use snips_nlu_lib::SnipsNluEngine;
use std::io;
use std::io::Write;
fn main() {
env_logger::Builder::from_default_env()
.default_format_timestamp_nanos(true)
.init();
let matches = App::new("snips-nlu-parse")
.about("Snips NLU interactive CLI for parsing intents")
.arg(
Arg::with_name("NLU_ENGINE_DIR")
.required(true)
.takes_value(true)
.index(1)
.help("path to the trained nlu engine directory"),
)
.arg(
Arg::with_name("intents_alternatives")
.short("i")
.long("--intents-alternatives")
.takes_value(true)
.help("number of alternative parsing results to return in the output"),
)
.arg(
Arg::with_name("slots_alternatives")
.short("s")
.long("--slots-alternatives")
.takes_value(true)
.help("number of alternative slot values to return along with each extracted slot"),
)
.get_matches();
let engine_dir = matches.value_of("NLU_ENGINE_DIR").unwrap();
let intents_alternatives = matches
.value_of("intents_alternatives")
.map(|v| v.to_string().parse::<usize>().unwrap())
.unwrap_or(0);
let slots_alternatives = matches
.value_of("slots_alternatives")
.map(|v| v.to_string().parse::<usize>().unwrap())
.unwrap_or(0);
println!("\nLoading the nlu engine...");
let engine = SnipsNluEngine::from_path(engine_dir).unwrap();
loop {
print!("> ");
io::stdout().flush().unwrap();
let mut query = String::new();
io::stdin().read_line(&mut query).unwrap();
let result = engine
.parse_with_alternatives(
query.trim(),
None,
None,
intents_alternatives,
slots_alternatives,
)
.unwrap();
let result_json = serde_json::to_string_pretty(&result).unwrap();
println!("{}", result_json);
}
}