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
#[macro_use]
extern crate lazy_static;

pub mod crawler;
pub mod fs;
pub mod parser;
pub mod trie;

use trie::Map;

#[derive(Debug)]
pub struct Word {
    pub entry: String,
    pub meaning: String,
    pub pos: Vec<String>,
    pub category: Vec<String>,
}

pub type Words = Vec<Word>;

pub struct Dictionary {
    pub words: Map<Word>,
}

impl Dictionary {
    pub fn new(words: Words) -> Dictionary {
        let mut map = Map::new();
        for word in words {
            map.insert(word.entry.to_string(), word);
        }
        Dictionary { words: map }
    }

    pub fn find(&self, entry: &str) -> Option<&Words> {
        self.words.get(entry.to_string())
    }

    pub fn find_mut(&mut self, entry: &str) -> Option<&mut Words> {
        self.words.get_mut(entry.to_string())
    }

    pub fn get_all(&self) -> Vec<&Word> {
        self.words.values()
    }

    pub fn has(&self, entry: &str) -> bool {
        self.words.contains_key(entry.to_string())
    }

    pub fn size(&self) -> usize {
        self.words.len()
    }

    pub fn starts_with(&self, start: &str) -> Option<Vec<&Word>> {
        self.words.range(start.to_string())
    }
}