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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! dictionary.rs
//!
//! Contains the implementation of word dictionary and its occurrences

use std::collections::HashMap;
use std::io;
use std::fs::File;
use std::io::BufRead;

/// A dictionary structure to track word occurrences
pub struct Dictionary {
    words: HashMap<String, usize>,
}

impl Dictionary {
    /// Checks whether the dictionary contains the given word
    pub fn contains(&self, word: &str) -> bool {
        self.words.contains_key(word.to_lowercase().as_str())
    }

    /// Initialize empty Dictionary
    ///
    /// # Examples
    ///
    /// ```
    /// use rustrawi::dictionary::Dictionary;
    /// let dictionary = Dictionary::new();
    /// assert_eq!(dictionary.len(), 0);
    /// ```
    pub fn new() -> Self {
        Self {
            words: HashMap::new()
        }
    }

    /// Initialize the dictionary from a custom String collection
    ///
    /// # Examples
    ///
    /// ```
    /// use rustrawi::dictionary::Dictionary;
    /// let word_list = vec!["burung", "ayam", "kucing"];
    /// let mut dictionary = Dictionary::from_list(word_list);
    /// assert_eq!(dictionary.contains("burung"), true);
    /// assert_eq!(dictionary.contains("ayam"), true);
    /// assert_eq!(dictionary.contains("kucing"), true);
    /// ```
    pub fn from_list(word_list: Vec<&str>) -> Self {
        let mut dictionary = Dictionary::new();
        for word in word_list {
            dictionary.add(word.to_string())
        }
        dictionary
    }

    /// Read line of string from a given filename
    fn read_lines_from_file<P> (filename: P) -> io::Result<io::Lines<io::BufReader<File>>>
    where P: AsRef<std::path::Path>, {
        let file = File::open(filename)?;
        Ok(io::BufReader::new(file).lines())
    }

    /// Initialize dictionary from a text file
    ///
    /// # Examples
    ///
    /// ```
    /// use rustrawi::dictionary::Dictionary;
    /// let dictionary = Dictionary::from_file("tests/example_word_list");
    /// assert_eq!(dictionary.len(), 3);
    /// ```
    pub fn from_file(filename: &str) -> Self {
        let mut dictionary = Dictionary::new();
        match Dictionary::read_lines_from_file(&filename) {
            Ok(lines) => {
                for line in lines {
                    if let Ok(value) = line {
                        dictionary.add(value);
                    }
                }
            },
            Err(e) => panic!("{}", e)
        }
        dictionary
    }

    /// Add a word to the dictionary (or update its occurrences)
    ///
    /// # Examples
    ///
    /// ```
    /// use rustrawi::dictionary::Dictionary;
    /// let mut dictionary = Dictionary::new();
    /// dictionary.add(String::from("burung"));
    /// assert_eq!(dictionary.contains("burung"), true);
    /// ```
    pub fn add(&mut self, word: String) {
        let word = word.trim().to_lowercase();
        if word == "" {
            return;
        }
        *self.words.entry(word.to_string()).or_insert(0_usize) += 1_usize;
    }

    /// Add list of word to the dictionary
    pub fn add_from_list(&mut self, words: Vec<&str>) {
        for word in words {
            self.add(word.to_string());
        }
    }

    /// Remove a word from dictionary
    /// Returns the word if it is removed successfully
    /// Returns None if the dictionary did not contains the word
    pub fn remove(&mut self, word: String) -> Option<String>{
        let word = word.trim();
        if word == "" {
            return None
        }
        match self.words.remove(word) {
            Some(_occurrence) => Some(word.to_string()),
            None => None
        }
    }

    /// Returns the length of the word dictionary
    pub fn len(&self) -> usize {
        self.words.len()
    }
}

#[cfg(test)]
mod dictionary_new_test {
    use super::*;

    #[test]
    fn should_generate_new_instance() {
        let dictionary = Dictionary::new();
        assert_eq!(dictionary.words.len(), 0);
    }
}

#[cfg(test)]
mod dictionary_from_test {
    use super::*;

    #[test]
    fn should_instantiate_with_custom_string_collection() {
        let word_list = vec!["burung", "ayam", "kucing"];
        let dictionary = Dictionary::from_list(word_list);
        assert_eq!(dictionary.contains("burung"), true);
    }

    #[test]
    fn should_instantiate_from_file() {
        let dictionary = Dictionary::from_file("tests/example_word_list");
        assert_eq!(dictionary.len(), 3);
        assert_eq!(dictionary.contains("burung"), true);
        assert_eq!(dictionary.contains("kucing"), true);
        assert_eq!(dictionary.contains("ayam"), true);
    }

    #[test]
    #[should_panic(expected = "No such file or directory (os error 2)")]
    fn should_panic_if_instantiating_from_invalid_file() {
        let _dictionary = Dictionary::from_file("tests/invalid_file");
    }
}

