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
extern crate prefixtree;
use std::fs::File;
use std::io::Read;
use std::path::Path;
use self::prefixtree::PrefixTree;
#[derive(Clone)]
pub struct Dict {
prefix_tree: PrefixTree<bool>,
}
impl Dict {
pub fn default_path() -> &'static Path {
Path::new(
concat!(env!("CARGO_MANIFEST_DIR"),
"/data/thai.txt"))
}
pub fn lao_path() -> &'static Path {
Path::new(
concat!(env!("CARGO_MANIFEST_DIR"),
"/data/laowords.txt"))
}
#[allow(dead_code)]
pub fn create<'a>(words: &Vec<String>) -> Result<Dict, &'a str> {
let words_payloads: Vec<(&str, bool)> =
words.iter().map(|word| (&word[..], true)).collect();
Ok(Dict{prefix_tree:PrefixTree::new(&words_payloads[..])})
}
pub fn load<'a>(path: &Path) -> Result<Dict, &'a str> {
let mut f = match File::open(path) {
Ok(f) => f,
Err(_) => return Err("Cannot open dict")
};
let mut s = String::new();
match f.read_to_string(&mut s) {
Ok(_) => (),
Err(_) => return Err("Cannot read dict")
};
let mut words: Vec<String> = s.lines().map(|word| String::from(word)).collect();
words.sort();
let words_payloads: Vec<(&str, bool)>
= words.iter().map(|word| (&word[..], true)).collect();
Ok(Dict{prefix_tree:PrefixTree::new(&words_payloads[..])})
}
pub fn seek(&self, p: usize, offset: usize, ch: char) -> Option<&(u32,bool,Option<bool>)> {
self.prefix_tree.seek(&(p as u32, offset as u32, ch))
}
}