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
use std::collections::HashMap;
use std::fs::OpenOptions;
use std::io::Write;
use std::path::PathBuf;
use chrono::prelude::*;
pub fn trim_to_words(content: String) -> std::vec::Vec<std::string::String> {
let content: Vec<String> = content
.to_lowercase()
.replace(&['-'][..], " ")
.replace("'s", "")
.replace(
&[
'(', ')', ',', '\"', '.', ';', ':', '=', '[', ']', '{', '}', '-', '_', '/', '\'',
'’', '?', '!', '“', '‘',
][..],
"",
)
.split_whitespace()
.map(String::from)
.collect::<Vec<String>>();
content
}
pub fn count_words(words: &[String]) -> std::collections::HashMap<std::string::String, u32> {
let mut frequency: HashMap<String, u32> = HashMap::new();
for word in words {
*frequency.entry(word.to_owned()).or_insert(0) += 1;
}
frequency
}
pub fn sort_map_to_vec(
frequency: HashMap<String, u32>,
) -> std::vec::Vec<(std::string::String, u32)> {
let mut vec_sorted: Vec<(String, u32)> = frequency.into_iter().collect();
vec_sorted.sort_by(|a, b| b.1.cmp(&a.1));
vec_sorted
}
pub fn get_index_min(index: &usize) -> usize {
if *index as isize - 5 < 0 {
0
} else {
index - 5
}
}
pub fn get_index_max(index: &usize, max_len: &usize) -> usize {
if index + 5 > *max_len {
*max_len as usize
} else {
index + 5
}
}
pub fn save_file(to_file: String, mut path: PathBuf) -> std::io::Result<PathBuf> {
let local: DateTime<Local> = Local::now();
let new_filename: String = local
.format("%Y_%m_%d_%H_%M_%S_results_word_analysis.txt")
.to_string();
path.push(new_filename);
let mut file = OpenOptions::new().write(true).create(true).open(&path)?;
file.write_all(to_file.as_bytes())?;
Ok(path)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_count() {
let words = vec![
"one".to_string(),
"two".to_string(),
"two".to_string(),
"three".to_string(),
"three".to_string(),
"three".to_string(),
];
let counted = count_words(&words);
let mut words_map = HashMap::new();
words_map.insert("one".to_string(), 1 as u32);
words_map.insert("two".to_string(), 2 as u32);
words_map.insert("three".to_string(), 3 as u32);
assert_eq!(counted, words_map);
}
#[test]
fn test_max_min_index() {
let index1 = 5;
let min_index1 = get_index_min(&index1);
let max_index1 = get_index_max(&index1, &9);
assert_eq!(min_index1, 0);
assert_eq!(max_index1, 9);
let index2 = 0;
let min_index2 = get_index_min(&index2);
let max_index2 = get_index_max(&index2, &5);
assert_eq!(min_index2, 0);
assert_eq!(max_index2, 5);
let index3 = 100;
let min_index3 = get_index_min(&index3);
let max_index3 = get_index_max(&index3, &103);
assert_eq!(min_index3, 95);
assert_eq!(max_index3, 103);
}
#[test]
fn example_test() {
use std::time::Instant;
let instant = Instant::now();
let mut frequency: HashMap<String, u32> = HashMap::new();
let mut words_near_vec_map: HashMap<String, Vec<String>> = HashMap::new();
let mut map_near: HashMap<String, Vec<(String, u32)>> = HashMap::new();
let text: String = "An example phrase including two times the word two".to_string();
let content_vec: Vec<String> = trim_to_words(text);
let mut words_near_vec: Vec<String> = Vec::new();
for (index, word) in content_vec.clone().into_iter().enumerate() {
*frequency.entry(word.to_owned()).or_insert(0) += 1;
let min: usize = get_index_min(&index);
let max: usize = get_index_max(&index, &content_vec.len());
(for (number, value) in content_vec.iter().enumerate().take(max).skip(min) {
if number == index {
continue;
} else {
words_near_vec.push(value.clone());
}
});
words_near_vec_map
.entry(word.to_owned())
.or_insert_with(Vec::new)
.append(&mut words_near_vec);
}
for (word, words) in words_near_vec_map {
let counted_near = sort_map_to_vec(count_words(&words));
map_near.entry(word).or_insert(counted_near);
}
let counted = sort_map_to_vec(frequency);
let mut to_file = String::new();
for (word, frequency) in counted {
let words_near = &map_near[&word];
let combined = format!(
"Word: {:?}, Frequency: {:?},\n Words near: {:?}\n\n",
word, frequency, words_near
);
to_file.push_str(&combined);
}
println!(
"Finished in {:?}! Results:\n {}",
instant.elapsed(),
to_file
);
}
}