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
mod utils;

#[macro_use]
extern crate lazy_static;

use regex::Regex;
use regex::RegexBuilder;
use regex::RegexSet;
use regex::RegexSetBuilder;
use wasm_bindgen::prelude::*;

// When the `wee_alloc` feature is enabled, use `wee_alloc` as the global
// allocator.
#[cfg(feature = "wee_alloc")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

pub const WORD_PATTERN: &str = r"\b(\p{L}+(?:[-']\p{L}+)?)\b";
pub const SENTENCE_PATTERN: &str = r"[^.?!]";
pub const VOWEL_GROUPS_PATTERN: &str = r"[aeiou]+";

lazy_static! {
    static ref WORD_REGEX: Regex = regex_builder(WORD_PATTERN);
    static ref SENTENCE_REGEX: Regex = regex_builder(SENTENCE_PATTERN);
    static ref VOWEL_GROUPS_REGEX: Regex = regex_builder(VOWEL_GROUPS_PATTERN);
    static ref DEDUCT_PATTERN_REGEXES: RegexSet = regexset_builder(&[
        r"e\b",
        r"ey\b",
        r"ed\b",
        r"ay\b",
        r"[kmrpbdtnvrw]es\b",
        r"ely\b",
        r"oy\b",
        r"cia",
        r"[aeilouy]le\b",
        r"tia[nl]?\b",
        r"tia([nl]s)?\b",
        r"[aeo]ym",
        r"eness\b",
        r"\bfore",
        r"ay[bclntrw]",
        r"ement",
        r"iles\b",
        r"[ao]les\b",
        r"eman\b",
        r"aying\b",
        r"oy[cln]",
        r"eful",
        r"\bey",
        r"geon",
        r"\bhome",
        r"eyn",
        r"ically",
        r"eless",
        r"sian\b",
        r"yles",
        r"\bwhite",
        r"eway",
        r"georg",
        r"lles\b",
        r"busine",
        r"illia",
        r"ules\b",
        r"\bhym",
        r"ryst",
        r"eyl",
        r"ehou",
        r"eyw",
        r"ekeep",
        r"people",
        r"every",
        r"\blife",
        r"giu",
        r"eyin",
        r"eout",
        r"oying\b",
        r"gues\b",
        r"\breine",
        r"geou",
        r"ques\b",
        r"vior",
        r"sewo",
        r"oseb",
        r"eyc",
        r"\bspace",
        r"\bstone",
        r"eover",
        r"ehol",
        r"iliar",
        r"estone",
        r"eyb",
        r"oyk",
        r"velan",
        r"piet",
        r"\bgia",
        r"somet",
        r"esvil",
        r"lyst",
        r"arriag",
        r"gior"
    ]);
    static ref ADD_PATTERN_REGEXES: RegexSet = regexset_builder(&[
        r"y\b",
        r"ia",
        r"\bmc",
        r"[il]e\b",
        r"ted\b",
        r"ee\b",
        r"io\b",
        r"ded\b",
        r"[io]er\b",
        r"y[bckglmnrstwxv]",
        r"sms?\b",
        r"eo",
        r"[eior]ed\b",
        r"iol",
        r"\bhy",
        r"iu",
        r"s'",
        r"oe\b",
        r"iot",
        r"tua",
        r"aue",
        r"ea\b",
        r"iest\b",
        r"ios",
        r"yst",
        r"nte\b",
        r"ce's",
        r"ying\b",
        r"[bcdfgkopt]led\b",
        r"ciat",
        r"lement",
        r"typ",
        r"ly[dehops]",
        r"[drv]ious",
        r"z's\b",
        r"ae\b",
        r"io[mpr]",
        r"tre\b",
        r"ione\b",
        r"[cdehlorn]ue\b",
        r"se's",
        r"nua",
        r"x'",
        r"oing",
        r"yz",
        r"creat",
        r"lua",
        r"iod",
        r"\breass",
        r"eing\b",
        r"dua",
        r"[bdprz]ion",
        r"iello\b",
        r"oa\b",
        r"ge's",
        r"phys",
        r"eact",
        r"ioc",
        r"iog",
        r"scien",
        r"dys",
        r"uou",
        r"\brein",
        r"ienn",
        r"rya",
        r"bre\b",
        r"tke\b",
        r"ryd",
        r"sh's\b",
        r"rua",
        r"ryp",
        r"rient",
        r"uing",
        r"xual",
        r"eely\b",
        r"leman\b",
        r"fluen",
        r"he'",
        r"dre\b",
        r"iet",
        r"loui",
        r"dl\b",
        r"\bio",
        r"rys",
        r"tui",
        r"rye",
        r"\bcoe",
        r"\breali",
        r"ntes\b",
        r"ch'",
        r"mye",
        r"eeman\b",
        r"ryo",
        r"linea",
        r"theat",
        r"reapp",
        r"oers\b",
        r"tys",
        r"\bcyp",
        r"eemp",
        r"nys",
        r"aic\b",
        r"cua",
        r"tl\b",
        r"tres\b",
        r"ciano",
        r"lione",
        r"eand",
        r"\bdya",
        r"gyp",
        r"croat",
        r"heroi",
        r"rearr",
        r"eex",
        r"cre\b",
        r"oniou",
        r"eum\b",
        r"fred\b",
        r"dien",
        r"oua",
        r"oincid",
        r"coordi",
        r"nucle",
        r"nyd",
        r"\breen",
        r"\breun",
        r"bys",
        r"iale\b",
        r"ifiers",
        r"rean",
        r"pre\b",
        r"iore\b",
        r"-in\b",
    ]);
}

