-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.rs
46 lines (41 loc) · 1.26 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
use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Result, Write};
use std::path::Path;
const NUM_LETTERS: usize = 26;
fn main() -> Result<()> {
let chars = {
let file = File::open("/usr/share/dict/words")?;
let reader = BufReader::new(file);
parse(reader)?
};
let out_path = Path::new(&std::env::var("OUT_DIR").unwrap()).join("prebuilt_model.rs");
let file = File::create(out_path)?;
let mut writer = BufWriter::new(file);
writeln!(writer, "[")?;
for i in 0..(NUM_LETTERS + 1) {
write!(writer, "[")?;
for j in 0..(NUM_LETTERS + 1) {
write!(writer, "{},", chars[i][j])?;
}
writeln!(writer, "],")?;
}
writeln!(writer, "]")?;
Ok(())
}
fn parse(r: BufReader<File>) -> Result<[[usize; NUM_LETTERS + 1]; NUM_LETTERS + 1]> {
let mut chars = [[0; NUM_LETTERS + 1]; NUM_LETTERS + 1];
for line in r.lines() {
let word = line?;
let mut prefix = NUM_LETTERS;
for c in word.bytes() {
let c = c.to_ascii_lowercase();
if !c.is_ascii_lowercase() {
continue;
}
let i = (c - b'a') as usize;
chars[prefix][i] += 1;
prefix = i;
}
}
Ok(chars)
}