-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
57 lines (53 loc) · 1.71 KB
/
index.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
/**
* @typedef Counts
* Counts from input document.
* @property {number} sentence
* Number of sentences.
* @property {number} word
* Number of words.
* @property {number} syllable
* Number of syllables.
*/
/**
* @typedef {Counts} FleschCounts
* Deprecated: please use the `Counts` type instead.
*/
const sentenceWeight = 1.015
const wordWeight = 84.6
const base = 206.835
/**
* Given an object containing the number of words (`word`), the number of
* sentences (`sentence`), and the number of syllables (`syllable`) in a
* document, returns the reading ease associated with the document.
*
* @param {Counts} counts
* Counts from input document.
* @returns {number}
* Result is `120` (every sentence consisting of only two one-syllable words)
* or lower (including negative values).
*
* The values have the following semantics:
*
* | Score | Semantics |
* | :----------: | :-------------------------------------------------- |
* | 90.0 – 100.0 | Easily understood by an average 11-year-old student |
* | 60.0 – 70.0 | Easily understood by 13- to 15-year-old students |
* | 0.0 – 30.0 | Best understood by university graduates |
*
* Therefore we can use the following formula to approximate the average age
* a student would understand a document at, given score `score`:
*
* ```js
* const age = 20 - Math.floor(score / 10)
* ```
*/
export function flesch(counts) {
if (!counts || !counts.sentence || !counts.word || !counts.syllable) {
return Number.NaN
}
return (
base -
sentenceWeight * (counts.word / counts.sentence) -
wordWeight * (counts.syllable / counts.word)
)
}