fn regex_builder(pattern: &str) -> Regex {
    RegexBuilder::new(pattern)
        .case_insensitive(true)
        .build()
        .unwrap()
}

fn regexset_builder(patterns: &[&str]) -> RegexSet {
    RegexSetBuilder::new(patterns)
        .case_insensitive(true)
        .build()
        .unwrap()
}

#[wasm_bindgen]
pub fn word_count(text: &str) -> usize {
    WORD_REGEX.find_iter(text).count()
}

#[wasm_bindgen]
pub fn sentence_count(text: &str) -> usize {
    SENTENCE_REGEX.find_iter(text).count()
}

#[wasm_bindgen]
pub fn syllables_in_text(text: &str) -> usize {
    WORD_REGEX
        .find_iter(text)
        .map(|word| syllables_in_word(word.as_str()))
        .sum()
}

fn syllables_in_word(word: &str) -> usize {
    let vowel_groups: usize = VOWEL_GROUPS_REGEX.find_iter(word).count();
    let add_count: usize = ADD_PATTERN_REGEXES.matches(word).iter().count();
    let minus_count: usize = DEDUCT_PATTERN_REGEXES.matches(word).iter().count();

    if vowel_groups + add_count < minus_count + 1 {
        return 1;
    }
    return vowel_groups + add_count - minus_count;
}

#[wasm_bindgen]
pub fn flesh_kincaid_reading_ease_score(text: &str) -> f32 {
    let word_count = word_count(text);
    let sentence_count = sentence_count(text);
    let syllables_in_text = syllables_in_text(text);

    206.835
        - 1.015 * word_count as f32 / sentence_count as f32
        - 84.6 * syllables_in_text as f32 / word_count as f32
}

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

    #[test]
    fn test_syllables_in_text() {
        assert_eq!(syllables_in_text(""), 0);
        assert_eq!(syllables_in_text("Hello"), 2);
        assert_eq!(syllables_in_text("Hello World"), 3);
        assert_eq!(syllables_in_text("Test-case"), 2);
        assert_eq!(syllables_in_text("Hello, World! This is a test"), 7);
        assert_eq!(syllables_in_text("Zylka"), 2);
    }

    #[test]
    fn test_syllables_in_word() {
        assert_eq!(syllables_in_word("unaware"), 3);
        assert_eq!(syllables_in_word("sum"), 1);
        assert_eq!(syllables_in_word("some"), 1);
        assert_eq!(syllables_in_word("pernicious"), 3);
        assert_eq!(syllables_in_word("egregious"), 3);
    }

    #[test]
    fn test_word_count() {
        assert_eq!(word_count("sample text"), 2);
        assert_eq!(word_count("$5 only"), 1);
        assert_eq!(word_count("This is noted in the book(1)"), 6);
        assert_eq!(
            word_count(
                "A sentence that's long—very-long, and contains some real world stuff;funny!"
            ),
            12
        );
    }
}