From a0e595b37269767be4f7f2d7387e6a2dd6ccddcc Mon Sep 17 00:00:00 2001 From: CodyHenshaw Date: Tue, 9 Jul 2013 09:57:59 -0400 Subject: [PATCH 1/9] updates --- readability.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/readability.js b/readability.js index 30e76f5..70de698 100644 --- a/readability.js +++ b/readability.js @@ -118,7 +118,8 @@ var colemanLiau = function(input) { // automatedReadability("I immediately regret this decision."); // fleschKincaidEase("I’ll have a Manhattan. And kick the vermouth to the side with a pair of steel-toed boots."); -fleschKincaidGradeLevel("Timmy's costume unboxing event is now LIVE! Join us in chat & get the chance to win mystery prizes"); +// fleschKincaidGradeLevel("Timmy's costume unboxing event is now LIVE! Join us in chat & get the chance to win mystery prizes"); // gunningFog("I love scotch. Scotchy scotch scotch. Here it goes down, down into my belly."); // smogIndex("Oh, I can barely lift my right arm ’cause I did so many. I don’t know if you heard me counting. I did over a thousand."); -// colemanLiau("Discovered by the Germans in 1904, they named it San Diego, which of course in German means ‘a whale’s vagina."); \ No newline at end of file +// colemanLiau("Discovered by the Germans in 1904, they named it San Diego, which of course in German means ‘a whale’s vagina." +fleschKincaidGradeLevel("hello there marley and austin"); \ No newline at end of file From 6066836939b429eadd2819dd785947305cd1b17b Mon Sep 17 00:00:00 2001 From: Cody Henshaw Date: Sat, 12 Apr 2014 10:24:29 -0400 Subject: [PATCH 2/9] Update readability.js --- readability.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/readability.js b/readability.js index 70de698..3b67748 100644 --- a/readability.js +++ b/readability.js @@ -121,5 +121,4 @@ var colemanLiau = function(input) { // fleschKincaidGradeLevel("Timmy's costume unboxing event is now LIVE! Join us in chat & get the chance to win mystery prizes"); // gunningFog("I love scotch. Scotchy scotch scotch. Here it goes down, down into my belly."); // smogIndex("Oh, I can barely lift my right arm ’cause I did so many. I don’t know if you heard me counting. I did over a thousand."); -// colemanLiau("Discovered by the Germans in 1904, they named it San Diego, which of course in German means ‘a whale’s vagina." -fleschKincaidGradeLevel("hello there marley and austin"); \ No newline at end of file +// colemanLiau("Discovered by the Germans in 1904, they named it San Diego, which of course in German means ‘a whale’s vagina."x From 7f7971b504b90d122deb44a1f358e1c99e85f418 Mon Sep 17 00:00:00 2001 From: Cody Henshaw Date: Sun, 26 Oct 2014 13:06:21 -0400 Subject: [PATCH 3/9] Updates --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f396570..fd2d02f 100644 --- a/README.md +++ b/README.md @@ -55,4 +55,4 @@ Total Characters: 86 Total Words: 20 Total Sentences: 1 Coleman Liau Index: 9.511999999999997 -``` \ No newline at end of file +``` From f7bbc63c6dbbef4a2c5a12ab39076461f09278c7 Mon Sep 17 00:00:00 2001 From: Cody Henshaw Date: Fri, 25 Sep 2015 21:26:49 -0700 Subject: [PATCH 4/9] Convert to ES6, Add Gulp --- .gitignore | 1 + README.md | 73 +++++++++++++++------------------------------ build/index.js | 75 +++++++++++++++++++++++++++++++++++++++++++++++ gulpfile.babel.js | 19 ++++++++++++ package.json | 30 +++++++++++++++++++ src/index.es6.js | 68 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 217 insertions(+), 49 deletions(-) create mode 100644 .gitignore create mode 100644 build/index.js create mode 100644 gulpfile.babel.js create mode 100644 package.json create mode 100644 src/index.es6.js diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3c3629e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules diff --git a/README.md b/README.md index fd2d02f..2e8a4d6 100644 --- a/README.md +++ b/README.md @@ -2,57 +2,32 @@ Readability =========== Automated Readability Index, Flesch-Kincaid, Gunning-Fog, SMOG Index, Coleman-Liau -Examples -======== -**Automated Readability Index** -``` javascript -automatedReadability("I immediately regret this decision."); -Characters: 30 -Words: 5 -Sentences: 1 -Automated Readability Index: 9.329999999999998 +### Examples ``` - -**Flesch Kincaid Ease** -``` javascript -fleschKincaidEase("I’ll have a Manhattan. And kick the vermouth to the side with a pair of steel-toed boots."); -Total Words: 17 -Total Sentences: 2 -Total Syllables: 21 -Flesch Kincaid Reading Ease: 93.70161764705884 +> const testPhrase = 'No kidding - Lorenzo called off his trip to visit Mexico City just because they told him the conquistadores were extinct.'; ``` - -**Flesch Kincaid Grade Level** -``` javascript -fleschKincaidGradeLevel("He had a voice that could make a wolverine purr and suits so fine they made Sinatra look like a hobo."); -Total Words: 21 -Total Sentences: 1 -Total Syllables: 26 -Flesch Kincaid Grade Level: 7.209523809523812 +#### [Automated Readability Index](https://en.wikipedia.org/wiki/Automated_readability_index) ``` - -**Gunning Fog Score** -``` javascript -gunningFog("I love scotch. Scotchy scotch scotch. Here it goes down, down into my belly."); -Total Words: 14 -Total Sentences: 3 -Total Complex Words: 0 -Gunning Fog Score: 1.866666666666667 -``` - -**SMOG Index** -``` javascript -smogIndex("Oh, I can barely lift my right arm ’cause I did so many. I don’t know if you heard me counting. I did over a thousand."); -Number of Sentences: 3 -Polysyllabic Words: 1 -SMOG index: 3.7792166259557014 +> Readability.automatedReadabilityIndex(testPhrase); +11.274285714285718 ``` - -**Coleman-Liau Index** -``` javascript -colemanLiau("Discovered by the Germans in 1904, they named it San Diego, which of course in German means ‘a whale’s vagina."); -Total Characters: 86 -Total Words: 20 -Total Sentences: 1 -Coleman Liau Index: 9.511999999999997 +#### [SMOG Index](https://en.wikipedia.org/wiki/SMOG) +``` +> Readability.SMOGIndex(testPhrase); +10.065306667255596 +``` +#### [Gunning Fog](https://en.wikipedia.org/wiki/Gunning_fog_index) +``` +> Readability.gunningFog(testPhrase); +14.114285714285714 +``` +#### [Coleman Liau](https://en.wikipedia.org/wiki/Coleman%E2%80%93Liau_index) +``` +> Readability.colemanLiau(testPhrase); +11.952857142857141 +``` +#### [Flesch Kincaid](https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests) +``` +> Readability.fleschKincaid(testPhrase); +{ ease: 52.57714285714289, gradeLevel: 11.142857142857142 } ``` diff --git a/build/index.js b/build/index.js new file mode 100644 index 0000000..1772ca1 --- /dev/null +++ b/build/index.js @@ -0,0 +1,75 @@ +'use strict'; + +Object.defineProperty(exports, '__esModule', { + value: true +}); +var Readability = { + getSyllableCount: function getSyllableCount(input) { + // lol @ this, obviously not 100% + return input.split(' ').reduce(function (a, b) { + return a + (b.length <= 3 ? 1 : b.toLowerCase().replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '').replace(/^y/, '').match(/[aeiouy]{1,2}/g).length); + }, 0); + }, + getCharacterCount: function getCharacterCount(input) { + return input.trim().match(/\w/g).length; + }, + getWordCount: function getWordCount(input) { + return input.trim().match(/\S+/g).length; + }, + getSentenceCount: function getSentenceCount(input) { + return input.trim().match(/[^\r\n.!?]+(:?(:?\r\n|[\r\n]|[.!?])+|$)/gi).length; + }, + getComplexWords: function getComplexWords(input) { + var _this = this; + + return input.trim().replace(/[^\w\s]/ig, '').split(' ').reduce(function (a, b) { + return a + (_this.getSyllableCount(b) >= 3 ? 1 : 0); + }, 0); + }, + automatedReadabilityIndex: function automatedReadabilityIndex(input) { + // 4.71(characters / words) + 0.5(words / sentences) - 21.43 + var nChar = this.getCharacterCount(input); + var nWord = this.getWordCount(input); + var nSent = this.getSentenceCount(input); + + return 4.71 * (nChar / nWord) + 0.5 * (nWord / nSent) - 21.43; + }, + SMOGIndex: function SMOGIndex(input) { + // 1.0430 * sqrt(num polysyllables * (30/number of sentences) + 3.1291) + var nSent = this.getSentenceCount(input); + var nComp = this.getComplexWords(input); + + return 1.0430 * Math.sqrt(nComp * (30 / nSent) + 3.1291); + }, + gunningFog: function gunningFog(input) { + // 0.4[(words/sentences) + 100(complex words/words)] + var nWord = this.getWordCount(input); + var nSent = this.getSentenceCount(input); + var nComp = this.getComplexWords(input); + + return 0.4 * (nWord / nSent + 100 * (nComp / nWord)); + }, + colemanLiau: function colemanLiau(input) { + // 5.89 x (characters/words) - 0.3 x (sentences/words) - 15.8 + var nChar = this.getCharacterCount(input); + var nWord = this.getWordCount(input); + var nSent = this.getSentenceCount(input); + + return 5.89 * (nChar / nWord) - 0.3 * (nSent / nWord) - 15.8; + }, + fleschKincaid: function fleschKincaid(input) { + // ease - 206.835 - 1.015(total words/total sentences) - 84.6(total syllables/total words) + // grade level - 0.39(total words/total sentences) + 11.8(total syllables/total words) - 15.59 + var nWord = this.getWordCount(input); + var nSent = this.getSentenceCount(input); + var nSyll = this.getSyllableCount(input); + + return { + ease: 206.835 - 1.015 * (nWord / nSent) - 84.6 * (nSyll / nWord), + gradeLevel: 0.39 * (nWord / nSent) + 11.8 * (nSyll / nWord) - 15.59 + }; + } +}; + +exports['default'] = Readability; +module.exports = exports['default']; \ No newline at end of file diff --git a/gulpfile.babel.js b/gulpfile.babel.js new file mode 100644 index 0000000..705deaa --- /dev/null +++ b/gulpfile.babel.js @@ -0,0 +1,19 @@ +import gulp from 'gulp'; +import babel from 'gulp-babel'; +import path from 'path'; +import rename from 'gulp-rename'; + +const paths = { + src: path.join(__dirname, 'src'), + build: path.join(__dirname, 'build'), +}; + +gulp.task('default', ['build:js']); +gulp.task('build:js', () => { + gulp.src(path.join(paths.src, 'index.es6.js')) + .pipe(babel()) + .pipe(rename('index.js')) + .pipe(gulp.dest(paths.build)) +}); + +gulp.watch(path.join(paths.src, '**/*'), ['build:js']); diff --git a/package.json b/package.json new file mode 100644 index 0000000..743d99a --- /dev/null +++ b/package.json @@ -0,0 +1,30 @@ +{ + "name": "readability", + "version": "0.0.1", + "description": "Automated Readability Index, Flesch-Kincaid, Gunning-Fog, SMOG Index, Coleman-Liau", + "main": "./build/index.js", + "dependencies": { + "babel": "^5.8.23", + "gulp": "^3.9.0", + "gulp-babel": "^5.2.1", + "gulp-rename": "^1.2.2", + "path": "^0.12.7" + }, + "devDependencies": { + "gulp-rename": "^1.2.2", + "path": "^0.12.7" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/brbcoding/Readability.git" + }, + "author": "Cody Henshaw", + "license": "ISC", + "bugs": { + "url": "https://github.com/brbcoding/Readability/issues" + }, + "homepage": "https://github.com/brbcoding/Readability#readme" +} diff --git a/src/index.es6.js b/src/index.es6.js new file mode 100644 index 0000000..8798772 --- /dev/null +++ b/src/index.es6.js @@ -0,0 +1,68 @@ +const Readability = { + getSyllableCount(input) { + // lol @ this, obviously not 100% + return input.split(' ').reduce((a, b) => { + return a + (b.length <= 3 ? 1 : b.toLowerCase().replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '') + .replace(/^y/, '').match(/[aeiouy]{1,2}/g).length); + }, 0); + }, + getCharacterCount(input) { + return input.trim().match(/\w/g).length; + }, + getWordCount(input) { + return input.trim().match(/\S+/g).length; + }, + getSentenceCount(input) { + return input.trim().match(/[^\r\n.!?]+(:?(:?\r\n|[\r\n]|[.!?])+|$)/gi).length; + }, + getComplexWords(input) { + return input.trim().replace(/[^\w\s]/ig, '').split(' ').reduce((a, b) => { + return a + (this.getSyllableCount(b) >= 3 ? 1 : 0); + }, 0); + }, + automatedReadabilityIndex(input) { + // 4.71(characters / words) + 0.5(words / sentences) - 21.43 + const nChar = this.getCharacterCount(input); + const nWord = this.getWordCount(input); + const nSent = this.getSentenceCount(input); + + return ((4.71 * (nChar / nWord)) + (0.5 * (nWord / nSent))) - 21.43; + }, + SMOGIndex(input) { + // 1.0430 * sqrt(num polysyllables * (30/number of sentences) + 3.1291) + const nSent = this.getSentenceCount(input); + const nComp = this.getComplexWords(input); + + return 1.0430 * Math.sqrt(nComp * (30 / nSent) + 3.1291); + }, + gunningFog(input) { + // 0.4[(words/sentences) + 100(complex words/words)] + const nWord = this.getWordCount(input); + const nSent = this.getSentenceCount(input); + const nComp = this.getComplexWords(input); + + return 0.4 * ((nWord / nSent) + 100 * (nComp / nWord)); + }, + colemanLiau(input) { + // 5.89 x (characters/words) - 0.3 x (sentences/words) - 15.8 + const nChar = this.getCharacterCount(input); + const nWord = this.getWordCount(input); + const nSent = this.getSentenceCount(input); + + return 5.89 * (nChar / nWord) - 0.3 * (nSent / nWord) - 15.8; + }, + fleschKincaid(input) { + // ease - 206.835 - 1.015(total words/total sentences) - 84.6(total syllables/total words) + // grade level - 0.39(total words/total sentences) + 11.8(total syllables/total words) - 15.59 + const nWord = this.getWordCount(input); + const nSent = this.getSentenceCount(input); + const nSyll = this.getSyllableCount(input); + + return { + ease: 206.835 - 1.015 * (nWord / nSent) - 84.6 * (nSyll / nWord), + gradeLevel: 0.39 * (nWord / nSent) + 11.8 * (nSyll / nWord) - 15.59 + } + }, +}; + +export default Readability; From 27c8f63578642c207369bea686cacb0550f6c2ec Mon Sep 17 00:00:00 2001 From: Cody Henshaw Date: Fri, 25 Sep 2015 21:28:27 -0700 Subject: [PATCH 5/9] remove readability.js --- readability.js | 124 ------------------------------------------------- 1 file changed, 124 deletions(-) delete mode 100644 readability.js diff --git a/readability.js b/readability.js deleted file mode 100644 index 3b67748..0000000 --- a/readability.js +++ /dev/null @@ -1,124 +0,0 @@ -var CHAR_REGEX = /\w/g; -var WORD_REGEX = /\S+/g; -var SENT_REGEX = /[^\r\n.!?]+(:?(:?\r\n|[\r\n]|[.!?])+|$)/gi; - -// syllables in word -var countSyllables = function(word) { - word = word.toLowerCase(); - if(word.length <= 3) { return 1; } - word = word.replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, ''); - word = word.replace(/^y/, ''); - return word.match(/[aeiouy]{1,2}/g).length; -}; - -// Automated Readability Index -var automatedReadability = function(input) { - // get counts - var charCount = input.match(CHAR_REGEX).length; - var wordCount = input.match(WORD_REGEX).length; - var sentCount = input.match(SENT_REGEX).length; - // 4.71(characters/words) + 0.5(words/sentences) - 21.43 - var ariResult = 4.71 * (charCount / wordCount) + 0.5 * (wordCount / sentCount) - 21.43; - console.log("Characters: " + charCount); - console.log("Words: " + wordCount); - console.log("Sentences: " + sentCount); - console.log("Automated Readability Index: " + ariResult); - return ariResult; -}; - -// Flesch-Kincaid Reading Ease -var fleschKincaidEase = function(input) { - // 206.835 - 1.015(total words/total sentences) - 84.6(total syllables/total words) - // split input into individual words, then get the total syllables - var syllCount = 0; - var words = input.replace(/[^\w\s]/ig, "").split(" "); - var wordCount = words.length; - var sentCount = input.match(SENT_REGEX).length; - for(i = 0; i < words.length; i++){ - syllCount += countSyllables(words[i]); - } - var fkeResult = 206.835 - 1.015 * (wordCount / sentCount) - 84.6 * (syllCount / wordCount); - console.log("Total Words: " + wordCount); - console.log("Total Sentences: " + sentCount); - console.log("Total Syllables: " + syllCount); - console.log("Flesch Kincaid Reading Ease: " + fkeResult); - return fkeResult; -}; - -// Flesch-Kincaid Grade Level -var fleschKincaidGradeLevel = function(input) { - // 0.39(total words/total sentences) + 11.8(total syllables/total words) - 15.59 - var syllCount = 0; - var words = input.replace(/[^\w\s]/ig, "").split(" "); - var wordCount = words.length; - var sentCount = input.match(SENT_REGEX).length; - for(i = 0; i < words.length; i++){ - syllCount += countSyllables(words[i]); - } - var fkgResult = 0.39 * (wordCount / sentCount) + 11.8 * (syllCount / wordCount) - 15.59; - console.log("Total Words: " + wordCount); - console.log("Total Sentences: " + sentCount); - console.log("Total Syllables: " + syllCount); - console.log("Flesch Kincaid Grade Level: " + fkgResult); - return fkgResult; - -}; - -// Gunning Fog Index -var gunningFog = function(input) { - // 0.4[(words/sentences) + 100(complex words/words)] - var compWords = 0; - var words = input.replace(/[^\w\s]/ig, "").split(" "); - var wordCount = words.length; - var sentCount = input.match(SENT_REGEX).length; - for(i = 0; i < words.length; i++){ - if(countSyllables(words[i]) >= 3) { - compWords += 1; - } - } - var gfsResult = 0.4 * ((wordCount/sentCount) + 100 * (compWords/wordCount)); - console.log("Total Words: " + wordCount); - console.log("Total Sentences: " + sentCount); - console.log("Total Complex Words: " + compWords); - console.log("Gunning Fog Score: " + gfsResult); - return gfsResult; - -}; -// SMOG Index -var smogIndex = function(input) { - // 1.0430 * sqrt(num polysyllables * (30/number of sentences) + 3.1291) - var polyWords = 0; - var sentCount = input.match(SENT_REGEX).length; - var words = input.replace(/[^\w\s]/ig, "").split(" "); - for(i = 0; i < words.length; i++){ - if(countSyllables(words[i]) >= 3) { - polyWords += 1; - } - } - var smgResult = 1.0430 * Math.sqrt(polyWords * (30/sentCount) + 3.1291); - console.log("Total of Sentences: " + sentCount); - console.log("Polysyllabic Words: " + polyWords); - console.log("SMOG index: " + smgResult); - return smgResult; -}; - -// Coleman-Liau Index -var colemanLiau = function(input) { - // 5.89 x (characters/words) - 0.3 x (sentences/words) - 15.8 - var charCount = input.match(CHAR_REGEX).length; - var wordCount = input.match(WORD_REGEX).length; - var sentCount = input.match(SENT_REGEX).length; - var cliResult = 5.89 * (charCount / wordCount) - 0.3 * (sentCount / wordCount) - 15.8; - console.log("Total Characters: " + charCount); - console.log("Total Words: " + wordCount); - console.log("Total Sentences: " + sentCount); - console.log("Coleman Liau Index: " + cliResult); - return cliResult; -}; - -// automatedReadability("I immediately regret this decision."); -// fleschKincaidEase("I’ll have a Manhattan. And kick the vermouth to the side with a pair of steel-toed boots."); -// fleschKincaidGradeLevel("Timmy's costume unboxing event is now LIVE! Join us in chat & get the chance to win mystery prizes"); -// gunningFog("I love scotch. Scotchy scotch scotch. Here it goes down, down into my belly."); -// smogIndex("Oh, I can barely lift my right arm ’cause I did so many. I don’t know if you heard me counting. I did over a thousand."); -// colemanLiau("Discovered by the Germans in 1904, they named it San Diego, which of course in German means ‘a whale’s vagina."x From 8bfc6cc6c2dcfaf885293020a467852f99caf785 Mon Sep 17 00:00:00 2001 From: Cody Henshaw Date: Sat, 26 Sep 2015 14:48:04 -0700 Subject: [PATCH 6/9] minor updates to lib, add index.html with examples for gh-pages --- README.md | 4 +- build/index.js | 20 ++---- index.html | 164 +++++++++++++++++++++++++++++++++++++++++++++++ package.json | 1 + src/index.es6.js | 14 ++-- 5 files changed, 180 insertions(+), 23 deletions(-) create mode 100644 index.html diff --git a/README.md b/README.md index 2e8a4d6..be26fad 100644 --- a/README.md +++ b/README.md @@ -8,12 +8,12 @@ Automated Readability Index, Flesch-Kincaid, Gunning-Fog, SMOG Index, Coleman-Li ``` #### [Automated Readability Index](https://en.wikipedia.org/wiki/Automated_readability_index) ``` -> Readability.automatedReadabilityIndex(testPhrase); +> Readability.automatedReadability(testPhrase); 11.274285714285718 ``` #### [SMOG Index](https://en.wikipedia.org/wiki/SMOG) ``` -> Readability.SMOGIndex(testPhrase); +> Readability.smog(testPhrase); 10.065306667255596 ``` #### [Gunning Fog](https://en.wikipedia.org/wiki/Gunning_fog_index) diff --git a/build/index.js b/build/index.js index 1772ca1..514ae38 100644 --- a/build/index.js +++ b/build/index.js @@ -1,12 +1,9 @@ 'use strict'; -Object.defineProperty(exports, '__esModule', { - value: true -}); var Readability = { getSyllableCount: function getSyllableCount(input) { // lol @ this, obviously not 100% - return input.split(' ').reduce(function (a, b) { + return input.trim().split(' ').reduce(function (a, b) { return a + (b.length <= 3 ? 1 : b.toLowerCase().replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '').replace(/^y/, '').match(/[aeiouy]{1,2}/g).length); }, 0); }, @@ -26,15 +23,15 @@ var Readability = { return a + (_this.getSyllableCount(b) >= 3 ? 1 : 0); }, 0); }, - automatedReadabilityIndex: function automatedReadabilityIndex(input) { + automatedReadability: function automatedReadability(input) { // 4.71(characters / words) + 0.5(words / sentences) - 21.43 var nChar = this.getCharacterCount(input); var nWord = this.getWordCount(input); var nSent = this.getSentenceCount(input); - - return 4.71 * (nChar / nWord) + 0.5 * (nWord / nSent) - 21.43; + var arIdx = 4.71 * (nChar / nWord) + 0.5 * (nWord / nSent) - 21.43; + return arIdx > 0 ? arIdx : 0; }, - SMOGIndex: function SMOGIndex(input) { + smog: function smog(input) { // 1.0430 * sqrt(num polysyllables * (30/number of sentences) + 3.1291) var nSent = this.getSentenceCount(input); var nComp = this.getComplexWords(input); @@ -47,7 +44,7 @@ var Readability = { var nSent = this.getSentenceCount(input); var nComp = this.getComplexWords(input); - return 0.4 * (nWord / nSent + 100 * (nComp / nWord)); + return 0.4 * (nWord / nSent) + 100 * (nComp / nWord); }, colemanLiau: function colemanLiau(input) { // 5.89 x (characters/words) - 0.3 x (sentences/words) - 15.8 @@ -69,7 +66,4 @@ var Readability = { gradeLevel: 0.39 * (nWord / nSent) + 11.8 * (nSyll / nWord) - 15.59 }; } -}; - -exports['default'] = Readability; -module.exports = exports['default']; \ No newline at end of file +}; \ No newline at end of file diff --git a/index.html b/index.html new file mode 100644 index 0000000..b81eb1a --- /dev/null +++ b/index.html @@ -0,0 +1,164 @@ + + + + + Example + + + + +
+

Readability

+ +
+
+ + + + diff --git a/package.json b/package.json index 743d99a..ff06b76 100644 --- a/package.json +++ b/package.json @@ -12,6 +12,7 @@ }, "devDependencies": { "gulp-rename": "^1.2.2", + "gulp-util": "^3.0.6", "path": "^0.12.7" }, "scripts": { diff --git a/src/index.es6.js b/src/index.es6.js index 8798772..962b9c6 100644 --- a/src/index.es6.js +++ b/src/index.es6.js @@ -1,7 +1,7 @@ const Readability = { getSyllableCount(input) { // lol @ this, obviously not 100% - return input.split(' ').reduce((a, b) => { + return input.trim().split(' ').reduce((a, b) => { return a + (b.length <= 3 ? 1 : b.toLowerCase().replace(/(?:[^laeiouy]es|ed|[^laeiouy]e)$/, '') .replace(/^y/, '').match(/[aeiouy]{1,2}/g).length); }, 0); @@ -20,15 +20,15 @@ const Readability = { return a + (this.getSyllableCount(b) >= 3 ? 1 : 0); }, 0); }, - automatedReadabilityIndex(input) { + automatedReadability(input) { // 4.71(characters / words) + 0.5(words / sentences) - 21.43 const nChar = this.getCharacterCount(input); const nWord = this.getWordCount(input); const nSent = this.getSentenceCount(input); - - return ((4.71 * (nChar / nWord)) + (0.5 * (nWord / nSent))) - 21.43; + const arIdx = 4.71 * (nChar / nWord) + 0.5 * (nWord / nSent) - 21.43; + return arIdx > 0 ? arIdx : 0; }, - SMOGIndex(input) { + smog(input) { // 1.0430 * sqrt(num polysyllables * (30/number of sentences) + 3.1291) const nSent = this.getSentenceCount(input); const nComp = this.getComplexWords(input); @@ -41,7 +41,7 @@ const Readability = { const nSent = this.getSentenceCount(input); const nComp = this.getComplexWords(input); - return 0.4 * ((nWord / nSent) + 100 * (nComp / nWord)); + return 0.4 * (nWord / nSent) + 100 * (nComp / nWord); }, colemanLiau(input) { // 5.89 x (characters/words) - 0.3 x (sentences/words) - 15.8 @@ -64,5 +64,3 @@ const Readability = { } }, }; - -export default Readability; From d15fb3c5467754029c559e9d5a0496c395a2f357 Mon Sep 17 00:00:00 2001 From: Cody Henshaw Date: Sat, 26 Sep 2015 14:51:31 -0700 Subject: [PATCH 7/9] add space to testPhrase --- index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/index.html b/index.html index b81eb1a..e558d0d 100644 --- a/index.html +++ b/index.html @@ -73,7 +73,7 @@

Readability

'Anyone who feels that if so many more students whom we haven’t actually admitted', 'are sitting in on the course than ones we have that the room had to be changed,', 'then probably auditors will have to be excluded, is likely to agree that the curriculum needs revision.' - ].join(''); + ].join(' '); document.querySelector('#example-input').innerHTML = testPhrase; updateOutput(testPhrase); From 0d71b01ef46c13ee282201cb2f5315ba484d251b Mon Sep 17 00:00:00 2001 From: Cody Henshaw Date: Sat, 26 Sep 2015 14:56:32 -0700 Subject: [PATCH 8/9] update readme --- README.md | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index be26fad..9eb7e70 100644 --- a/README.md +++ b/README.md @@ -1,32 +1,34 @@ Readability =========== -Automated Readability Index, Flesch-Kincaid, Gunning-Fog, SMOG Index, Coleman-Liau +_Automated Readability Index, Flesch-Kincaid (ease/level), Gunning-Fog, SMOG Index, Coleman-Liau_ -### Examples +### **[Demo](http://brbcoding.github.io/Readability/)** + +#### Examples ``` > const testPhrase = 'No kidding - Lorenzo called off his trip to visit Mexico City just because they told him the conquistadores were extinct.'; ``` -#### [Automated Readability Index](https://en.wikipedia.org/wiki/Automated_readability_index) +##### [Automated Readability Index](https://en.wikipedia.org/wiki/Automated_readability_index) ``` > Readability.automatedReadability(testPhrase); 11.274285714285718 ``` -#### [SMOG Index](https://en.wikipedia.org/wiki/SMOG) +##### [SMOG Index](https://en.wikipedia.org/wiki/SMOG) ``` > Readability.smog(testPhrase); 10.065306667255596 ``` -#### [Gunning Fog](https://en.wikipedia.org/wiki/Gunning_fog_index) +##### [Gunning Fog](https://en.wikipedia.org/wiki/Gunning_fog_index) ``` > Readability.gunningFog(testPhrase); 14.114285714285714 ``` -#### [Coleman Liau](https://en.wikipedia.org/wiki/Coleman%E2%80%93Liau_index) +##### [Coleman Liau](https://en.wikipedia.org/wiki/Coleman%E2%80%93Liau_index) ``` > Readability.colemanLiau(testPhrase); 11.952857142857141 ``` -#### [Flesch Kincaid](https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests) +##### [Flesch Kincaid](https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests) ``` > Readability.fleschKincaid(testPhrase); { ease: 52.57714285714289, gradeLevel: 11.142857142857142 } From ee06424de72ec82ca943ebc9c43b0a0bf092934c Mon Sep 17 00:00:00 2001 From: Cody Henshaw Date: Sat, 26 Sep 2015 18:37:07 -0700 Subject: [PATCH 9/9] style index a bit --- index.html | 57 ++++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 8 deletions(-) diff --git a/index.html b/index.html index e558d0d..649091e 100644 --- a/index.html +++ b/index.html @@ -39,6 +39,10 @@ margin-bottom: 0.75em; } + .output-container:hover > .output-value { + color: #2BA4FF; + } + .output-label, .output-value { display: inline-block; width: 50%; @@ -54,6 +58,32 @@ font-family: 'Cardo', italic; } + .output-label a { + color: #000; + text-decoration: none; + position: relative; + width: auto; + } + + .output-label a:hover { + color: #2BA4FF; + } + + .output-label a:after { + content: ''; + position: absolute; + background: #2BA4FF; + width: 0; + left: 0; + height: 0.1em; + bottom: 0.2em; + } + + .output-label a:hover:after { + transition: width 0.25s ease-in; + width: 100%; + } + .output-value { text-align: right; } @@ -84,26 +114,32 @@

Readability

{ name: 'Automated Readability Index', func: 'automatedReadability', + url: 'https://en.wikipedia.org/wiki/Automated_readability_index' }, { name: 'SMOG Grade (Index)', - func: 'smog' + func: 'smog', + url: 'https://en.wikipedia.org/wiki/SMOG' }, { name: 'Gunning Fog Index', - func: 'gunningFog' + func: 'gunningFog', + url: 'https://en.wikipedia.org/wiki/Gunning_fog_index' }, { name: 'Coleman Liau', - func: 'colemanLiau' + func: 'colemanLiau', + url: 'https://en.wikipedia.org/wiki/Coleman%E2%80%93Liau_index' }, { name: 'Flesch-Kincaid Ease', - func: 'fleschKincaid.ease' + func: 'fleschKincaid.ease', + url: 'https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests' }, { name: 'Flesch-Kincaid Level', - func: 'fleschKincaid.gradeLevel' + func: 'fleschKincaid.gradeLevel', + url: 'https://en.wikipedia.org/wiki/Flesch%E2%80%93Kincaid_readability_tests' } ]; } @@ -124,13 +160,18 @@

Readability

outputLabel = document.createElement('div'); outputValue = document.createElement('div'); + outputLink = document.createElement('a'); + outputContainer = document.createElement('div'); outputLabel.classList.add('output-label'); outputValue.classList.add('output-value'); - outputLabel.innerHTML = method.name; + outputContainer.classList.add('output-container'); + + outputLink.innerHTML = method.name; + outputLink.setAttribute('href', method.url); + outputLink.setAttribute('target', '_blank'); outputValue.innerHTML = val; - outputContainer = document.createElement('div'); - outputContainer.classList.add('output-container'); + outputLabel.appendChild(outputLink); outputContainer.appendChild(outputLabel); outputContainer.appendChild(outputValue); fragment.appendChild(outputContainer);