forked from funador/reading-level
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reading-level.js
58 lines (42 loc) · 1.36 KB
/
reading-level.js
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
const syllable = require('syllable')
const Tokenizer = require('sentence-tokenizer')
exports.readingLevel = (text, full) => {
const tokenizer = new Tokenizer('ChuckNorris')
tokenizer.setEntry(text)
const tokenSentences = tokenizer.getSentences()
const tracker = {
syllables: 0,
words: 0
}
const counts = tokenSentences.reduce((obj, sentence) => {
// strip all punctuation and numbers from the sentence
const words = sentence
.replace(/[^\w\s]|_/g, "")
.replace(/\s+/g, " ")
.replace(/[0-9]/g, '')
.split(' ')
.filter(letter => letter)
obj.syllables += words.reduce((total, word) => total += syllable(word), 0)
obj.words += words.length
return obj
}, tracker)
const { words, syllables } = counts
const sentences = tokenSentences.length
const unrounded = 0.39 * (words / sentences) + 11.8 * (syllables / words) - 15.59
const rounded = Math.round(isNaN(unrounded) ? NaN : unrounded)
const result = {
sentences, words, syllables, unrounded, rounded
}
const err = 'Either no sentences or words, please enter valid text'
const nan = isNaN(result.rounded)
if (nan) {
result.error = err
}
if (full == 'full') {
return result
}
if (nan) {
return err
}
return result.rounded
}