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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
use wordsworth::syllable_counter;

use punkt::params::Standard;
use punkt::SentenceTokenizer;
use punkt::Trainer;
use punkt::TrainingData;

use unicode_segmentation::UnicodeSegmentation;

use regex::Regex;

use std::fs;

// Removing stopwords
pub fn stopword_filter(mut vec: Vec<String>, lang: &str) ->  Vec<String>{
    let s_words = stop_words::get_nltk(lang);
    for w in s_words{
            vec.retain(|word| *word != w);
        }
    vec

}

pub fn stopwords_file(file: &str, lang: &str) {
    // let s_words = stop_words::get_nltk(lang);
    let file_text = file_to_string(file);
    let vec_of_words = word_list_from_string(&file_text);
    let no_stopwords = stopword_filter(vec_of_words, lang);
    let joined = no_stopwords.join(" ");
    fs::write("stopwords_removed.txt", joined).expect("Unable to write file");
}

pub fn stopwords_string(string: &str, lang: &str) -> String {
    let vec_of_words = word_list_from_string(string);
    let no_stopwords = stopword_filter(vec_of_words, lang);
    let joined = no_stopwords.join(" ");
    joined
}

pub fn word_list_from_string(string_to_analyze: &str) -> Vec<String> {
    let mut word_list: Vec<String> = Vec::new();
    let words = string_to_analyze.unicode_words();
    for w in words {
        word_list.push(w.to_string());
    }

    word_list
}

pub fn file_to_string(filename: &str) -> String {
    let string_from_file = fs::read_to_string(filename).expect("Unable to read file");
    string_from_file
}

pub fn long_words(word_list: Vec<String>) -> f64 {
    let mut sum = 0;
    for word in word_list {
        if word.len() >= 6 {
            sum += 1;
        }
    }
    let long_words_count: f64;
    long_words_count = sum as f64;
    long_words_count
}

pub fn percent_long_words(word_list: Vec<String>) -> f64 {
    let list_count = word_list.len() as i32;
    let long_words_count = long_words(word_list);
    let percent_longwords = long_words_count as f64 / list_count as f64;
    percent_longwords
}

pub fn character_count(string_to_analyze: &str) -> f64 {
    let re = Regex::new(r"[^\w]").unwrap();
    let result = re.replace_all(string_to_analyze, "");

    let character_count: f64;
    character_count = result.graphemes(true).count() as f64;

    character_count
}

pub fn syllable_count_list(word_list: Vec<String>) -> Vec<i32> {
    let mut sylcount_list: Vec<i32> = Vec::new();
    for word in &word_list {
        sylcount_list.push(syllable_counter(&word).try_into().unwrap());
    }
    sylcount_list
}

pub fn avg_syl_count(sylcount_list: Vec<i32>) -> f64 {
    let mut sum = 0;
    for w in &sylcount_list {
        sum += w;
    }
    let avg_syls: f64;
    avg_syls = sum as f64 / sylcount_list.len() as f64;
    avg_syls
}

// num of words with 2 syllables or less;
pub fn short_syl_count(word_list: Vec<String>) -> i32 {
    let sylcount_list = syllable_count_list(word_list);
    let mut short_syls = 0;
    for syl in sylcount_list {
        if syl <= 2 {
            short_syls += 1;
        }
    }
    short_syls
}

// num of words with 3 syls or more;
pub fn long_syl_count(word_list: Vec<String>) -> i32 {
    let sylcount_list = syllable_count_list(word_list);
    let mut long_syls = 0;
    for syl in sylcount_list {
        if syl >= 3 {
            long_syls += 1;
        }
    }
    long_syls
}

pub fn wordcount_list(word_list: Vec<String>) -> i32 {
    let wordcount: i32;
    wordcount = word_list.len() as i32;
    wordcount
}

pub fn split_into_sentences(doc: &str) -> Vec<String> {
    let trainer: Trainer<Standard> = Trainer::new();
    let mut data = TrainingData::new();

    trainer.train(doc, &mut data);
    let mut sent_list: Vec<String> = Vec::new();

    for s in SentenceTokenizer::<Standard>::new(doc, &data) {
        sent_list.push(s.to_string());
    }
    sent_list
}

