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
use lazy_static::{initialize, lazy_static};
use libflate::gzip::Decoder;
use std::{collections::HashMap, io::Read, str};

const WORDNET_DICT_COMPRESSED: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/wordnet.gz"));

lazy_static! {
    static ref WORDNET_DICT: HashMap<String, Vec<String>> = {
        let mut dec = Decoder::new(WORDNET_DICT_COMPRESSED)
            .expect("Failed to initialize runtime dictionary decoder");
        let mut output = Vec::new();
        dec.read_to_end(&mut output)
            .expect("Failed to decompress dictionary");

        let uncompressed_dictionary = String::from_utf8(output).unwrap();

        let mut dict: HashMap<String, Vec<String>> = HashMap::new();

        for line in uncompressed_dictionary.lines() {
            let mut line: Vec<String> = line.split('|').map(|x| x.to_string()).collect();

            let name = line.remove(0);

            dict.insert(name, line);
        }

        dict
    };
}

/// Initializes the dictionary in memory, this will make the first subsequent call faster (optional).
pub fn init() {
    initialize(&WORDNET_DICT);
}

/// Returns the dictionary from memory.
pub fn dict() -> HashMap<String, Vec<String>> {
    WORDNET_DICT.to_owned()
}

/// Retrieve synonyms for a word.
pub fn synonyms(word: impl AsRef<str>) -> Vec<String> {
    WORDNET_DICT
        .get(&word.as_ref().to_lowercase())
        .map(|x| x.clone())
        .unwrap_or_default()
}