#[cfg(test)]
mod dictionary_contains_test {
    use super::*;

    #[test]
    fn should_return_false_if_string_is_not_contained() {
        let dictionary = Dictionary::new();
        assert_eq!(dictionary.contains(String::from("burung").as_str()), false);
    }

    #[test]
    fn should_return_true_if_string_is_contained() {
        let word_list = vec!["burung", "ayam", "kucing"];
        let dictionary = Dictionary::from_list(word_list);
        assert_eq!(dictionary.contains(String::from("burung").as_str()), true);
    }
}

#[cfg(test)]
mod dictionary_add_test {
    use super::*;

    #[test]
    fn should_be_able_to_add_word() {
        let mut dictionary = Dictionary::new();
        dictionary.add(String::from("burung"));
        assert_eq!(dictionary.contains(String::from("burung").as_str()), true);
        assert_eq!(dictionary.words.len(), 1);

        dictionary.add(String::from("kucing"));
        assert_eq!(dictionary.contains(String::from("kucing").as_str()), true);
        assert_eq!(dictionary.words.len(), 2);
    }

    #[test]
    fn should_not_add_empty_string() {
        let mut dictionary = Dictionary::new();
        dictionary.add(String::from(""));
        assert_eq!(dictionary.words.len(), 0);
    }

    #[test]
    fn should_not_add_whitespace() {
        let mut dictionary = Dictionary::new();
        dictionary.add(String::from(" "));
        assert_eq!(dictionary.words.len(), 0);
    }

    #[test]
    fn should_trim_before_add() {
        let mut dictionary = Dictionary::new();
        dictionary.add(String::from(" burung "));
        assert_eq!(dictionary.contains(String::from(" burung ").as_str()), false);
        assert_eq!(dictionary.contains(String::from("burung").as_str()), true);
        assert_eq!(dictionary.words.len(), 1);
    }

    #[test]
    fn should_add_from_list() {
        let mut dictionary = Dictionary::new();
        let word_list = vec!["burung", "kucing", "ayam"];
        dictionary.add_from_list(word_list);
        assert_eq!(dictionary.words.len(), 3);
        assert_eq!(dictionary.contains(String::from("burung").as_str()), true);
        assert_eq!(dictionary.contains(String::from("kucing").as_str()), true);
        assert_eq!(dictionary.contains(String::from("ayam").as_str()), true);
    }
}

#[cfg(test)]
mod dictionary_remove_test {
    use super::*;

    #[test]
    fn should_remove_entry() {
        let mut dictionary = Dictionary::new();
        dictionary.add(String::from("burung"));
        dictionary.add(String::from("kucing"));
        assert_eq!(dictionary.words.len(), 2);
        let result: Option<String> = dictionary.remove(String::from("burung"));
        assert_eq!(result.is_some(), true);
        assert_eq!(dictionary.words.len(), 1);
        assert_eq!(dictionary.contains(String::from("burung").as_str()), false);

        // try to re-delete the same word
        let result: Option<String> = dictionary.remove(String::from("burung"));
        assert_eq!(result.is_some(), false);
        assert_eq!(dictionary.words.len(), 1);
        assert_eq!(dictionary.contains(String::from("burung").as_str()), false);
    }

    #[test]
    fn should_return_none_on_empty_string() {
        let mut dictionary = Dictionary::new();
        dictionary.add(String::from("burung"));
        assert_eq!(dictionary.words.len(), 1);
        let result: Option<String> = dictionary.remove(String::from(""));
        assert_eq!(result.is_none(), true);
        assert_eq!(dictionary.words.len(), 1);
        assert_eq!(dictionary.contains(String::from("burung").as_str()), true);
    }

    #[test]
    fn should_return_none_on_whitespace() {
        let mut dictionary = Dictionary::new();
        dictionary.add(String::from("burung"));
        assert_eq!(dictionary.words.len(), 1);
        let result: Option<String> = dictionary.remove(String::from(" "));
        assert_eq!(result.is_none(), true);
        assert_eq!(dictionary.words.len(), 1);
        assert_eq!(dictionary.contains(String::from("burung").as_str()), true);
    }

    #[test]
    fn should_return_none_on_not_found() {
        let mut dictionary = Dictionary::new();
        dictionary.add(String::from("burung"));
        assert_eq!(dictionary.words.len(), 1);
        let result: Option<String> = dictionary.remove(String::from("kucing"));
        assert_eq!(result.is_none(), true);
    }
}

#[cfg(test)]
mod dictionary_len_test {
    use super::*;

    #[test]
    fn should_be_able_to_return_dictionary_length() {
        let mut dictionary = Dictionary::new();
        dictionary.add(String::from("burung"));
        assert_eq!(dictionary.len(), 1);

        dictionary.add(String::from("kucing"));
        assert_eq!(dictionary.len(), 2);
    }
}