pub fn sent_word_counts(doc: &str) -> Vec<usize> {
    let trainer: Trainer<Standard> = Trainer::new();
    let mut data = TrainingData::new();

    trainer.train(doc, &mut data);
    let mut sent_word_list: Vec<String> = Vec::new();

    let mut sent_wordcount_list: Vec<usize> = Vec::new();

    for s in SentenceTokenizer::<Standard>::new(doc, &data) {
        sent_word_list.push(s.to_string());
    }
    for s in sent_word_list {
        let mut temp_vec: Vec<String> = Vec::new();
        for word in s.split_whitespace() {
            temp_vec.push(word.to_string());
        }
        sent_wordcount_list.push(temp_vec.len());
    }
    sent_wordcount_list
}

pub fn sent_avg_wordcount(sent_wordcount_list: Vec<usize>) -> f64 {
    let mut sum = 0;
    for w in &sent_wordcount_list {
        sum += w;
    }
    let avg_word_length: f64;
    avg_word_length = sum as f64 / sent_wordcount_list.len() as f64;
    avg_word_length
}

pub fn lix(file_to_analyze: &str) -> f64 {
    //file to string
    let string_to_analyze = file_to_string(file_to_analyze);
    //total_words
    let all_words = word_list_from_string(&string_to_analyze);
    //num long words
    let num_long_words = percent_long_words(all_words);
    //avg_num_words/sentence
    let sent_wordcount_list = sent_word_counts(&string_to_analyze);
    let avg_words_per_sentence = sent_avg_wordcount(sent_wordcount_list);
    let lix_index = num_long_words + avg_words_per_sentence;
    println!("{}", lix_index);
    lix_index
}

pub fn rix(file_to_analyze: &str) -> f64 {
    //file to string
    let string_to_analyze = file_to_string(file_to_analyze);
    //total_words
    let all_words = word_list_from_string(&string_to_analyze);
    //num long words
    let num_long_words = long_words(all_words);
    let num_of_sents = split_into_sentences(&string_to_analyze).len() as f64;
    let rix_index = num_long_words / num_of_sents;
    println!("{}", rix_index);
    rix_index
}

pub fn flesch(file_to_analyze: &str) -> f64 {
    //file to string
    let string_to_analyze = file_to_string(file_to_analyze);
    let sent_counts = sent_word_counts(&string_to_analyze);
    let avg_sent_length = sent_avg_wordcount(sent_counts);
    //syls
    let all_words = word_list_from_string(&string_to_analyze);
    let all_syls = syllable_count_list(all_words);
    let avg_syls = avg_syl_count(all_syls);
    let flesch_index = 206.835 - (1.015 * avg_sent_length) - (84.6 * avg_syls);
    println!("{}", flesch_index);
    flesch_index
}

pub fn flesch_kincaid(file_to_analyze: &str) -> f64 {
    //file to string
    let string_to_analyze = file_to_string(file_to_analyze);
    // avg_words_per_sentence
    let sent_counts = sent_word_counts(&string_to_analyze);
    let avg_sent_length = sent_avg_wordcount(sent_counts);
    // avg syls per word
    let all_words = word_list_from_string(&string_to_analyze);
    let all_syls = syllable_count_list(all_words);
    let avg_syls = avg_syl_count(all_syls);
    let flesch_kincaid_index = (0.39 * avg_sent_length) + (11.8 * avg_syls) - 15.59;
    println!("{}", flesch_kincaid_index);
    flesch_kincaid_index
}

pub fn coleman_liau(file_to_analyze: &str) -> f64 {
    //file to string
    let string_to_analyze = file_to_string(file_to_analyze);
    //total_words
    let all_words = word_list_from_string(&string_to_analyze).len() as f64;
    // characters
    let chars = character_count(&string_to_analyze);
    // sents
    let sent_count = split_into_sentences(&string_to_analyze).len() as f64;

    let coleman_liau_index =
        0.0588 * ((chars / all_words) * 100.0) - 0.296 * ((sent_count / chars) * 100.0) - 15.8;
    println!("{}", coleman_liau_index);
    coleman_liau_index
}

