-
Notifications
You must be signed in to change notification settings - Fork 47
/
build.rs
68 lines (60 loc) · 1.89 KB
/
build.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
extern crate rmp_serde;
extern crate rustling;
extern crate rustling_ontology_grammar as grammar;
extern crate rustling_ontology_values;
extern crate serde;
#[path = "src/parser.rs"]
mod parser;
use grammar::Lang;
use std::thread::{self, JoinHandle};
use std::{env, fs, path};
pub fn train_async(lang: Lang) -> JoinHandle<()> {
println!(
"cargo:rerun-if-changed=grammar/{}/src/",
lang.to_string().to_lowercase()
);
thread::spawn(move || {
let out_dir = path::PathBuf::from(env::var("OUT_DIR").unwrap());
let mut file = fs::File::create(out_dir.join(format!(
"{}{}",
lang.to_string().to_lowercase(),
".rmp"
)))
.unwrap();
let rules = grammar::rules(lang).unwrap();
let examples = grammar::examples(lang);
let model = rustling::train::train(&rules, examples, parser::FeatureExtractor()).unwrap();
rmp_serde::encode::write(&mut file, &model).unwrap();
})
}
pub fn train_sync(lang: Lang) {
println!(
"cargo:rerun-if-changed=grammar/{}/src/",
lang.to_string().to_lowercase()
);
let out_dir = path::PathBuf::from(env::var("OUT_DIR").unwrap());
let mut file =
fs::File::create(out_dir.join(format!("{}{}", lang.to_string().to_lowercase(), ".rmp")))
.unwrap();
let rules = grammar::rules(lang).unwrap();
let examples = grammar::examples(lang);
let model = rustling::train::train(&rules, examples, parser::FeatureExtractor()).unwrap();
rmp_serde::encode::write(&mut file, &model).unwrap();
}
pub fn train_all_async() {
let join_handlers: Vec<_> = Lang::all()
.into_iter()
.map(|lang| train_async(lang))
.collect();
for join in join_handlers {
join.join().unwrap();
}
}
pub fn train_all_sync() {
for lang in Lang::all() {
train_sync(lang);
}
}
fn main() {
train_all_sync();
}