forked from dana-ross/flesch-kincaid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflesch-kincaid.js
79 lines (70 loc) · 2.83 KB
/
flesch-kincaid.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
/*eslint prefer-const: "error", sourceType: "module" */
/*eslint-env es6*/
var syllables = function syllables(x) {
/*
* basic algortithm: each vowel-group indicates a syllable, except for: final
* (silent) e 'ia' ind two syl @AddSyl and @SubSyl list regexps to massage the
* basic count. Each match from @AddSyl adds 1 to the basic count, each
* @SubSyl match -1 Keep in mind that when the regexps are checked, any final
* 'e' will have been removed, and all '\'' will have been removed.
*/
var subSyl = [/cial/, /tia/, /cius/, /cious/, /giu/, // belgium!
/ion/, /iou/, /sia$/, /.ely$/, // absolutely! (but not ely!)
/sed$/];
var addSyl = [/ia/, /riet/, /dien/, /iu/, /io/, /ii/, /[aeiouym]bl$/, // -Vble, plus -mble
/[aeiou]{3}/, // agreeable
/^mc/, /ism$/, // -isms
/([^aeiouy])\1l$/, // middle twiddle battle bottle, etc.
/[^l]lien/, // // alien, salient [1]
/^coa[dglx]./, // [2]
/[^gq]ua[^auieo]/, // i think this fixes more than it breaks
/dnt$/];
// (comments refer to titan's /usr/dict/words)
// [1] alien, salient, but not lien or ebbullient...
// (those are the only 2 exceptions i found, there may be others)
// [2] exception for 7 words:
// coadjutor coagulable coagulate coalesce coalescent coalition coaxial
var xx = x.toLowerCase().replace(/'/g, '').replace(/e\b/g, '');
var scrugg = xx.split(/[^aeiouy]+/).filter(Boolean); // '-' should be perhaps added?
return undefined === x || null === x || '' === x ? 0 : 1 === xx.length ? 1 : subSyl.map(function (r) {
return (xx.match(r) || []).length;
}).reduce(function (a, b) {
return a - b;
}) + addSyl.map(function (r) {
return (xx.match(r) || []).length;
}).reduce(function (a, b) {
return a + b;
}) + scrugg.length - (scrugg.length > 0 && '' === scrugg[0] ? 1 : 0) +
// got no vowels? ("the", "crwth")
xx.split(/\b/).map(function (x) {
return x.trim();
}).filter(Boolean).filter(function (x) {
return !x.match(/[.,'!?]/g);
}).map(function (x) {
return x.match(/[aeiouy]/) ? 0 : 1;
}).reduce(function (a, b) {
return a + b;
});
};
var words = function words(x) {
return (x.split(/\s+/) || ['']).length;
};
var sentences = function sentences(x) {
return (x.split('. ') || ['']).length;
};
var syllablesPerWord = function syllablesPerWord(x) {
return syllables(x) / words(x);
};
var wordsPerSentence = function wordsPerSentence(x) {
return words(x) / sentences(x);
};
var rate = exports.rate = function rate(x) {
return 206.835 - 1.015 * wordsPerSentence(x) - 84.6 * syllablesPerWord(x);
};
var grade = exports.grade = function grade(x) {
return 0.39 * wordsPerSentence(x) + 11.8 * syllablesPerWord(x) - 15.59;
};