pub fn ari(file_to_analyze: &str) -> f64 {
    //file to string
    let string_to_analyze = file_to_string(file_to_analyze);
    //total_words
    let all_words = word_list_from_string(&string_to_analyze).len() as f64;
    // characters
    let chars = character_count(&string_to_analyze);
    // sents
    let sent_count = split_into_sentences(&string_to_analyze).len() as f64;

    let automated_readability_index =
        (4.71 * (chars / all_words)) + (0.5 * (all_words / sent_count)) - 21.43;
    println!("{}", automated_readability_index);
    automated_readability_index
}

pub fn lix_string(string_to_analyze: &str) -> f64 {
    //total_words
    let all_words = word_list_from_string(string_to_analyze);
    //num long words
    let num_long_words = percent_long_words(all_words);
    //avg_num_words/sentence
    let sent_wordcount_list = sent_word_counts(string_to_analyze);
    for x in &sent_wordcount_list {
        println!("{}", x);
    }
    let avg_words_per_sentence = sent_avg_wordcount(sent_wordcount_list);
    let lix_index = num_long_words + avg_words_per_sentence;
    println!("{}", lix_index);
    lix_index
}

pub fn rix_string(string_to_analyze: &str) -> f64 {
    //total_words
    let all_words = word_list_from_string(string_to_analyze);
    //num long words
    let num_long_words = long_words(all_words);
    // let sentences
    let num_of_sents = split_into_sentences(string_to_analyze).len() as f64;
    let rix_index = num_long_words / num_of_sents;
    println!("{}", rix_index);
    rix_index
}

pub fn flesch_string(string_to_analyze: &str) -> f64 {
    let sent_counts = sent_word_counts(string_to_analyze);
    let avg_sent_length = sent_avg_wordcount(sent_counts);
    //syls
    let all_words = word_list_from_string(string_to_analyze);
    let all_syls = syllable_count_list(all_words);
    let avg_syls = avg_syl_count(all_syls);
    let flesch_index = 206.835 - (1.015 * avg_sent_length) - (84.6 * avg_syls);
    println!("{}", flesch_index);
    flesch_index
}

pub fn flesch_kincaid_string(string_to_analyze: &str) -> f64 {
    // avg_words_per_sentence
    let sent_counts = sent_word_counts(string_to_analyze);
    let avg_sent_length = sent_avg_wordcount(sent_counts);
    // avg syls per word
    let all_words = word_list_from_string(string_to_analyze);
    let all_syls = syllable_count_list(all_words);
    let avg_syls = avg_syl_count(all_syls);
    let flesch_kincaid_index = (0.39 * avg_sent_length) + (11.8 * avg_syls) - 15.59;
    println!("{}", flesch_kincaid_index);
    flesch_kincaid_index
}

pub fn coleman_liau_string(string_to_analyze: &str) -> f64 {
    //total_words
    let all_words = word_list_from_string(string_to_analyze).len() as f64;
    // characters
    let chars = character_count(string_to_analyze);
    // sents
    let sent_count = split_into_sentences(string_to_analyze).len() as f64;

    let coleman_liau_index =
        0.0588 * ((chars / all_words) * 100.0) - 0.296 * ((sent_count / chars) * 100.0) - 15.8;
    println!("{}", coleman_liau_index);
    coleman_liau_index
}

pub fn automated_readability_index_string(string_to_analyze: &str) -> f64 {
    //total_words
    let all_words = word_list_from_string(string_to_analyze).len() as f64;
    // characters
    let chars = character_count(string_to_analyze);
    // sents
    let sent_count = split_into_sentences(string_to_analyze).len() as f64;

    let automated_readability_index =
        (4.71 * (chars / all_words)) + (0.5 * (all_words / sent_count)) - 21.43;
    println!("{}", automated_readability_index);
    automated_readability_index
}