This repository has been archived by the owner on Sep 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathindex.js
239 lines (195 loc) · 7.36 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
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
// TextStatistics.js
// Christopher Giffard (2012)
// 1:1 API Fork of TextStatistics.php by Dave Child (Thanks mate!)
// https://github.com/DaveChild/Text-Statistics
(function(glob) {
function cleanText(text) {
// all these tags should be preceeded by a full stop.
var fullStopTags = ['li', 'p', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'dd'];
fullStopTags.forEach(function(tag) {
text = text.replace("</" + tag + ">",".");
});
text = text
.replace(/<[^>]+>/g, "") // Strip tags
.replace(/[,:;()\/&+]|\-\-/g, " ") // Replace commas, hyphens etc (count them as spaces)
.replace(/[\.!?]/g, ".") // Unify terminators
.replace(/^\s+/, "") // Strip leading whitespace
.replace(/[\.]?(\w+)[\.]?(\w+)@(\w+)[\.](\w+)[\.]?/g, "$1$2@$3$4") // strip periods in email addresses (so they remain counted as one word)
.replace(/[ ]*(\n|\r\n|\r)[ ]*/g, ".") // Replace new lines with periods
.replace(/([\.])[\.]+/g, ".") // Check for duplicated terminators
.replace(/[ ]*([\.])/g, ". ") // Pad sentence terminators
.replace(/\s+/g, " ") // Remove multiple spaces
.replace(/\s+$/, ""); // Strip trailing whitespace
if(text.slice(-1) != '.') {
text += "."; // Add final terminator, just in case it's missing.
}
return text;
}
var TextStatistics = function TextStatistics(text) {
this.text = text ? cleanText(text) : this.text;
};
TextStatistics.prototype.fleschKincaidReadingEase = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round((206.835 - (1.015 * this.averageWordsPerSentence(text)) - (84.6 * this.averageSyllablesPerWord(text)))*10)/10;
};
TextStatistics.prototype.fleschKincaidGradeLevel = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(((0.39 * this.averageWordsPerSentence(text)) + (11.8 * this.averageSyllablesPerWord(text)) - 15.59)*10)/10;
};
TextStatistics.prototype.gunningFogScore = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(((this.averageWordsPerSentence(text) + this.percentageWordsWithThreeSyllables(text, false)) * 0.4)*10)/10;
};
TextStatistics.prototype.colemanLiauIndex = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(((5.89 * (this.letterCount(text) / this.wordCount(text))) - (0.3 * (this.sentenceCount(text) / this.wordCount(text))) - 15.8 ) *10)/10;
};
TextStatistics.prototype.smogIndex = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(1.043 * Math.sqrt((this.wordsWithThreeSyllables(text) * (30 / this.sentenceCount(text))) + 3.1291)*10)/10;
};
TextStatistics.prototype.automatedReadabilityIndex = function(text) {
text = text ? cleanText(text) : this.text;
return Math.round(((4.71 * (this.letterCount(text) / this.wordCount(text))) + (0.5 * (this.wordCount(text) / this.sentenceCount(text))) - 21.43)*10)/10;
};
TextStatistics.prototype.textLength = function(text) {
text = text ? cleanText(text) : this.text;
return text.length;
};
TextStatistics.prototype.letterCount = function(text) {
text = text ? cleanText(text) : this.text;
text = text.replace(/[^a-z]+/ig,"");
return text.length;
};
TextStatistics.prototype.sentenceCount = function(text) {
text = text ? cleanText(text) : this.text;
// Will be tripped up by "Mr." or "U.K.". Not a major concern at this point.
return text.replace(/[^\.!?]/g, '').length || 1;
};
TextStatistics.prototype.wordCount = function(text) {
text = text ? cleanText(text) : this.text;
return text.split(/[^a-z0-9\'@\.\-]+/i).length || 1;
};
TextStatistics.prototype.averageWordsPerSentence = function(text) {
text = text ? cleanText(text) : this.text;
return this.wordCount(text) / this.sentenceCount(text);
};
TextStatistics.prototype.averageSyllablesPerWord = function(text) {
text = text ? cleanText(text) : this.text;
var syllableCount = 0, wordCount = this.wordCount(text), self = this;
text.split(/\s+/).forEach(function(word) {
syllableCount += self.syllableCount(word);
});
// Prevent NaN...
return (syllableCount||1) / (wordCount||1);
};
TextStatistics.prototype.wordsWithThreeSyllables = function(text, countProperNouns) {
text = text ? cleanText(text) : this.text;
var longWordCount = 0, self = this;
countProperNouns = countProperNouns === false ? false : true;
text.split(/\s+/).forEach(function(word) {
// We don't count proper nouns or capitalised words if the countProperNouns attribute is set.
// Defaults to true.
if (!word.match(/^[A-Z]/) || countProperNouns) {
if (self.syllableCount(word) > 2) longWordCount ++;
}
});
return longWordCount;
};
TextStatistics.prototype.percentageWordsWithThreeSyllables = function(text, countProperNouns) {
text = text ? cleanText(text) : this.text;
return (this.wordsWithThreeSyllables(text,countProperNouns) / this.wordCount(text)) * 100;
};
TextStatistics.prototype.syllableCount = function(word) {
var syllableCount = 0,
prefixSuffixCount = 0,
wordPartCount = 0;
// Prepare word - make lower case and remove non-word characters
word = word.toLowerCase().replace(/[^a-z]/g,"");
// Specific common exceptions that don't follow the rule set below are handled individually
// Array of problem words (with word as key, syllable count as value)
var problemWords = {
"simile": 3,
"forever": 3,
"shoreline": 2
};
// Return if we've hit one of those...
if (problemWords.hasOwnProperty(word)) return problemWords[word];
// These syllables would be counted as two but should be one
var subSyllables = [
/cial/,
/tia/,
/cius/,
/cious/,
/giu/,
/ion/,
/iou/,
/sia$/,
/[^aeiuoyt]{2,}ed$/,
/.ely$/,
/[cg]h?e[rsd]?$/,
/rved?$/,
/[aeiouy][dt]es?$/,
/[aeiouy][^aeiouydt]e[rsd]?$/,
/^[dr]e[aeiou][^aeiou]+$/, // Sorts out deal, deign etc
/[aeiouy]rse$/ // Purse, hearse
];
// These syllables would be counted as one but should be two
var addSyllables = [
/ia/,
/riet/,
/dien/,
/iu/,
/io/,
/ii/,
/[aeiouym]bl$/,
/[aeiou]{3}/,
/^mc/,
/ism$/,
/([^aeiouy])\1l$/,
/[^l]lien/,
/^coa[dglx]./,
/[^gq]ua[^auieo]/,
/dnt$/,
/uity$/,
/ie(r|st)$/
];
// Single syllable prefixes and suffixes
var prefixSuffix = [
/^un/,
/^fore/,
/ly$/,
/less$/,
/ful$/,
/ers?$/,
/ings?$/
];
// Remove prefixes and suffixes and count how many were taken
prefixSuffix.forEach(function(regex) {
if (word.match(regex)) {
word = word.replace(regex,"");
prefixSuffixCount ++;
}
});
wordPartCount = word
.split(/[^aeiouy]+/ig)
.filter(function(wordPart) {
return !!wordPart.replace(/\s+/ig,"").length;
})
.length;
// Get preliminary syllable count...
syllableCount = wordPartCount + prefixSuffixCount;
// Some syllables do not follow normal rules - check for them
subSyllables.forEach(function(syllable) {
if (word.match(syllable)) syllableCount --;
});
addSyllables.forEach(function(syllable) {
if (word.match(syllable)) syllableCount ++;
});
return syllableCount || 1;
};
function textStatistics(text) {
return new TextStatistics(text);
}
(typeof module != "undefined" && module.exports) ? (module.exports = textStatistics) : (typeof define != "undefined" ? (define("textstatistics", [], function() { return textStatistics; })) : (glob.textstatistics = textStatistics));
})(this);