-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
123 lines (95 loc) · 3.04 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#![allow(dead_code)]
use std::env;
use std::fs::File;
use std::io::{BufRead, BufReader, Write};
use std::path::{Path, PathBuf};
use std::sync::mpsc;
use std::thread;
const ALPHABET_EN: &'static str = "abcdefghijklmnopqrstuvwxyz";
fn main() {
if let Ok(s) = env::var("SKIP_DICT_REBUILD") {
if s != String::from("false") {
return;
}
let out_dir =
if let Ok(result) = env::var("LOCALE") {
format!("./resources/{}/", result.to_lowercase())
} else {
format!("./resources/{}/", String::from("en-us"))
};
let dest_path = Path::new(&out_dir).join("freq_50k_precalc.txt");
let mut f = File::create(&dest_path).unwrap();
refresh_dict(&out_dir, &mut f);
}
}
fn find_variations(word: String) -> Vec<String> {
let mut result: Vec<String> = Vec::new();
let mut base: String;
let mut replace: String;
let mut removed: char;
let len = word.len() + 1;
for pos in 0..len {
if pos < len - 1 {
base = word.clone();
// deletes
removed = base.remove(pos);
result.push(base.clone());
// replaces
for chara in ALPHABET_EN.chars() {
if chara == removed {
continue;
}
replace = base.clone();
replace.insert(pos, chara);
result.push(replace.clone());
}
// transposes
if pos > 0 {
base.insert(pos - 1, removed);
result.push(base.clone());
}
}
// inserts
for chara in ALPHABET_EN.chars() {
base = word.clone();
base.insert(pos, chara);
result.push(base.clone());
}
}
result
}
fn refresh_dict(source_dir: &String, target: &mut File) {
let (tx, rx) = mpsc::channel();
let source = source_dir.clone();
thread::spawn(move || {
let path =
if let Ok(override_dict) = env::var("OVERRIDE_DICT") {
PathBuf::from(&override_dict)
} else {
Path::new(&source).join("freq_50k.txt")
};
if !path.exists() || !path.is_file() {
eprintln!("Unable to open the source dictionary from path: {:?}...", path);
return;
}
let file = File::open(path).expect("file not found");
let reader = BufReader::new(file);
for raw_line in reader.lines() {
if let Ok(line) = raw_line {
tx.send(line).unwrap();
}
}
});
let mut result: String;
for entry in rx {
let temp: Vec<&str> = entry.splitn(2, ",").collect();
if temp[0].is_empty() {
continue;
}
result = temp[0].to_owned();
//TODO: call `find_variations` to get all words within 1 edit distance, then save it to the
// designated location.
result.push_str("\r\n");
target.write(result.as_bytes()).unwrap();
}
}